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