XPathFactory.java revision 1038:ccad0993fc67
1114402Sru/*
2114402Sru * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
3114402Sru * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4114402Sru *
5114402Sru * This code is free software; you can redistribute it and/or modify it
6114402Sru * under the terms of the GNU General Public License version 2 only, as
7114402Sru * published by the Free Software Foundation.  Oracle designates this
8114402Sru * particular file as subject to the "Classpath" exception as provided
9114402Sru * by Oracle in the LICENSE file that accompanied this code.
10114402Sru *
11114402Sru * This code is distributed in the hope that it will be useful, but WITHOUT
12114402Sru * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13114402Sru * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14114402Sru * version 2 for more details (a copy is included in the LICENSE file that
15114402Sru * accompanied this code).
16114402Sru *
17114402Sru * You should have received a copy of the GNU General Public License version
18114402Sru * 2 along with this work; if not, write to the Free Software Foundation,
19114402Sru * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20151497Sru *
21114402Sru * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22114402Sru * or visit www.oracle.com if you need additional information or have any
23114402Sru * questions.
24114402Sru */
25114402Sru
26114402Srupackage javax.xml.xpath;
27114402Sru
28114402Sruimport com.sun.org.apache.xpath.internal.jaxp.XPathFactoryImpl;
29114402Sru
30114402Sru/**
31114402Sru * <p>An {@code XPathFactory} instance can be used to create
32114402Sru * {@link javax.xml.xpath.XPath} objects.</p>
33114402Sru *
34114402Sru *<p>See {@link #newInstance(String uri)} for lookup mechanism.</p>
35114402Sru *
36114402Sru * <p>The {@link XPathFactory} class is not thread-safe. In other words,
37114402Sru * it is the application's responsibility to ensure that at most
38114402Sru * one thread is using a {@link XPathFactory} object at any
39114402Sru * given moment. Implementations are encouraged to mark methods
40114402Sru * as <code>synchronized</code> to protect themselves from broken clients.
41114402Sru *
42114402Sru * <p>{@link XPathFactory} is not re-entrant. While one of the
43114402Sru * <code>newInstance</code> methods is being invoked, applications
44114402Sru * may not attempt to recursively invoke a <code>newInstance</code> method,
45114402Sru * even from the same thread.
46114402Sru *
47114402Sru * @author  <a href="mailto:Norman.Walsh@Sun.com">Norman Walsh</a>
48114402Sru * @author  <a href="mailto:Jeff.Suttor@Sun.com">Jeff Suttor</a>
49114402Sru *
50114402Sru * @since 1.5
51114402Sru */
52114402Srupublic abstract class XPathFactory {
53114402Sru
54114402Sru
55114402Sru    /**
56114402Sru     * <p>The default property name according to the JAXP spec.</p>
57114402Sru     */
58114402Sru    public static final String DEFAULT_PROPERTY_NAME = "javax.xml.xpath.XPathFactory";
59114402Sru
60114402Sru    /**
61114402Sru     * <p>Default Object Model URI.</p>
62114402Sru     */
63114402Sru    public static final String DEFAULT_OBJECT_MODEL_URI = "http://java.sun.com/jaxp/xpath/dom";
64114402Sru
65114402Sru    /**
66114402Sru     *<p> Take care of restrictions imposed by java security model </p>
67114402Sru     */
68114402Sru    private static SecuritySupport ss = new SecuritySupport() ;
69114402Sru
70114402Sru    /**
71114402Sru     * <p>Protected constructor as {@link #newInstance()} or {@link #newInstance(String uri)}
72114402Sru     * or {@link #newInstance(String uri, String factoryClassName, ClassLoader classLoader)}
73114402Sru     * should be used to create a new instance of an {@code XPathFactory}.</p>
74114402Sru     */
75114402Sru    protected XPathFactory() {
76114402Sru    }
77114402Sru
78114402Sru    /**
79114402Sru     * Creates a new instance of the {@code XPathFactory} builtin
80114402Sru     * system-default implementation.
81114402Sru     *
82114402Sru     * @implSpec The {@code XPathFactory} builtin
83114402Sru     * system-default implementation is only required to support the
84114402Sru     * {@link #DEFAULT_OBJECT_MODEL_URI default object model}, the
85114402Sru     * {@linkplain org.w3c.dom W3C DOM}, but may support additional
86114402Sru     * object models.
87114402Sru     *
88114402Sru     * @return A new instance of the {@code XPathFactory} builtin
89114402Sru     *         system-default implementation.
90114402Sru     *
91114402Sru     * @since 9
92114402Sru     */
93114402Sru    public static XPathFactory newDefaultInstance() {
94114402Sru        return XPathFactoryImpl.newXPathFactoryNoServiceLoader();
95114402Sru    }
96114402Sru
97114402Sru    /**
98114402Sru     * <p>Get a new {@code XPathFactory} instance using the default object model,
99114402Sru     * {@link #DEFAULT_OBJECT_MODEL_URI},
100114402Sru     * the W3C DOM.</p>
101114402Sru     *
102114402Sru     * <p>This method is functionally equivalent to:</p>
103114402Sru     * <pre>
104114402Sru     *   newInstance(DEFAULT_OBJECT_MODEL_URI)
105114402Sru     * </pre>
106114402Sru     *
107114402Sru     * <p>Since the implementation for the W3C DOM is always available, this method will never fail.</p>
108114402Sru     *
109114402Sru     * @return Instance of an {@code XPathFactory}.
110114402Sru     *
111114402Sru     * @throws RuntimeException When there is a failure in creating an
112114402Sru     *   {@code XPathFactory} for the default object model.
113114402Sru     */
114114402Sru    public static XPathFactory newInstance() {
115114402Sru
116114402Sru        try {
117114402Sru            return newInstance(DEFAULT_OBJECT_MODEL_URI);
118114402Sru        } catch (XPathFactoryConfigurationException e) {
119114402Sru            throw new RuntimeException(
120114402Sru                    "XPathFactory#newInstance() failed to create an XPathFactory for the default object model: "
121114402Sru                    + DEFAULT_OBJECT_MODEL_URI
122114402Sru                    + " with the XPathFactoryConfigurationException: "
123114402Sru                    + e.getMessage(), e
124114402Sru            );
125114402Sru        }
126114402Sru    }
127114402Sru
128114402Sru    /**
129114402Sru    * <p>Get a new {@code XPathFactory} instance using the specified object model.</p>
130114402Sru    *
131114402Sru    * <p>To find a {@code XPathFactory} object,
132114402Sru    * this method looks the following places in the following order where "the class loader" refers to the context class loader:</p>
133114402Sru    * <ol>
134114402Sru    *   <li>
135114402Sru    *     <p>
136114402Sru    *     If the system property {@link #DEFAULT_PROPERTY_NAME} + ":uri" is present,
137114402Sru    *     where uri is the parameter to this method, then its value is read as a class name.
138114402Sru    *     The method will try to create a new instance of this class by using the class loader,
139114402Sru    *     and returns it if it is successfully created.
140114402Sru    *   </li>
141114402Sru    *   <li>
142114402Sru    *     <p>
143114402Sru    *     Use the configuration file "jaxp.properties". The file is in standard
144114402Sru    *     {@link java.util.Properties} format and typically located in the
145114402Sru    *     conf directory of the Java installation. It contains the fully qualified
146114402Sru    *     name of the implementation class with the key being the system property
147114402Sru    *     defined above.
148114402Sru    *     <p>
149114402Sru    *     The jaxp.properties file is read only once by the JAXP implementation
150114402Sru    *     and its values are then cached for future use.  If the file does not exist
151114402Sru    *     when the first attempt is made to read from it, no further attempts are
152114402Sru    *     made to check for its existence.  It is not possible to change the value
153114402Sru    *     of any property in jaxp.properties after it has been read for the first time.
154114402Sru    *   </li>
155114402Sru    *   <li>
156114402Sru    *     <p>
157114402Sru    *     Use the service-provider loading facility, defined by the
158114402Sru    *     {@link java.util.ServiceLoader} class, to attempt to locate and load an
159114402Sru    *     implementation of the service using the {@linkplain
160114402Sru    *     java.util.ServiceLoader#load(java.lang.Class) default loading mechanism}:
161114402Sru    *     the service-provider loading facility will use the {@linkplain
162114402Sru    *     java.lang.Thread#getContextClassLoader() current thread's context class loader}
163114402Sru    *     to attempt to load the service. If the context class
164114402Sru    *     loader is null, the {@linkplain
165114402Sru    *     ClassLoader#getSystemClassLoader() system class loader} will be used.
166114402Sru    *     <br>
167114402Sru    *     Each potential service provider is required to implement the method
168114402Sru    *     {@link #isObjectModelSupported(String objectModel)}.
169114402Sru    *     The first service provider found that supports the specified object
170114402Sru    *     model is returned.
171114402Sru    *     <br>
172114402Sru    *     In case of {@link java.util.ServiceConfigurationError} an
173114402Sru    *     {@link XPathFactoryConfigurationException} will be thrown.
174114402Sru    *   </li>
175114402Sru    *   <li>
176114402Sru    *     <p>
177114402Sru    *     Platform default {@code XPathFactory} is located in a platform
178114402Sru    *     specific way.
179114402Sru    *     There must be a {@linkplain #newDefaultInstance() platform default}
180114402Sru    *     {@code XPathFactory} for the W3C DOM, i.e.
181114402Sru    *     {@link #DEFAULT_OBJECT_MODEL_URI}.
182114402Sru    *   </li>
183114402Sru    * </ol>
184114402Sru    * <p>If everything fails, an {@code XPathFactoryConfigurationException} will be thrown.
185114402Sru    *
186114402Sru    * <p>Tip for Trouble-shooting:
187114402Sru    * <p>See {@link java.util.Properties#load(java.io.InputStream)} for exactly how a property file is parsed.
188114402Sru    * In particular, colons ':' need to be escaped in a property file, so make sure the URIs are properly escaped in it.
189114402Sru    * For example:
190114402Sru    * <pre>
191114402Sru    *   http\://java.sun.com/jaxp/xpath/dom=org.acme.DomXPathFactory
192114402Sru    * </pre>
193114402Sru    *
194114402Sru    * @param uri Identifies the underlying object model.
195114402Sru    *   The specification only defines the URI {@link #DEFAULT_OBJECT_MODEL_URI},
196114402Sru    *   <code>http://java.sun.com/jaxp/xpath/dom</code> for the W3C DOM,
197114402Sru    *   the org.w3c.dom package, and implementations are free to introduce other URIs for other object models.
198114402Sru    *
199114402Sru    * @return Instance of an {@code XPathFactory}.
200114402Sru    *
201114402Sru    * @throws XPathFactoryConfigurationException If the specified object model
202114402Sru    *      is unavailable, or if there is a configuration error.
203114402Sru    * @throws NullPointerException If <code>uri</code> is <code>null</code>.
204114402Sru    * @throws IllegalArgumentException If <code>uri</code> is <code>null</code>
205114402Sru    *   or <code>uri.length() == 0</code>.
206114402Sru    */
207114402Sru    public static XPathFactory newInstance(final String uri)
208114402Sru        throws XPathFactoryConfigurationException {
209114402Sru
210114402Sru        if (uri == null) {
211114402Sru            throw new NullPointerException(
212114402Sru                    "XPathFactory#newInstance(String uri) cannot be called with uri == null");
213114402Sru        }
214114402Sru
215114402Sru        if (uri.length() == 0) {
216114402Sru            throw new IllegalArgumentException(
217114402Sru                    "XPathFactory#newInstance(String uri) cannot be called with uri == \"\"");
218114402Sru        }
219114402Sru
220114402Sru        ClassLoader classLoader = ss.getContextClassLoader();
221114402Sru
222114402Sru        if (classLoader == null) {
223114402Sru            //use the current class loader
224114402Sru            classLoader = XPathFactory.class.getClassLoader();
225114402Sru        }
226114402Sru
227114402Sru        XPathFactory xpathFactory = new XPathFactoryFinder(classLoader).newFactory(uri);
228114402Sru
229114402Sru        if (xpathFactory == null) {
230114402Sru            throw new XPathFactoryConfigurationException(
231114402Sru                    "No XPathFactory implementation found for the object model: "
232114402Sru                    + uri);
233114402Sru        }
234114402Sru
235114402Sru        return xpathFactory;
236114402Sru    }
237114402Sru
238114402Sru    /**
239114402Sru     * <p>Obtain a new instance of a {@code XPathFactory} from a factory class name. {@code XPathFactory}
240114402Sru     * is returned if specified factory class supports the specified object model.
241114402Sru     * This function is useful when there are multiple providers in the classpath.
242114402Sru     * It gives more control to the application as it can specify which provider
243114402Sru     * should be loaded.</p>
244114402Sru     *
245114402Sru     *
246114402Sru     * <h2>Tip for Trouble-shooting</h2>
247114402Sru     * <p>Setting the <code>jaxp.debug</code> system property will cause
248114402Sru     * this method to print a lot of debug messages
249114402Sru     * to <code>System.err</code> about what it is doing and where it is looking at.</p>
250114402Sru     *
251114402Sru     * <p> If you have problems try:</p>
252114402Sru     * <pre>
253114402Sru     * java -Djaxp.debug=1 YourProgram ....
254114402Sru     * </pre>
255114402Sru     *
256114402Sru     * @param uri         Identifies the underlying object model. The specification only defines the URI
257114402Sru     *                    {@link #DEFAULT_OBJECT_MODEL_URI},<code>http://java.sun.com/jaxp/xpath/dom</code>
258114402Sru     *                    for the W3C DOM, the org.w3c.dom package, and implementations are free to introduce
259114402Sru     *                    other URIs for other object models.
260114402Sru     *
261114402Sru     * @param factoryClassName fully qualified factory class name that provides implementation of <code>javax.xml.xpath.XPathFactory</code>.
262114402Sru     *
263114402Sru     * @param classLoader <code>ClassLoader</code> used to load the factory class. If <code>null</code>
264114402Sru     *                     current <code>Thread</code>'s context classLoader is used to load the factory class.
265114402Sru     *
266114402Sru     *
267114402Sru     * @return New instance of a {@code XPathFactory}
268114402Sru     *
269114402Sru     * @throws XPathFactoryConfigurationException
270114402Sru     *                   if <code>factoryClassName</code> is <code>null</code>, or
271114402Sru     *                   the factory class cannot be loaded, instantiated
272114402Sru     *                   or the factory class does not support the object model specified
273114402Sru     *                   in the <code>uri</code> parameter.
274114402Sru     *
275114402Sru     * @throws NullPointerException If <code>uri</code> is <code>null</code>.
276114402Sru     * @throws IllegalArgumentException If <code>uri</code> is <code>null</code>
277114402Sru     *          or <code>uri.length() == 0</code>.
278114402Sru     *
279114402Sru     * @see #newInstance()
280114402Sru     * @see #newInstance(String uri)
281114402Sru     *
282114402Sru     * @since 1.6
283114402Sru     */
284114402Sru    public static XPathFactory newInstance(String uri, String factoryClassName, ClassLoader classLoader)
285114402Sru        throws XPathFactoryConfigurationException{
286114402Sru        ClassLoader cl = classLoader;
287114402Sru
288114402Sru        if (uri == null) {
289114402Sru            throw new NullPointerException(
290114402Sru                    "XPathFactory#newInstance(String uri) cannot be called with uri == null");
291114402Sru        }
292114402Sru
293114402Sru        if (uri.length() == 0) {
294114402Sru            throw new IllegalArgumentException(
295114402Sru                    "XPathFactory#newInstance(String uri) cannot be called with uri == \"\"");
296114402Sru        }
297114402Sru
298114402Sru        if (cl == null) {
299114402Sru            cl = ss.getContextClassLoader();
300114402Sru        }
301114402Sru
302114402Sru        XPathFactory f = new XPathFactoryFinder(cl).createInstance(factoryClassName);
303114402Sru
304114402Sru        if (f == null) {
305114402Sru            throw new XPathFactoryConfigurationException(
306114402Sru                    "No XPathFactory implementation found for the object model: "
307114402Sru                    + uri);
308114402Sru        }
309114402Sru        //if this factory supports the given schemalanguage return this factory else thrown exception
310114402Sru        if (f.isObjectModelSupported(uri)) {
311114402Sru            return f;
312114402Sru        } else {
313114402Sru            throw new XPathFactoryConfigurationException("Factory "
314114402Sru                    + factoryClassName + " doesn't support given " + uri
315114402Sru                    + " object model");
316114402Sru        }
317114402Sru
318114402Sru    }
319114402Sru
320114402Sru    /**
321114402Sru     * <p>Is specified object model supported by this {@code XPathFactory}?</p>
322114402Sru     *
323114402Sru     * @param objectModel Specifies the object model which the returned {@code XPathFactory} will understand.
324114402Sru     *
325114402Sru     * @return <code>true</code> if {@code XPathFactory} supports <code>objectModel</code>, else <code>false</code>.
326114402Sru     *
327114402Sru     * @throws NullPointerException If <code>objectModel</code> is <code>null</code>.
328114402Sru     * @throws IllegalArgumentException If <code>objectModel.length() == 0</code>.
329114402Sru     */
330114402Sru    public abstract boolean isObjectModelSupported(String objectModel);
331114402Sru
332114402Sru    /**
333114402Sru     * <p>Set a feature for this {@code XPathFactory} and
334114402Sru     * <code>XPath</code>s created by this factory.</p>
335114402Sru     *
336114402Sru     * <p>
337114402Sru     * Feature names are fully qualified {@link java.net.URI}s.
338114402Sru     * Implementations may define their own features.
339114402Sru     * An {@link XPathFactoryConfigurationException} is thrown if this
340114402Sru     * {@code XPathFactory} or the <code>XPath</code>s
341114402Sru     * it creates cannot support the feature.
342114402Sru     * It is possible for an {@code XPathFactory} to expose a feature value
343114402Sru     * but be unable to change its state.
344114402Sru     * </p>
345114402Sru     *
346114402Sru     * <p>
347114402Sru     * All implementations are required to support the {@link javax.xml.XMLConstants#FEATURE_SECURE_PROCESSING} feature.
348114402Sru     * When the feature is <code>true</code>, any reference to  an external function is an error.
349114402Sru     * Under these conditions, the implementation must not call the {@link XPathFunctionResolver}
350114402Sru     * and must throw an {@link XPathFunctionException}.
351114402Sru     * </p>
352114402Sru     *
353114402Sru     * @param name Feature name.
354114402Sru     * @param value Is feature state <code>true</code> or <code>false</code>.
355114402Sru     *
356114402Sru     * @throws XPathFactoryConfigurationException if this {@code XPathFactory} or the <code>XPath</code>s
357114402Sru     *   it creates cannot support this feature.
358114402Sru     * @throws NullPointerException if <code>name</code> is <code>null</code>.
359114402Sru     */
360114402Sru    public abstract void setFeature(String name, boolean value)
361114402Sru            throws XPathFactoryConfigurationException;
362114402Sru
363114402Sru    /**
364114402Sru     * <p>Get the state of the named feature.</p>
365114402Sru     *
366114402Sru     * <p>
367114402Sru     * Feature names are fully qualified {@link java.net.URI}s.
368114402Sru     * Implementations may define their own features.
369114402Sru     * An {@link XPathFactoryConfigurationException} is thrown if this
370114402Sru     * {@code XPathFactory} or the <code>XPath</code>s
371114402Sru     * it creates cannot support the feature.
372114402Sru     * It is possible for an {@code XPathFactory} to expose a feature value
373114402Sru     * but be unable to change its state.
374114402Sru     * </p>
375114402Sru     *
376114402Sru     * @param name Feature name.
377114402Sru     *
378114402Sru     * @return State of the named feature.
379114402Sru     *
380114402Sru     * @throws XPathFactoryConfigurationException if this
381114402Sru     *   {@code XPathFactory} or the <code>XPath</code>s
382114402Sru     *   it creates cannot support this feature.
383114402Sru     * @throws NullPointerException if <code>name</code> is <code>null</code>.
384114402Sru     */
385114402Sru    public abstract boolean getFeature(String name)
386114402Sru            throws XPathFactoryConfigurationException;
387114402Sru
388114402Sru    /**
389114402Sru     * <p>Establish a default variable resolver.</p>
390114402Sru     *
391114402Sru     * <p>Any <code>XPath</code> objects constructed from this factory will use
392114402Sru     * the specified resolver by default.</p>
393114402Sru     *
394114402Sru     * <p>A <code>NullPointerException</code> is thrown if <code>resolver</code>
395114402Sru     * is <code>null</code>.</p>
396114402Sru     *
397114402Sru     * @param resolver Variable resolver.
398114402Sru     *
399114402Sru     * @throws NullPointerException If <code>resolver</code> is
400114402Sru     *   <code>null</code>.
401114402Sru     */
402114402Sru    public abstract void setXPathVariableResolver(XPathVariableResolver resolver);
403114402Sru
404114402Sru    /**
405114402Sru     * <p>Establish a default function resolver.</p>
406114402Sru     *
407114402Sru     * <p>Any <code>XPath</code> objects constructed from this factory will
408114402Sru     * use the specified resolver by default.</p>
409114402Sru     *
410114402Sru     * <p>A <code>NullPointerException</code> is thrown if
411114402Sru     * <code>resolver</code> is <code>null</code>.</p>
412114402Sru     *
413114402Sru     * @param resolver XPath function resolver.
414114402Sru     *
415114402Sru     * @throws NullPointerException If <code>resolver</code> is
416114402Sru     *   <code>null</code>.
417114402Sru     */
418114402Sru    public abstract void setXPathFunctionResolver(XPathFunctionResolver resolver);
419114402Sru
420114402Sru    /**
421114402Sru    * <p>Return a new <code>XPath</code> using the underlying object
422114402Sru    * model determined when the {@code XPathFactory} was instantiated.</p>
423114402Sru    *
424114402Sru    * @return New instance of an <code>XPath</code>.
425114402Sru    */
426114402Sru    public abstract XPath newXPath();
427114402Sru}
428114402Sru