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.SAXPathException;
68 import org.jaxen.saxpath.XPathReader;
69
70 /*** Create an {@link org.jaxen.saxpath.XPathReader} from
71 * either a system property, or a named class.
72 *
73 * <p>
74 * Similar to the SAX API, the <code>XPathReaderFactory</code>
75 * can create an <code>XPathReader</code> from a name of a
76 * class passed in directly, or by inspecting the system
77 * property <code>org.saxpath.driver</code>.
78 *
79 * @author bob mcwhirter (bob@werken.com)
80 */
81 public class XPathReaderFactory
82 {
83 /*** The <code>org.saxpath.driver</code> property name. */
84 public static final String DRIVER_PROPERTY = "org.saxpath.driver";
85
86 /*** The default driver to use if none is configured. */
87 protected static final String DEFAULT_DRIVER = "org.jaxen.saxpath.base.XPathReader";
88
89 private XPathReaderFactory() {}
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 SAXPathException if the property is not set, 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 SAXPathException
104 {
105 String className = null;
106
107 try
108 {
109 className = System.getProperty( DRIVER_PROPERTY );
110 }
111 catch (SecurityException e)
112 {
113
114 }
115
116 if ( className == null
117 ||
118 className.length() == 0 )
119 {
120 className = DEFAULT_DRIVER;
121 }
122
123 return createReader( className );
124 }
125
126 /*** Create an <code>XPathReader</code> using the passed
127 * in class name.
128 *
129 * @param className the name of the class that implements
130 * the <code>XPathReader</code> interface.
131 *
132 * @return an XPathReader
133 *
134 * @throws SAXPathException if the class cannot be
135 * instantiated for some reason, or if the
136 * class doesn't implement the <code>XPathReader</code>
137 * interface
138 */
139 public static XPathReader createReader(String className) throws SAXPathException
140 {
141 Class readerClass = null;
142 XPathReader reader = null;
143
144 try
145 {
146
147
148
149
150 readerClass = Class.forName( className,
151 true,
152 XPathReaderFactory.class.getClassLoader() );
153
154
155
156
157 if ( ! XPathReader.class.isAssignableFrom( readerClass ) )
158 {
159 throw new SAXPathException( "Class [" + className
160 + "] does not implement the org.jaxen.saxpath.XPathReader interface." );
161 }
162 }
163 catch (ClassNotFoundException e)
164 {
165 throw new SAXPathException( e );
166 }
167
168 try
169 {
170 reader = (XPathReader) readerClass.newInstance();
171 }
172 catch (IllegalAccessException e)
173 {
174 throw new SAXPathException( e );
175 }
176 catch (InstantiationException e)
177 {
178 throw new SAXPathException( e );
179 }
180
181 if ( reader == null )
182 {
183 throw new SAXPathException( "Unable to create XPathReader" );
184 }
185
186 return reader;
187 }
188 }