1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 package org.jaxen.saxpath.helpers;
66
67 import org.jaxen.saxpath.XPathReader;
68
69 /*** Create an {@link org.jaxen.saxpath.XPathReader} from
70 * either a system property, or a named class.
71 *
72 * <p>
73 * Similar to the SAX API, the <code>XPathReaderFactory</code>
74 * can create an <code>XPathReader</code> from a name of a
75 * class passed in directly, or by inspecting the system
76 * property <code>org.saxpath.driver</code>.
77 *
78 * @author bob mcwhirter (bob@werken.com)
79 */
80 public class XPathReaderFactory
81 {
82 /*** The <code>org.saxpath.driver</code> property name. */
83 public static final String DRIVER_PROPERTY = "org.saxpath.driver";
84
85 /*** The default driver to use if none is configured. */
86 protected static final String DEFAULT_DRIVER = "org.jaxen.saxpath.base.XPathReader";
87
88 /*** Should the default driver be used */
89 private static boolean USE_DEFAULT = true;
90
91
92 /*** Create an <code>XPathReader</code> using the value of
93 * the <code>org.saxpath.driver</code> system property.
94 *
95 * @return An instance of the <code>XPathReader</code> specified
96 * by the <code>org.saxpath.driver</code> property.
97 *
98 * @throws org.jaxen.saxpath.SAXPathException if the property is unset, or if
99 * the class can not be instantiated for some reason.,
100 * or if the class doesn't implement the <code>XPathReader</code>
101 * interface.
102 */
103 public static XPathReader createReader() throws org.jaxen.saxpath.SAXPathException
104 {
105 String className = null;
106
107 boolean securityException = false;
108
109 try
110 {
111 className = System.getProperty( DRIVER_PROPERTY );
112 }
113 catch (SecurityException e)
114 {
115 securityException = true;
116 }
117
118 if ( className == null
119 ||
120 className.length() == 0 )
121 {
122 if ( USE_DEFAULT )
123 {
124 className = DEFAULT_DRIVER;
125 }
126 else
127 {
128 if ( securityException )
129 {
130 throw new org.jaxen.saxpath.SAXPathException( "Reading of property " + DRIVER_PROPERTY + " disallowed." );
131 }
132 else
133 {
134 throw new org.jaxen.saxpath.SAXPathException( "Property " + DRIVER_PROPERTY + " not set" );
135 }
136 }
137 }
138
139 return createReader( className );
140 }
141
142 /*** Create an <code>XPathReader</code> using the passed
143 * in class name.
144 *
145 * @param className The name of the class which implements
146 * the <code>XPathReader</code> interface.
147 *
148 * @throws org.jaxen.saxpath.SAXPathException if the class can not be
149 * instantiated for some reason, or if the
150 * class doesn't implement the <code>XPathReader</code>
151 * interface.
152 */
153 public static XPathReader createReader(String className) throws org.jaxen.saxpath.SAXPathException
154 {
155 Class readerClass = null;
156 XPathReader reader = null;
157
158 try
159 {
160
161
162
163
164 readerClass = Class.forName( className,
165 true,
166 XPathReaderFactory.class.getClassLoader() );
167
168
169
170
171 if ( ! XPathReader.class.isAssignableFrom( readerClass ) )
172 {
173 throw new org.jaxen.saxpath.SAXPathException( "Class [" + className + "] does not implement the org.jaxen.saxpath.XPathReader interface." );
174 }
175 }
176 catch (ClassNotFoundException e)
177 {
178 throw new org.jaxen.saxpath.SAXPathException( e.getMessage() );
179 }
180
181 try
182 {
183 reader = (XPathReader) readerClass.newInstance();
184 }
185 catch (IllegalAccessException e)
186 {
187 throw new org.jaxen.saxpath.SAXPathException( e.getMessage() );
188 }
189 catch (InstantiationException e)
190 {
191 throw new org.jaxen.saxpath.SAXPathException( e.getMessage() );
192 }
193
194 if ( reader == null )
195 {
196 throw new org.jaxen.saxpath.SAXPathException( "Unable to create XPathReader" );
197 }
198
199 return reader;
200 }
201 }