001 /*
002 * Copyright 2003-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.xml.xpath;
027
028 /**
029 * <p>An <code>XPathFactory</code> instance can be used to create
030 * {@link javax.xml.xpath.XPath} objects.</p>
031 *
032 *<p>See {@link #newInstance(String uri)} for lookup mechanism.</p>
033 *
034 * <p>The {@link XPathFactory} class is not thread-safe. In other words,
035 * it is the application's responsibility to ensure that at most
036 * one thread is using a {@link XPathFactory} object at any
037 * given moment. Implementations are encouraged to mark methods
038 * as <code>synchronized</code> to protect themselves from broken clients.
039 *
040 * <p>{@link XPathFactory} is not re-entrant. While one of the
041 * <code>newInstance</code> methods is being invoked, applications
042 * may not attempt to recursively invoke a <code>newInstance</code> method,
043 * even from the same thread.
044 *
045 * @author <a href="mailto:Norman.Walsh@Sun.com">Norman Walsh</a>
046 * @author <a href="mailto:Jeff.Suttor@Sun.com">Jeff Suttor</a>
047 *
048 * @version $Revision: 1.4 $, $Date: 2005/11/03 19:34:16 $
049 * @since 1.5
050 */
051 public abstract class XPathFactory {
052
053 /**
054 * <p>The default property name according to the JAXP spec.</p>
055 */
056 public static final String DEFAULT_PROPERTY_NAME = "javax.xml.xpath.XPathFactory";
057
058 /**
059 * <p>Default Object Model URI.</p>
060 */
061 public static final String DEFAULT_OBJECT_MODEL_URI = "http://java.sun.com/jaxp/xpath/dom";
062
063 /**
064 *<p> Take care of restrictions imposed by java security model </p>
065 */
066 private static SecuritySupport ss = new SecuritySupport();
067
068 /**
069 * <p>Protected constructor as {@link #newInstance()} or {@link #newInstance(String uri)}
070 * or {@link #newInstance(String uri, String factoryClassName, ClassLoader classLoader)}
071 * should be used to create a new instance of an <code>XPathFactory</code>.</p>
072 */
073 protected XPathFactory() {
074 }
075
076 /**
077 * <p>Get a new <code>XPathFactory</code> instance using the default object model,
078 * {@link #DEFAULT_OBJECT_MODEL_URI},
079 * the W3C DOM.</p>
080 *
081 * <p>This method is functionally equivalent to:</p>
082 * <pre>
083 * newInstance(DEFAULT_OBJECT_MODEL_URI)
084 * </pre>
085 *
086 * <p>Since the implementation for the W3C DOM is always available, this method will never fail.</p>
087 *
088 * @return Instance of an <code>XPathFactory</code>.
089 *
090 * @throws RuntimeException When there is a failure in creating an
091 * <code>XPathFactory</code> for the default object model.
092 */
093 public static final XPathFactory newInstance() {
094
095 try {
096 return newInstance(DEFAULT_OBJECT_MODEL_URI);
097 } catch (XPathFactoryConfigurationException xpathFactoryConfigurationException) {
098 throw new RuntimeException(
099 "XPathFactory#newInstance() failed to create an XPathFactory for the default object model: "
100 + DEFAULT_OBJECT_MODEL_URI
101 + " with the XPathFactoryConfigurationException: "
102 + xpathFactoryConfigurationException
103 .toString());
104 }
105 }
106
107 /**
108 * <p>Get a new <code>XPathFactory</code> instance using the specified object model.</p>
109 *
110 * <p>To find a <code>XPathFactory</code> object,
111 * this method looks the following places in the following order where "the class loader" refers to the context class loader:</p>
112 * <ol>
113 * <li>
114 * If the system property {@link #DEFAULT_PROPERTY_NAME} + ":uri" is present,
115 * where uri is the parameter to this method, then its value is read as a class name.
116 * The method will try to create a new instance of this class by using the class loader,
117 * and returns it if it is successfully created.
118 * </li>
119 * <li>
120 * ${java.home}/lib/jaxp.properties is read and the value associated with the key being the system property above is looked for.
121 * If present, the value is processed just like above.
122 * </li>
123 * <li>
124 * The class loader is asked for service provider provider-configuration files matching <code>javax.xml.xpath.XPathFactory</code>
125 * in the resource directory META-INF/services.
126 * See the JAR File Specification for file format and parsing rules.
127 * Each potential service provider is required to implement the method:
128 * <pre>
129 * {@link #isObjectModelSupported(String objectModel)}
130 * </pre>
131 * The first service provider found in class loader order that supports the specified object model is returned.
132 * </li>
133 * <li>
134 * Platform default <code>XPathFactory</code> is located in a platform specific way.
135 * There must be a platform default XPathFactory for the W3C DOM, i.e. {@link #DEFAULT_OBJECT_MODEL_URI}.
136 * </li>
137 * </ol>
138 * <p>If everything fails, an <code>XPathFactoryConfigurationException</code> will be thrown.</p>
139 *
140 * <p>Tip for Trouble-shooting:</p>
141 * <p>See {@link java.util.Properties#load(java.io.InputStream)} for exactly how a property file is parsed.
142 * In particular, colons ':' need to be escaped in a property file, so make sure the URIs are properly escaped in it.
143 * For example:</p>
144 * <pre>
145 * http\://java.sun.com/jaxp/xpath/dom=org.acme.DomXPathFactory
146 * </pre>
147 *
148 * @param uri Identifies the underlying object model.
149 * The specification only defines the URI {@link #DEFAULT_OBJECT_MODEL_URI},
150 * <code>http://java.sun.com/jaxp/xpath/dom</code> for the W3C DOM,
151 * the org.w3c.dom package, and implementations are free to introduce other URIs for other object models.
152 *
153 * @return Instance of an <code>XPathFactory</code>.
154 *
155 * @throws XPathFactoryConfigurationException If the specified object model is unavailable.
156 * @throws NullPointerException If <code>uri</code> is <code>null</code>.
157 * @throws IllegalArgumentException If <code>uri</code> is <code>null</code>
158 * or <code>uri.length() == 0</code>.
159 */
160 public static final XPathFactory newInstance(final String uri)
161 throws XPathFactoryConfigurationException {
162
163 if (uri == null) {
164 throw new NullPointerException(
165 "XPathFactory#newInstance(String uri) cannot be called with uri == null");
166 }
167
168 if (uri.length() == 0) {
169 throw new IllegalArgumentException(
170 "XPathFactory#newInstance(String uri) cannot be called with uri == \"\"");
171 }
172
173 ClassLoader classLoader = ss.getContextClassLoader();
174
175 if (classLoader == null) {
176 //use the current class loader
177 classLoader = XPathFactory.class.getClassLoader();
178 }
179
180 XPathFactory xpathFactory = new XPathFactoryFinder(classLoader)
181 .newFactory(uri);
182
183 if (xpathFactory == null) {
184 throw new XPathFactoryConfigurationException(
185 "No XPathFactory implementation found for the object model: "
186 + uri);
187 }
188
189 return xpathFactory;
190 }
191
192 /**
193 * <p>Obtain a new instance of a <code>XPathFactory</code> from a factory class name. <code>XPathFactory</code>
194 * is returned if specified factory class supports the specified object model.
195 * This function is useful when there are multiple providers in the classpath.
196 * It gives more control to the application as it can specify which provider
197 * should be loaded.</p>
198 *
199 *
200 * <h2>Tip for Trouble-shooting</h2>
201 * <p>Setting the <code>jaxp.debug</code> system property will cause
202 * this method to print a lot of debug messages
203 * to <code>System.err</code> about what it is doing and where it is looking at.</p>
204 *
205 * <p> If you have problems try:</p>
206 * <pre>
207 * java -Djaxp.debug=1 YourProgram ....
208 * </pre>
209 *
210 * @param uri Identifies the underlying object model. The specification only defines the URI
211 * {@link #DEFAULT_OBJECT_MODEL_URI},<code>http://java.sun.com/jaxp/xpath/dom</code>
212 * for the W3C DOM, the org.w3c.dom package, and implementations are free to introduce
213 * other URIs for other object models.
214 *
215 * @param factoryClassName fully qualified factory class name that provides implementation of <code>javax.xml.xpath.XPathFactory</code>.
216 *
217 * @param classLoader <code>ClassLoader</code> used to load the factory class. If <code>null</code>
218 * current <code>Thread</code>'s context classLoader is used to load the factory class.
219 *
220 *
221 * @return New instance of a <code>XPathFactory</code>
222 *
223 * @throws XPathFactoryConfigurationException
224 * if <code>factoryClassName</code> is <code>null</code>, or
225 * the factory class cannot be loaded, instantiated
226 * or the factory class does not support the object model specified
227 * in the <code>uri</code> parameter.
228 *
229 * @throws NullPointerException If <code>uri</code> is <code>null</code>.
230 * @throws IllegalArgumentException If <code>uri</code> is <code>null</code>
231 * or <code>uri.length() == 0</code>.
232 *
233 * @see #newInstance()
234 * @see #newInstance(String uri)
235 *
236 * @since 1.6
237 */
238 public static XPathFactory newInstance(String uri,
239 String factoryClassName, ClassLoader classLoader)
240 throws XPathFactoryConfigurationException {
241 ClassLoader cl = classLoader;
242
243 if (uri == null) {
244 throw new NullPointerException(
245 "XPathFactory#newInstance(String uri) cannot be called with uri == null");
246 }
247
248 if (uri.length() == 0) {
249 throw new IllegalArgumentException(
250 "XPathFactory#newInstance(String uri) cannot be called with uri == \"\"");
251 }
252
253 if (cl == null) {
254 cl = ss.getContextClassLoader();
255 }
256
257 XPathFactory f = new XPathFactoryFinder(cl)
258 .createInstance(factoryClassName);
259
260 if (f == null) {
261 throw new XPathFactoryConfigurationException(
262 "No XPathFactory implementation found for the object model: "
263 + uri);
264 }
265 //if this factory supports the given schemalanguage return this factory else thrown exception
266 if (f.isObjectModelSupported(uri)) {
267 return f;
268 } else {
269 throw new XPathFactoryConfigurationException("Factory "
270 + factoryClassName + " doesn't support given "
271 + uri + " object model");
272 }
273
274 }
275
276 /**
277 * <p>Is specified object model supported by this <code>XPathFactory</code>?</p>
278 *
279 * @param objectModel Specifies the object model which the returned <code>XPathFactory</code> will understand.
280 *
281 * @return <code>true</code> if <code>XPathFactory</code> supports <code>objectModel</code>, else <code>false</code>.
282 *
283 * @throws NullPointerException If <code>objectModel</code> is <code>null</code>.
284 * @throws IllegalArgumentException If <code>objectModel.length() == 0</code>.
285 */
286 public abstract boolean isObjectModelSupported(String objectModel);
287
288 /**
289 * <p>Set a feature for this <code>XPathFactory</code> and
290 * <code>XPath</code>s created by this factory.</p>
291 *
292 * <p>
293 * Feature names are fully qualified {@link java.net.URI}s.
294 * Implementations may define their own features.
295 * An {@link XPathFactoryConfigurationException} is thrown if this
296 * <code>XPathFactory</code> or the <code>XPath</code>s
297 * it creates cannot support the feature.
298 * It is possible for an <code>XPathFactory</code> to expose a feature value
299 * but be unable to change its state.
300 * </p>
301 *
302 * <p>
303 * All implementations are required to support the {@link javax.xml.XMLConstants#FEATURE_SECURE_PROCESSING} feature.
304 * When the feature is <code>true</code>, any reference to an external function is an error.
305 * Under these conditions, the implementation must not call the {@link XPathFunctionResolver}
306 * and must throw an {@link XPathFunctionException}.
307 * </p>
308 *
309 * @param name Feature name.
310 * @param value Is feature state <code>true</code> or <code>false</code>.
311 *
312 * @throws XPathFactoryConfigurationException if this <code>XPathFactory</code> or the <code>XPath</code>s
313 * it creates cannot support this feature.
314 * @throws NullPointerException if <code>name</code> is <code>null</code>.
315 */
316 public abstract void setFeature(String name, boolean value)
317 throws XPathFactoryConfigurationException;
318
319 /**
320 * <p>Get the state of the named feature.</p>
321 *
322 * <p>
323 * Feature names are fully qualified {@link java.net.URI}s.
324 * Implementations may define their own features.
325 * An {@link XPathFactoryConfigurationException} is thrown if this
326 * <code>XPathFactory</code> or the <code>XPath</code>s
327 * it creates cannot support the feature.
328 * It is possible for an <code>XPathFactory</code> to expose a feature value
329 * but be unable to change its state.
330 * </p>
331 *
332 * @param name Feature name.
333 *
334 * @return State of the named feature.
335 *
336 * @throws XPathFactoryConfigurationException if this
337 * <code>XPathFactory</code> or the <code>XPath</code>s
338 * it creates cannot support this feature.
339 * @throws NullPointerException if <code>name</code> is <code>null</code>.
340 */
341 public abstract boolean getFeature(String name)
342 throws XPathFactoryConfigurationException;
343
344 /**
345 * <p>Establish a default variable resolver.</p>
346 *
347 * <p>Any <code>XPath</code> objects constructed from this factory will use
348 * the specified resolver by default.</p>
349 *
350 * <p>A <code>NullPointerException</code> is thrown if <code>resolver</code>
351 * is <code>null</code>.</p>
352 *
353 * @param resolver Variable resolver.
354 *
355 * @throws NullPointerException If <code>resolver</code> is
356 * <code>null</code>.
357 */
358 public abstract void setXPathVariableResolver(
359 XPathVariableResolver resolver);
360
361 /**
362 * <p>Establish a default function resolver.</p>
363 *
364 * <p>Any <code>XPath</code> objects constructed from this factory will
365 * use the specified resolver by default.</p>
366 *
367 * <p>A <code>NullPointerException</code> is thrown if
368 * <code>resolver</code> is <code>null</code>.</p>
369 *
370 * @param resolver XPath function resolver.
371 *
372 * @throws NullPointerException If <code>resolver</code> is
373 * <code>null</code>.
374 */
375 public abstract void setXPathFunctionResolver(
376 XPathFunctionResolver resolver);
377
378 /**
379 * <p>Return a new <code>XPath</code> using the underlying object
380 * model determined when the <code>XPathFactory</code> was instantiated.</p>
381 *
382 * @return New instance of an <code>XPath</code>.
383 */
384 public abstract XPath newXPath();
385 }
|