286N/A/*
286N/A * Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved.
286N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
286N/A *
286N/A * This code is free software; you can redistribute it and/or modify it
286N/A * under the terms of the GNU General Public License version 2 only, as
286N/A * published by the Free Software Foundation. Oracle designates this
286N/A * particular file as subject to the "Classpath" exception as provided
286N/A * by Oracle in the LICENSE file that accompanied this code.
286N/A *
286N/A * This code is distributed in the hope that it will be useful, but WITHOUT
286N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
286N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
286N/A * version 2 for more details (a copy is included in the LICENSE file that
286N/A * accompanied this code).
286N/A *
286N/A * You should have received a copy of the GNU General Public License version
286N/A * 2 along with this work; if not, write to the Free Software Foundation,
286N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
286N/A *
286N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
286N/A * or visit www.oracle.com if you need additional information or have any
286N/A * questions.
286N/A */
286N/A
286N/Apackage javax.xml.xpath;
286N/A
286N/A/**
286N/A * <p>An <code>XPathFactory</code> instance can be used to create
286N/A * {@link javax.xml.xpath.XPath} objects.</p>
286N/A *
286N/A *<p>See {@link #newInstance(String uri)} for lookup mechanism.</p>
286N/A *
286N/A * <p>The {@link XPathFactory} class is not thread-safe. In other words,
286N/A * it is the application's responsibility to ensure that at most
286N/A * one thread is using a {@link XPathFactory} object at any
286N/A * given moment. Implementations are encouraged to mark methods
286N/A * as <code>synchronized</code> to protect themselves from broken clients.
286N/A *
286N/A * <p>{@link XPathFactory} is not re-entrant. While one of the
286N/A * <code>newInstance</code> methods is being invoked, applications
286N/A * may not attempt to recursively invoke a <code>newInstance</code> method,
286N/A * even from the same thread.
286N/A *
286N/A * @author <a href="mailto:Norman.Walsh@Sun.com">Norman Walsh</a>
286N/A * @author <a href="mailto:Jeff.Suttor@Sun.com">Jeff Suttor</a>
286N/A *
286N/A * @since 1.5
286N/A */
286N/Apublic abstract class XPathFactory {
286N/A
286N/A
286N/A /**
286N/A * <p>The default property name according to the JAXP spec.</p>
286N/A */
286N/A public static final String DEFAULT_PROPERTY_NAME = "javax.xml.xpath.XPathFactory";
286N/A
286N/A /**
286N/A * <p>Default Object Model URI.</p>
286N/A */
286N/A public static final String DEFAULT_OBJECT_MODEL_URI = "http://java.sun.com/jaxp/xpath/dom";
286N/A
286N/A /**
286N/A *<p> Take care of restrictions imposed by java security model </p>
286N/A */
286N/A private static SecuritySupport ss = new SecuritySupport() ;
286N/A
286N/A /**
286N/A * <p>Protected constructor as {@link #newInstance()} or {@link #newInstance(String uri)}
286N/A * or {@link #newInstance(String uri, String factoryClassName, ClassLoader classLoader)}
286N/A * should be used to create a new instance of an <code>XPathFactory</code>.</p>
286N/A */
286N/A protected XPathFactory() {
286N/A }
286N/A
286N/A /**
286N/A * <p>Get a new <code>XPathFactory</code> instance using the default object model,
286N/A * {@link #DEFAULT_OBJECT_MODEL_URI},
286N/A * the W3C DOM.</p>
286N/A *
286N/A * <p>This method is functionally equivalent to:</p>
286N/A * <pre>
286N/A * newInstance(DEFAULT_OBJECT_MODEL_URI)
286N/A * </pre>
286N/A *
286N/A * <p>Since the implementation for the W3C DOM is always available, this method will never fail.</p>
286N/A *
286N/A * @return Instance of an <code>XPathFactory</code>.
286N/A *
286N/A * @throws RuntimeException When there is a failure in creating an
286N/A * <code>XPathFactory</code> for the default object model.
286N/A */
286N/A public static final XPathFactory newInstance() {
286N/A
286N/A try {
286N/A return newInstance(DEFAULT_OBJECT_MODEL_URI);
286N/A } catch (XPathFactoryConfigurationException xpathFactoryConfigurationException) {
286N/A throw new RuntimeException(
286N/A "XPathFactory#newInstance() failed to create an XPathFactory for the default object model: "
286N/A + DEFAULT_OBJECT_MODEL_URI
286N/A + " with the XPathFactoryConfigurationException: "
286N/A + xpathFactoryConfigurationException.toString()
286N/A );
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * <p>Get a new <code>XPathFactory</code> instance using the specified object model.</p>
286N/A *
286N/A * <p>To find a <code>XPathFactory</code> object,
286N/A * this method looks the following places in the following order where "the class loader" refers to the context class loader:</p>
286N/A * <ol>
286N/A * <li>
286N/A * If the system property {@link #DEFAULT_PROPERTY_NAME} + ":uri" is present,
286N/A * where uri is the parameter to this method, then its value is read as a class name.
286N/A * The method will try to create a new instance of this class by using the class loader,
286N/A * and returns it if it is successfully created.
286N/A * </li>
286N/A * <li>
286N/A * ${java.home}/lib/jaxp.properties is read and the value associated with the key being the system property above is looked for.
286N/A * If present, the value is processed just like above.
286N/A * </li>
286N/A * <li>
286N/A * The class loader is asked for service provider provider-configuration files matching <code>javax.xml.xpath.XPathFactory</code>
286N/A * in the resource directory META-INF/services.
286N/A * See the JAR File Specification for file format and parsing rules.
286N/A * Each potential service provider is required to implement the method:
286N/A * <pre>
286N/A * {@link #isObjectModelSupported(String objectModel)}
286N/A * </pre>
286N/A * The first service provider found in class loader order that supports the specified object model is returned.
286N/A * </li>
286N/A * <li>
286N/A * Platform default <code>XPathFactory</code> is located in a platform specific way.
286N/A * There must be a platform default XPathFactory for the W3C DOM, i.e. {@link #DEFAULT_OBJECT_MODEL_URI}.
286N/A * </li>
286N/A * </ol>
286N/A * <p>If everything fails, an <code>XPathFactoryConfigurationException</code> will be thrown.</p>
286N/A *
286N/A * <p>Tip for Trouble-shooting:</p>
286N/A * <p>See {@link java.util.Properties#load(java.io.InputStream)} for exactly how a property file is parsed.
286N/A * In particular, colons ':' need to be escaped in a property file, so make sure the URIs are properly escaped in it.
286N/A * For example:</p>
286N/A * <pre>
286N/A * http\://java.sun.com/jaxp/xpath/dom=org.acme.DomXPathFactory
286N/A * </pre>
286N/A *
286N/A * @param uri Identifies the underlying object model.
286N/A * The specification only defines the URI {@link #DEFAULT_OBJECT_MODEL_URI},
286N/A * <code>http://java.sun.com/jaxp/xpath/dom</code> for the W3C DOM,
286N/A * the org.w3c.dom package, and implementations are free to introduce other URIs for other object models.
286N/A *
286N/A * @return Instance of an <code>XPathFactory</code>.
286N/A *
286N/A * @throws XPathFactoryConfigurationException If the specified object model is unavailable.
286N/A * @throws NullPointerException If <code>uri</code> is <code>null</code>.
286N/A * @throws IllegalArgumentException If <code>uri</code> is <code>null</code>
286N/A * or <code>uri.length() == 0</code>.
286N/A */
286N/A public static final XPathFactory newInstance(final String uri)
286N/A throws XPathFactoryConfigurationException {
286N/A
286N/A if (uri == null) {
286N/A throw new NullPointerException(
286N/A "XPathFactory#newInstance(String uri) cannot be called with uri == null"
286N/A );
286N/A }
286N/A
286N/A if (uri.length() == 0) {
286N/A throw new IllegalArgumentException(
286N/A "XPathFactory#newInstance(String uri) cannot be called with uri == \"\""
286N/A );
286N/A }
286N/A
286N/A ClassLoader classLoader = ss.getContextClassLoader();
286N/A
286N/A if (classLoader == null) {
286N/A //use the current class loader
286N/A classLoader = XPathFactory.class.getClassLoader();
286N/A }
286N/A
286N/A XPathFactory xpathFactory = new XPathFactoryFinder(classLoader).newFactory(uri);
286N/A
286N/A if (xpathFactory == null) {
286N/A throw new XPathFactoryConfigurationException(
286N/A "No XPathFactory implementation found for the object model: "
286N/A + uri
286N/A );
286N/A }
286N/A
286N/A return xpathFactory;
286N/A }
286N/A
286N/A /**
286N/A * <p>Obtain a new instance of a <code>XPathFactory</code> from a factory class name. <code>XPathFactory</code>
286N/A * is returned if specified factory class supports the specified object model.
286N/A * This function is useful when there are multiple providers in the classpath.
286N/A * It gives more control to the application as it can specify which provider
286N/A * should be loaded.</p>
286N/A *
286N/A *
286N/A * <h2>Tip for Trouble-shooting</h2>
286N/A * <p>Setting the <code>jaxp.debug</code> system property will cause
286N/A * this method to print a lot of debug messages
286N/A * to <code>System.err</code> about what it is doing and where it is looking at.</p>
286N/A *
286N/A * <p> If you have problems try:</p>
286N/A * <pre>
286N/A * java -Djaxp.debug=1 YourProgram ....
286N/A * </pre>
286N/A *
286N/A * @param uri Identifies the underlying object model. The specification only defines the URI
286N/A * {@link #DEFAULT_OBJECT_MODEL_URI},<code>http://java.sun.com/jaxp/xpath/dom</code>
286N/A * for the W3C DOM, the org.w3c.dom package, and implementations are free to introduce
286N/A * other URIs for other object models.
286N/A *
286N/A * @param factoryClassName fully qualified factory class name that provides implementation of <code>javax.xml.xpath.XPathFactory</code>.
286N/A *
286N/A * @param classLoader <code>ClassLoader</code> used to load the factory class. If <code>null</code>
286N/A * current <code>Thread</code>'s context classLoader is used to load the factory class.
286N/A *
286N/A *
286N/A * @return New instance of a <code>XPathFactory</code>
286N/A *
286N/A * @throws XPathFactoryConfigurationException
286N/A * if <code>factoryClassName</code> is <code>null</code>, or
286N/A * the factory class cannot be loaded, instantiated
286N/A * or the factory class does not support the object model specified
286N/A * in the <code>uri</code> parameter.
286N/A *
286N/A * @throws NullPointerException If <code>uri</code> is <code>null</code>.
286N/A * @throws IllegalArgumentException If <code>uri</code> is <code>null</code>
286N/A * or <code>uri.length() == 0</code>.
286N/A *
286N/A * @see #newInstance()
286N/A * @see #newInstance(String uri)
286N/A *
286N/A * @since 1.6
286N/A */
286N/A public static XPathFactory newInstance(String uri, String factoryClassName, ClassLoader classLoader)
286N/A throws XPathFactoryConfigurationException{
286N/A ClassLoader cl = classLoader;
286N/A
286N/A if (uri == null) {
286N/A throw new NullPointerException(
286N/A "XPathFactory#newInstance(String uri) cannot be called with uri == null"
286N/A );
286N/A }
286N/A
286N/A if (uri.length() == 0) {
286N/A throw new IllegalArgumentException(
286N/A "XPathFactory#newInstance(String uri) cannot be called with uri == \"\""
286N/A );
286N/A }
286N/A
286N/A if (cl == null) {
286N/A cl = ss.getContextClassLoader();
286N/A }
286N/A
286N/A XPathFactory f = new XPathFactoryFinder(cl).createInstance(factoryClassName);
286N/A
286N/A if (f == null) {
286N/A throw new XPathFactoryConfigurationException(
286N/A "No XPathFactory implementation found for the object model: "
286N/A + uri
286N/A );
286N/A }
286N/A //if this factory supports the given schemalanguage return this factory else thrown exception
286N/A if(f.isObjectModelSupported(uri)){
286N/A return f;
286N/A }else{
286N/A throw new XPathFactoryConfigurationException("Factory " + factoryClassName + " doesn't support given " + uri + " object model");
286N/A }
286N/A
286N/A }
286N/A
286N/A /**
286N/A * <p>Is specified object model supported by this <code>XPathFactory</code>?</p>
286N/A *
286N/A * @param objectModel Specifies the object model which the returned <code>XPathFactory</code> will understand.
286N/A *
286N/A * @return <code>true</code> if <code>XPathFactory</code> supports <code>objectModel</code>, else <code>false</code>.
286N/A *
286N/A * @throws NullPointerException If <code>objectModel</code> is <code>null</code>.
286N/A * @throws IllegalArgumentException If <code>objectModel.length() == 0</code>.
286N/A */
286N/A public abstract boolean isObjectModelSupported(String objectModel);
286N/A
286N/A /**
286N/A * <p>Set a feature for this <code>XPathFactory</code> and
286N/A * <code>XPath</code>s created by this factory.</p>
286N/A *
286N/A * <p>
286N/A * Feature names are fully qualified {@link java.net.URI}s.
286N/A * Implementations may define their own features.
286N/A * An {@link XPathFactoryConfigurationException} is thrown if this
286N/A * <code>XPathFactory</code> or the <code>XPath</code>s
286N/A * it creates cannot support the feature.
286N/A * It is possible for an <code>XPathFactory</code> to expose a feature value
286N/A * but be unable to change its state.
286N/A * </p>
286N/A *
286N/A * <p>
286N/A * All implementations are required to support the {@link javax.xml.XMLConstants#FEATURE_SECURE_PROCESSING} feature.
286N/A * When the feature is <code>true</code>, any reference to an external function is an error.
286N/A * Under these conditions, the implementation must not call the {@link XPathFunctionResolver}
286N/A * and must throw an {@link XPathFunctionException}.
286N/A * </p>
286N/A *
286N/A * @param name Feature name.
286N/A * @param value Is feature state <code>true</code> or <code>false</code>.
286N/A *
286N/A * @throws XPathFactoryConfigurationException if this <code>XPathFactory</code> or the <code>XPath</code>s
286N/A * it creates cannot support this feature.
286N/A * @throws NullPointerException if <code>name</code> is <code>null</code>.
286N/A */
286N/A public abstract void setFeature(String name, boolean value)
286N/A throws XPathFactoryConfigurationException;
286N/A
286N/A /**
286N/A * <p>Get the state of the named feature.</p>
286N/A *
286N/A * <p>
286N/A * Feature names are fully qualified {@link java.net.URI}s.
286N/A * Implementations may define their own features.
286N/A * An {@link XPathFactoryConfigurationException} is thrown if this
286N/A * <code>XPathFactory</code> or the <code>XPath</code>s
286N/A * it creates cannot support the feature.
286N/A * It is possible for an <code>XPathFactory</code> to expose a feature value
286N/A * but be unable to change its state.
286N/A * </p>
286N/A *
286N/A * @param name Feature name.
286N/A *
286N/A * @return State of the named feature.
286N/A *
286N/A * @throws XPathFactoryConfigurationException if this
286N/A * <code>XPathFactory</code> or the <code>XPath</code>s
286N/A * it creates cannot support this feature.
286N/A * @throws NullPointerException if <code>name</code> is <code>null</code>.
286N/A */
286N/A public abstract boolean getFeature(String name)
286N/A throws XPathFactoryConfigurationException;
286N/A
286N/A /**
286N/A * <p>Establish a default variable resolver.</p>
286N/A *
286N/A * <p>Any <code>XPath</code> objects constructed from this factory will use
286N/A * the specified resolver by default.</p>
286N/A *
286N/A * <p>A <code>NullPointerException</code> is thrown if <code>resolver</code>
286N/A * is <code>null</code>.</p>
286N/A *
286N/A * @param resolver Variable resolver.
286N/A *
286N/A * @throws NullPointerException If <code>resolver</code> is
286N/A * <code>null</code>.
286N/A */
286N/A public abstract void setXPathVariableResolver(XPathVariableResolver resolver);
286N/A
286N/A /**
286N/A * <p>Establish a default function resolver.</p>
286N/A *
286N/A * <p>Any <code>XPath</code> objects constructed from this factory will
286N/A * use the specified resolver by default.</p>
286N/A *
286N/A * <p>A <code>NullPointerException</code> is thrown if
286N/A * <code>resolver</code> is <code>null</code>.</p>
286N/A *
286N/A * @param resolver XPath function resolver.
286N/A *
286N/A * @throws NullPointerException If <code>resolver</code> is
286N/A * <code>null</code>.
286N/A */
286N/A public abstract void setXPathFunctionResolver(XPathFunctionResolver resolver);
286N/A
286N/A /**
286N/A * <p>Return a new <code>XPath</code> using the underlying object
286N/A * model determined when the <code>XPathFactory</code> was instantiated.</p>
286N/A *
286N/A * @return New instance of an <code>XPath</code>.
286N/A */
286N/A public abstract XPath newXPath();
286N/A}