Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxp/src/javax/xml/xpath/XPathFactory.java
32285 views
/*1* Copyright (c) 2003, 2013, 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;2627/**28* <p>An <code>XPathFactory</code> instance can be used to create29* {@link javax.xml.xpath.XPath} objects.</p>30*31*<p>See {@link #newInstance(String uri)} for lookup mechanism.</p>32*33* <p>The {@link XPathFactory} class is not thread-safe. In other words,34* it is the application's responsibility to ensure that at most35* one thread is using a {@link XPathFactory} object at any36* given moment. Implementations are encouraged to mark methods37* as <code>synchronized</code> to protect themselves from broken clients.38*39* <p>{@link XPathFactory} is not re-entrant. While one of the40* <code>newInstance</code> methods is being invoked, applications41* may not attempt to recursively invoke a <code>newInstance</code> method,42* even from the same thread.43*44* @author <a href="mailto:[email protected]">Norman Walsh</a>45* @author <a href="mailto:[email protected]">Jeff Suttor</a>46*47* @since 1.548*/49public abstract class XPathFactory {505152/**53* <p>The default property name according to the JAXP spec.</p>54*/55public static final String DEFAULT_PROPERTY_NAME = "javax.xml.xpath.XPathFactory";5657/**58* <p>Default Object Model URI.</p>59*/60public static final String DEFAULT_OBJECT_MODEL_URI = "http://java.sun.com/jaxp/xpath/dom";6162/**63*<p> Take care of restrictions imposed by java security model </p>64*/65private static SecuritySupport ss = new SecuritySupport() ;6667/**68* <p>Protected constructor as {@link #newInstance()} or {@link #newInstance(String uri)}69* or {@link #newInstance(String uri, String factoryClassName, ClassLoader classLoader)}70* should be used to create a new instance of an <code>XPathFactory</code>.</p>71*/72protected XPathFactory() {73}7475/**76* <p>Get a new <code>XPathFactory</code> instance using the default object model,77* {@link #DEFAULT_OBJECT_MODEL_URI},78* the W3C DOM.</p>79*80* <p>This method is functionally equivalent to:</p>81* <pre>82* newInstance(DEFAULT_OBJECT_MODEL_URI)83* </pre>84*85* <p>Since the implementation for the W3C DOM is always available, this method will never fail.</p>86*87* @return Instance of an <code>XPathFactory</code>.88*89* @throws RuntimeException When there is a failure in creating an90* <code>XPathFactory</code> for the default object model.91*/92public static XPathFactory newInstance() {9394try {95return newInstance(DEFAULT_OBJECT_MODEL_URI);96} catch (XPathFactoryConfigurationException xpathFactoryConfigurationException) {97throw new RuntimeException(98"XPathFactory#newInstance() failed to create an XPathFactory for the default object model: "99+ DEFAULT_OBJECT_MODEL_URI100+ " with the XPathFactoryConfigurationException: "101+ xpathFactoryConfigurationException.toString()102);103}104}105106/**107* <p>Get a new <code>XPathFactory</code> instance using the specified object model.</p>108*109* <p>To find a <code>XPathFactory</code> object,110* this method looks the following places in the following order where "the class loader" refers to the context class loader:</p>111* <ol>112* <li>113* If the system property {@link #DEFAULT_PROPERTY_NAME} + ":uri" is present,114* where uri is the parameter to this method, then its value is read as a class name.115* The method will try to create a new instance of this class by using the class loader,116* and returns it if it is successfully created.117* </li>118* <li>119* ${java.home}/lib/jaxp.properties is read and the value associated with the key being the system property above is looked for.120* If present, the value is processed just like above.121* </li>122* <li>123* Use the service-provider loading facilities, defined by the124* {@link java.util.ServiceLoader} class, to attempt to locate and load an125* implementation of the service using the {@linkplain126* java.util.ServiceLoader#load(java.lang.Class) default loading mechanism}:127* the service-provider loading facility will use the {@linkplain128* java.lang.Thread#getContextClassLoader() current thread's context class loader}129* to attempt to load the service. If the context class130* loader is null, the {@linkplain131* ClassLoader#getSystemClassLoader() system class loader} will be used.132* <br>133* Each potential service provider is required to implement the method134* {@link #isObjectModelSupported(String objectModel)}.135* The first service provider found that supports the specified object136* model is returned.137* <br>138* In case of {@link java.util.ServiceConfigurationError} an139* {@link XPathFactoryConfigurationException} will be thrown.140* </li>141* <li>142* Platform default <code>XPathFactory</code> is located in a platform specific way.143* There must be a platform default XPathFactory for the W3C DOM, i.e. {@link #DEFAULT_OBJECT_MODEL_URI}.144* </li>145* </ol>146* <p>If everything fails, an <code>XPathFactoryConfigurationException</code> will be thrown.</p>147*148* <p>Tip for Trouble-shooting:</p>149* <p>See {@link java.util.Properties#load(java.io.InputStream)} for exactly how a property file is parsed.150* In particular, colons ':' need to be escaped in a property file, so make sure the URIs are properly escaped in it.151* For example:</p>152* <pre>153* http\://java.sun.com/jaxp/xpath/dom=org.acme.DomXPathFactory154* </pre>155*156* @param uri Identifies the underlying object model.157* The specification only defines the URI {@link #DEFAULT_OBJECT_MODEL_URI},158* <code>http://java.sun.com/jaxp/xpath/dom</code> for the W3C DOM,159* the org.w3c.dom package, and implementations are free to introduce other URIs for other object models.160*161* @return Instance of an <code>XPathFactory</code>.162*163* @throws XPathFactoryConfigurationException If the specified object model164* is unavailable, or if there is a configuration error.165* @throws NullPointerException If <code>uri</code> is <code>null</code>.166* @throws IllegalArgumentException If <code>uri</code> is <code>null</code>167* or <code>uri.length() == 0</code>.168*/169public static XPathFactory newInstance(final String uri)170throws XPathFactoryConfigurationException {171172if (uri == null) {173throw new NullPointerException(174"XPathFactory#newInstance(String uri) cannot be called with uri == null");175}176177if (uri.length() == 0) {178throw new IllegalArgumentException(179"XPathFactory#newInstance(String uri) cannot be called with uri == \"\"");180}181182ClassLoader classLoader = ss.getContextClassLoader();183184if (classLoader == null) {185//use the current class loader186classLoader = XPathFactory.class.getClassLoader();187}188189XPathFactory xpathFactory = new XPathFactoryFinder(classLoader).newFactory(uri);190191if (xpathFactory == null) {192throw new XPathFactoryConfigurationException(193"No XPathFactory implementation found for the object model: "194+ uri);195}196197return xpathFactory;198}199200/**201* <p>Obtain a new instance of a <code>XPathFactory</code> from a factory class name. <code>XPathFactory</code>202* is returned if specified factory class supports the specified object model.203* This function is useful when there are multiple providers in the classpath.204* It gives more control to the application as it can specify which provider205* should be loaded.</p>206*207*208* <h2>Tip for Trouble-shooting</h2>209* <p>Setting the <code>jaxp.debug</code> system property will cause210* this method to print a lot of debug messages211* to <code>System.err</code> about what it is doing and where it is looking at.</p>212*213* <p> If you have problems try:</p>214* <pre>215* java -Djaxp.debug=1 YourProgram ....216* </pre>217*218* @param uri Identifies the underlying object model. The specification only defines the URI219* {@link #DEFAULT_OBJECT_MODEL_URI},<code>http://java.sun.com/jaxp/xpath/dom</code>220* for the W3C DOM, the org.w3c.dom package, and implementations are free to introduce221* other URIs for other object models.222*223* @param factoryClassName fully qualified factory class name that provides implementation of <code>javax.xml.xpath.XPathFactory</code>.224*225* @param classLoader <code>ClassLoader</code> used to load the factory class. If <code>null</code>226* current <code>Thread</code>'s context classLoader is used to load the factory class.227*228*229* @return New instance of a <code>XPathFactory</code>230*231* @throws XPathFactoryConfigurationException232* if <code>factoryClassName</code> is <code>null</code>, or233* the factory class cannot be loaded, instantiated234* or the factory class does not support the object model specified235* in the <code>uri</code> parameter.236*237* @throws NullPointerException If <code>uri</code> is <code>null</code>.238* @throws IllegalArgumentException If <code>uri</code> is <code>null</code>239* or <code>uri.length() == 0</code>.240*241* @see #newInstance()242* @see #newInstance(String uri)243*244* @since 1.6245*/246public static XPathFactory newInstance(String uri, String factoryClassName, ClassLoader classLoader)247throws XPathFactoryConfigurationException{248ClassLoader cl = classLoader;249250if (uri == null) {251throw new NullPointerException(252"XPathFactory#newInstance(String uri) cannot be called with uri == null");253}254255if (uri.length() == 0) {256throw new IllegalArgumentException(257"XPathFactory#newInstance(String uri) cannot be called with uri == \"\"");258}259260if (cl == null) {261cl = ss.getContextClassLoader();262}263264XPathFactory f = new XPathFactoryFinder(cl).createInstance(factoryClassName);265266if (f == null) {267throw new XPathFactoryConfigurationException(268"No XPathFactory implementation found for the object model: "269+ uri);270}271//if this factory supports the given schemalanguage return this factory else thrown exception272if (f.isObjectModelSupported(uri)) {273return f;274} else {275throw new XPathFactoryConfigurationException("Factory "276+ factoryClassName + " doesn't support given " + uri277+ " object model");278}279280}281282/**283* <p>Is specified object model supported by this <code>XPathFactory</code>?</p>284*285* @param objectModel Specifies the object model which the returned <code>XPathFactory</code> will understand.286*287* @return <code>true</code> if <code>XPathFactory</code> supports <code>objectModel</code>, else <code>false</code>.288*289* @throws NullPointerException If <code>objectModel</code> is <code>null</code>.290* @throws IllegalArgumentException If <code>objectModel.length() == 0</code>.291*/292public abstract boolean isObjectModelSupported(String objectModel);293294/**295* <p>Set a feature for this <code>XPathFactory</code> and296* <code>XPath</code>s created by this factory.</p>297*298* <p>299* Feature names are fully qualified {@link java.net.URI}s.300* Implementations may define their own features.301* An {@link XPathFactoryConfigurationException} is thrown if this302* <code>XPathFactory</code> or the <code>XPath</code>s303* it creates cannot support the feature.304* It is possible for an <code>XPathFactory</code> to expose a feature value305* but be unable to change its state.306* </p>307*308* <p>309* All implementations are required to support the {@link javax.xml.XMLConstants#FEATURE_SECURE_PROCESSING} feature.310* When the feature is <code>true</code>, any reference to an external function is an error.311* Under these conditions, the implementation must not call the {@link XPathFunctionResolver}312* and must throw an {@link XPathFunctionException}.313* </p>314*315* @param name Feature name.316* @param value Is feature state <code>true</code> or <code>false</code>.317*318* @throws XPathFactoryConfigurationException if this <code>XPathFactory</code> or the <code>XPath</code>s319* it creates cannot support this feature.320* @throws NullPointerException if <code>name</code> is <code>null</code>.321*/322public abstract void setFeature(String name, boolean value)323throws XPathFactoryConfigurationException;324325/**326* <p>Get the state of the named feature.</p>327*328* <p>329* Feature names are fully qualified {@link java.net.URI}s.330* Implementations may define their own features.331* An {@link XPathFactoryConfigurationException} is thrown if this332* <code>XPathFactory</code> or the <code>XPath</code>s333* it creates cannot support the feature.334* It is possible for an <code>XPathFactory</code> to expose a feature value335* but be unable to change its state.336* </p>337*338* @param name Feature name.339*340* @return State of the named feature.341*342* @throws XPathFactoryConfigurationException if this343* <code>XPathFactory</code> or the <code>XPath</code>s344* it creates cannot support this feature.345* @throws NullPointerException if <code>name</code> is <code>null</code>.346*/347public abstract boolean getFeature(String name)348throws XPathFactoryConfigurationException;349350/**351* <p>Establish a default variable resolver.</p>352*353* <p>Any <code>XPath</code> objects constructed from this factory will use354* the specified resolver by default.</p>355*356* <p>A <code>NullPointerException</code> is thrown if <code>resolver</code>357* is <code>null</code>.</p>358*359* @param resolver Variable resolver.360*361* @throws NullPointerException If <code>resolver</code> is362* <code>null</code>.363*/364public abstract void setXPathVariableResolver(XPathVariableResolver resolver);365366/**367* <p>Establish a default function resolver.</p>368*369* <p>Any <code>XPath</code> objects constructed from this factory will370* use the specified resolver by default.</p>371*372* <p>A <code>NullPointerException</code> is thrown if373* <code>resolver</code> is <code>null</code>.</p>374*375* @param resolver XPath function resolver.376*377* @throws NullPointerException If <code>resolver</code> is378* <code>null</code>.379*/380public abstract void setXPathFunctionResolver(XPathFunctionResolver resolver);381382/**383* <p>Return a new <code>XPath</code> using the underlying object384* model determined when the <code>XPathFactory</code> was instantiated.</p>385*386* @return New instance of an <code>XPath</code>.387*/388public abstract XPath newXPath();389}390391392