Path: blob/master/src/java.xml/share/classes/javax/xml/xpath/XPathFactory.java
40948 views
/*1* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.xml.xpath;2627import com.sun.org.apache.xpath.internal.jaxp.XPathFactoryImpl;28import jdk.xml.internal.SecuritySupport;2930/**31* <p>An {@code XPathFactory} instance can be used to create32* {@link javax.xml.xpath.XPath} objects.</p>33*34*<p>See {@link #newInstance(String uri)} for lookup mechanism.</p>35*36* <p>The {@link XPathFactory} class is not thread-safe. In other words,37* it is the application's responsibility to ensure that at most38* one thread is using a {@link XPathFactory} object at any39* given moment. Implementations are encouraged to mark methods40* as <code>synchronized</code> to protect themselves from broken clients.41*42* <p>{@link XPathFactory} is not re-entrant. While one of the43* <code>newInstance</code> methods is being invoked, applications44* may not attempt to recursively invoke a <code>newInstance</code> method,45* even from the same thread.46*47* @author Norman Walsh48* @author Jeff Suttor49*50* @since 1.551*/52public abstract class XPathFactory {535455/**56* <p>The default property name according to the JAXP spec.</p>57*/58public static final String DEFAULT_PROPERTY_NAME = "javax.xml.xpath.XPathFactory";5960/**61* <p>Default Object Model URI.</p>62*/63public static final String DEFAULT_OBJECT_MODEL_URI = "http://java.sun.com/jaxp/xpath/dom";6465/**66* <p>Protected constructor as {@link #newInstance()} or {@link #newInstance(String uri)}67* or {@link #newInstance(String uri, String factoryClassName, ClassLoader classLoader)}68* should be used to create a new instance of an {@code XPathFactory}.</p>69*/70protected XPathFactory() {71}7273/**74* Creates a new instance of the {@code XPathFactory} builtin75* system-default implementation.76*77* @implSpec The {@code XPathFactory} builtin78* system-default implementation is only required to support the79* {@link #DEFAULT_OBJECT_MODEL_URI default object model}, the80* {@linkplain org.w3c.dom W3C DOM}, but may support additional81* object models.82*83* @return A new instance of the {@code XPathFactory} builtin84* system-default implementation.85*86* @since 987*/88public static XPathFactory newDefaultInstance() {89return new XPathFactoryImpl();90}9192/**93* <p>Get a new {@code XPathFactory} instance using the default object model,94* {@link #DEFAULT_OBJECT_MODEL_URI},95* the W3C DOM.</p>96*97* <p>This method is functionally equivalent to:</p>98* <pre>99* newInstance(DEFAULT_OBJECT_MODEL_URI)100* </pre>101*102* <p>Since the implementation for the W3C DOM is always available, this method will never fail.</p>103*104* @return Instance of an {@code XPathFactory}.105*106* @throws RuntimeException When there is a failure in creating an107* {@code XPathFactory} for the default object model.108*/109public static XPathFactory newInstance() {110111try {112return newInstance(DEFAULT_OBJECT_MODEL_URI);113} catch (XPathFactoryConfigurationException e) {114throw new RuntimeException(115"XPathFactory#newInstance() failed to create an XPathFactory for the default object model: "116+ DEFAULT_OBJECT_MODEL_URI117+ " with the XPathFactoryConfigurationException: "118+ e.getMessage(), e119);120}121}122123/**124* Obtains a new {@code XPathFactory} instance using the specified object model.125* This method uses the126* <a href="../../../module-summary.html#LookupMechanism">JAXP Lookup Mechanism</a>127* to determine and load the {@code XPathFactory} implementation that supports128* the specified object model.129*130* <p>Tip for Trouble-shooting:131* <p>See {@link java.util.Properties#load(java.io.InputStream)} for exactly how a property file is parsed.132* In particular, colons ':' need to be escaped in a property file, so make sure the URIs are properly escaped in it.133* For example:134* <pre>135* http\://java.sun.com/jaxp/xpath/dom=org.acme.DomXPathFactory136* </pre>137*138* @param uri Identifies the underlying object model.139* The specification only defines the URI {@link #DEFAULT_OBJECT_MODEL_URI},140* <code>http://java.sun.com/jaxp/xpath/dom</code> for the W3C DOM,141* the org.w3c.dom package, and implementations are free to introduce other URIs for other object models.142*143* @return Instance of an {@code XPathFactory}.144*145* @throws XPathFactoryConfigurationException If the specified object model146* is unavailable, or if there is a configuration error.147* @throws NullPointerException If <code>uri</code> is <code>null</code>.148* @throws IllegalArgumentException If <code>uri</code> is <code>null</code>149* or <code>uri.length() == 0</code>.150*/151public static XPathFactory newInstance(final String uri)152throws XPathFactoryConfigurationException {153154if (uri == null) {155throw new NullPointerException(156"XPathFactory#newInstance(String uri) cannot be called with uri == null");157}158159if (uri.length() == 0) {160throw new IllegalArgumentException(161"XPathFactory#newInstance(String uri) cannot be called with uri == \"\"");162}163164ClassLoader classLoader = SecuritySupport.getContextClassLoader();165166if (classLoader == null) {167//use the current class loader168classLoader = XPathFactory.class.getClassLoader();169}170171XPathFactory xpathFactory = new XPathFactoryFinder(classLoader).newFactory(uri);172173if (xpathFactory == null) {174throw new XPathFactoryConfigurationException(175"No XPathFactory implementation found for the object model: "176+ uri);177}178179return xpathFactory;180}181182/**183* <p>Obtain a new instance of a {@code XPathFactory} from a factory class name. {@code XPathFactory}184* is returned if specified factory class supports the specified object model.185* This function is useful when there are multiple providers in the classpath.186* It gives more control to the application as it can specify which provider187* should be loaded.</p>188*189*190* <h4>Tip for Trouble-shooting</h4>191* <p>Setting the <code>jaxp.debug</code> system property will cause192* this method to print a lot of debug messages193* to <code>System.err</code> about what it is doing and where it is looking at.</p>194*195* <p> If you have problems try:</p>196* <pre>197* java -Djaxp.debug=1 YourProgram ....198* </pre>199*200* @param uri Identifies the underlying object model. The specification only defines the URI201* {@link #DEFAULT_OBJECT_MODEL_URI},<code>http://java.sun.com/jaxp/xpath/dom</code>202* for the W3C DOM, the org.w3c.dom package, and implementations are free to introduce203* other URIs for other object models.204*205* @param factoryClassName fully qualified factory class name that provides implementation of <code>javax.xml.xpath.XPathFactory</code>.206*207* @param classLoader <code>ClassLoader</code> used to load the factory class. If <code>null</code>208* current <code>Thread</code>'s context classLoader is used to load the factory class.209*210*211* @return New instance of a {@code XPathFactory}212*213* @throws XPathFactoryConfigurationException214* if <code>factoryClassName</code> is <code>null</code>, or215* the factory class cannot be loaded, instantiated216* or the factory class does not support the object model specified217* in the <code>uri</code> parameter.218*219* @throws NullPointerException If <code>uri</code> is <code>null</code>.220* @throws IllegalArgumentException If <code>uri</code> is <code>null</code>221* or <code>uri.length() == 0</code>.222*223* @see #newInstance()224* @see #newInstance(String uri)225*226* @since 1.6227*/228public static XPathFactory newInstance(String uri, String factoryClassName, ClassLoader classLoader)229throws XPathFactoryConfigurationException{230ClassLoader cl = classLoader;231232if (uri == null) {233throw new NullPointerException(234"XPathFactory#newInstance(String uri) cannot be called with uri == null");235}236237if (uri.length() == 0) {238throw new IllegalArgumentException(239"XPathFactory#newInstance(String uri) cannot be called with uri == \"\"");240}241242if (cl == null) {243cl = SecuritySupport.getContextClassLoader();244}245246XPathFactory f = new XPathFactoryFinder(cl).createInstance(factoryClassName);247248if (f == null) {249throw new XPathFactoryConfigurationException(250"No XPathFactory implementation found for the object model: "251+ uri);252}253//if this factory supports the given schemalanguage return this factory else thrown exception254if (f.isObjectModelSupported(uri)) {255return f;256} else {257throw new XPathFactoryConfigurationException("Factory "258+ factoryClassName + " doesn't support given " + uri259+ " object model");260}261262}263264/**265* <p>Is specified object model supported by this {@code XPathFactory}?</p>266*267* @param objectModel Specifies the object model which the returned {@code XPathFactory} will understand.268*269* @return <code>true</code> if {@code XPathFactory} supports <code>objectModel</code>, else <code>false</code>.270*271* @throws NullPointerException If <code>objectModel</code> is <code>null</code>.272* @throws IllegalArgumentException If <code>objectModel.length() == 0</code>.273*/274public abstract boolean isObjectModelSupported(String objectModel);275276/**277* <p>Set a feature for this {@code XPathFactory} and278* <code>XPath</code>s created by this factory.</p>279*280* <p>281* Feature names are fully qualified {@link java.net.URI}s.282* Implementations may define their own features.283* An {@link XPathFactoryConfigurationException} is thrown if this284* {@code XPathFactory} or the <code>XPath</code>s285* it creates cannot support the feature.286* It is possible for an {@code XPathFactory} to expose a feature value287* but be unable to change its state.288* </p>289*290* <p>291* All implementations are required to support the {@link javax.xml.XMLConstants#FEATURE_SECURE_PROCESSING} feature.292* When the feature is <code>true</code>, any reference to an external function is an error.293* Under these conditions, the implementation must not call the {@link XPathFunctionResolver}294* and must throw an {@link XPathFunctionException}.295* </p>296*297* @param name Feature name.298* @param value Is feature state <code>true</code> or <code>false</code>.299*300* @throws XPathFactoryConfigurationException if this {@code XPathFactory} or the <code>XPath</code>s301* it creates cannot support this feature.302* @throws NullPointerException if <code>name</code> is <code>null</code>.303*/304public abstract void setFeature(String name, boolean value)305throws XPathFactoryConfigurationException;306307/**308* <p>Get the state of the named feature.</p>309*310* <p>311* Feature names are fully qualified {@link java.net.URI}s.312* Implementations may define their own features.313* An {@link XPathFactoryConfigurationException} is thrown if this314* {@code XPathFactory} or the <code>XPath</code>s315* it creates cannot support the feature.316* It is possible for an {@code XPathFactory} to expose a feature value317* but be unable to change its state.318* </p>319*320* @param name Feature name.321*322* @return State of the named feature.323*324* @throws XPathFactoryConfigurationException if this325* {@code XPathFactory} or the <code>XPath</code>s326* it creates cannot support this feature.327* @throws NullPointerException if <code>name</code> is <code>null</code>.328*/329public abstract boolean getFeature(String name)330throws XPathFactoryConfigurationException;331332/**333* <p>Establish a default variable resolver.</p>334*335* <p>Any <code>XPath</code> objects constructed from this factory will use336* the specified resolver by default.</p>337*338* <p>A <code>NullPointerException</code> is thrown if <code>resolver</code>339* is <code>null</code>.</p>340*341* @param resolver Variable resolver.342*343* @throws NullPointerException If <code>resolver</code> is344* <code>null</code>.345*/346public abstract void setXPathVariableResolver(XPathVariableResolver resolver);347348/**349* <p>Establish a default function resolver.</p>350*351* <p>Any <code>XPath</code> objects constructed from this factory will352* use the specified resolver by default.</p>353*354* <p>A <code>NullPointerException</code> is thrown if355* <code>resolver</code> is <code>null</code>.</p>356*357* @param resolver XPath function resolver.358*359* @throws NullPointerException If <code>resolver</code> is360* <code>null</code>.361*/362public abstract void setXPathFunctionResolver(XPathFunctionResolver resolver);363364/**365* <p>Return a new <code>XPath</code> using the underlying object366* model determined when the {@code XPathFactory} was instantiated.</p>367*368* @return New instance of an <code>XPath</code>.369*/370public abstract XPath newXPath();371}372373374