1 /*
2 Copyright (c) 2003, Vilmantas Baranauskas
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright notice,
8 this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright notice,
10 this list of conditions and the following disclaimer in the documentation
11 and/or other materials provided with the distribution.
12 * The names of its contributors may not be used to endorse or promote
13 products derived from this software without specific prior written
14 permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 POSSIBILITY OF SUCH DAMAGE.
27 */
28 package org.sovt.xml;
29
30 import org.apache.commons.digester.AbstractObjectCreationFactory;
31 import org.sovt.Inspector;
32 import org.sovt.SovtLibrary;
33 import org.sovt.impl.GroupInspector;
34 import org.xml.sax.Attributes;
35
36 import java.util.NoSuchElementException;
37
38 /***
39 * Factory of Inspector objects for digister.
40 *
41 * @author Vilmantas Baranauskas (vilmantas_baranauskas@yahoo.com)
42 */
43 public class InspectorFactory extends AbstractObjectCreationFactory {
44 //-- constants -------------------------------------------------------------
45 //-- variables -------------------------------------------------------------
46
47 //-- constructors ----------------------------------------------------------
48 /***
49 * Creates instence of InspectorFactory.
50 */
51 public InspectorFactory() {
52 }
53
54 //-- methods ---------------------------------------------------------------
55 /***
56 * Creates Inspector for given attributes.
57 * Inspector creation order is following:<ol>
58 * <li>Named inspector is returned if "ref" attribute is present.</li>
59 * <li>Class instance is returned if "class" attribute is present.</li>
60 * <li>Instance of GroupInspector.</li>
61 * </ol>
62 *
63 * @param attributes Attribute of xml inspector definition.
64 * @return instance of Inspector.
65 * @throws ClassNotFoundException If unable to instantiate Inspector class.
66 * @throws IllegalAccessException If unable to instantiate Inspector class.
67 * @throws InstantiationException If unable to instantiate Inspector class.
68 */
69 public Object createObject(
70 Attributes attributes
71 ) throws ClassNotFoundException, IllegalAccessException,
72 InstantiationException, CloneNotSupportedException {
73 Inspector inspector;
74 String ref = attributes.getValue("ref");
75 if (ref != null) {
76 inspector = SovtLibrary.getInstance().getInspector(ref);
77 if (inspector == null) {
78 throw new NoSuchElementException("Unknown inspector [" + ref +
79 "]");
80 } else {
81 inspector = (Inspector) inspector.clone();
82 }
83 } else if (attributes.getValue("class") != null) {
84 inspector = (Inspector)
85 Class.forName(attributes.getValue("class")).newInstance();
86 } else {
87 // return instance of default inspector class
88 inspector = new GroupInspector();
89 }
90
91 if (attributes.getValue("id") != null) {
92 // this is named inspector which has to be saved for later reference
93 SovtLibrary.getInstance().addInspector(
94 attributes.getValue("id"),
95 inspector
96 );
97 }
98
99 return inspector;
100 }
101
102 }