1 /*
   2  * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.xml.xpath;
  27 
  28 /**
  29  * <p>An <code>XPathFactory</code> instance can be used to create
  30  * {@link javax.xml.xpath.XPath} objects.</p>
  31  *
  32  *<p>See {@link #newInstance(String uri)} for lookup mechanism.</p>
  33  *
  34  * <p>The {@link XPathFactory} class is not thread-safe. In other words,
  35  * it is the application's responsibility to ensure that at most
  36  * one thread is using a {@link XPathFactory} object at any
  37  * given moment. Implementations are encouraged to mark methods
  38  * as <code>synchronized</code> to protect themselves from broken clients.
  39  *
  40  * <p>{@link XPathFactory} is not re-entrant. While one of the
  41  * <code>newInstance</code> methods is being invoked, applications
  42  * may not attempt to recursively invoke a <code>newInstance</code> method,
  43  * even from the same thread.
  44  *
  45  * @author  <a href="mailto:Norman.Walsh@Sun.com">Norman Walsh</a>
  46  * @author  <a href="mailto:Jeff.Suttor@Sun.com">Jeff Suttor</a>
  47  *
  48  * @since 1.5
  49  */
  50 public abstract class XPathFactory {
  51 
  52 
  53     /**
  54      * <p>The default property name according to the JAXP spec.</p>
  55      */
  56     public static final String DEFAULT_PROPERTY_NAME = "javax.xml.xpath.XPathFactory";
  57 
  58     /**
  59      * <p>Default Object Model URI.</p>
  60      */
  61     public static final String DEFAULT_OBJECT_MODEL_URI = "http://java.sun.com/jaxp/xpath/dom";
  62 
  63     /**
  64      *<p> Take care of restrictions imposed by java security model </p>
  65      */
  66     private static SecuritySupport ss = new SecuritySupport() ;
  67 
  68     /**
  69      * <p>Protected constructor as {@link #newInstance()} or {@link #newInstance(String uri)}
  70      * or {@link #newInstance(String uri, String factoryClassName, ClassLoader classLoader)}
  71      * should be used to create a new instance of an <code>XPathFactory</code>.</p>
  72      */
  73     protected XPathFactory() {
  74     }
  75 
  76     /**
  77      * <p>Get a new <code>XPathFactory</code> instance using the default object model,
  78      * {@link #DEFAULT_OBJECT_MODEL_URI},
  79      * the W3C DOM.</p>
  80      *
  81      * <p>This method is functionally equivalent to:</p>
  82      * <pre>
  83      *   newInstance(DEFAULT_OBJECT_MODEL_URI)
  84      * </pre>
  85      *
  86      * <p>Since the implementation for the W3C DOM is always available, this method will never fail.</p>
  87      *
  88      * @return Instance of an <code>XPathFactory</code>.
  89      *
  90      * @throws RuntimeException When there is a failure in creating an
  91      *   <code>XPathFactory</code> for the default object model.
  92      */
  93     public static XPathFactory newInstance() {
  94 
  95         try {
  96                 return newInstance(DEFAULT_OBJECT_MODEL_URI);
  97         } catch (XPathFactoryConfigurationException xpathFactoryConfigurationException) {
  98                 throw new RuntimeException(
  99                         "XPathFactory#newInstance() failed to create an XPathFactory for the default object model: "
 100                         + DEFAULT_OBJECT_MODEL_URI
 101                         + " with the XPathFactoryConfigurationException: "
 102                         + xpathFactoryConfigurationException.toString()
 103                 );
 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}/conf/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     *     Use the service-provider loading facilities, defined by the
 125     *     {@link java.util.ServiceLoader} class, to attempt to locate and load an
 126     *     implementation of the service using the {@linkplain
 127     *     java.util.ServiceLoader#load(java.lang.Class) default loading mechanism}:
 128     *     the service-provider loading facility will use the {@linkplain
 129     *     java.lang.Thread#getContextClassLoader() current thread's context class loader}
 130     *     to attempt to load the service. If the context class
 131     *     loader is null, the {@linkplain
 132     *     ClassLoader#getSystemClassLoader() system class loader} will be used.
 133     *     <br>
 134     *     Each potential service provider is required to implement the method
 135     *     {@link #isObjectModelSupported(String objectModel)}.
 136     *     The first service provider found that supports the specified object
 137     *     model is returned.
 138     *     <br>
 139     *     In case of {@link java.util.ServiceConfigurationError} an
 140     *     {@link XPathFactoryConfigurationException} will be thrown.
 141     *   </li>
 142     *   <li>
 143     *     Platform default <code>XPathFactory</code> is located in a platform specific way.
 144     *     There must be a platform default XPathFactory for the W3C DOM, i.e. {@link #DEFAULT_OBJECT_MODEL_URI}.
 145     *   </li>
 146     * </ol>
 147     * <p>If everything fails, an <code>XPathFactoryConfigurationException</code> will be thrown.</p>
 148     *
 149     * <p>Tip for Trouble-shooting:</p>
 150     * <p>See {@link java.util.Properties#load(java.io.InputStream)} for exactly how a property file is parsed.
 151     * In particular, colons ':' need to be escaped in a property file, so make sure the URIs are properly escaped in it.
 152     * For example:</p>
 153     * <pre>
 154     *   http\://java.sun.com/jaxp/xpath/dom=org.acme.DomXPathFactory
 155     * </pre>
 156     *
 157     * @param uri Identifies the underlying object model.
 158     *   The specification only defines the URI {@link #DEFAULT_OBJECT_MODEL_URI},
 159     *   <code>http://java.sun.com/jaxp/xpath/dom</code> for the W3C DOM,
 160     *   the org.w3c.dom package, and implementations are free to introduce other URIs for other object models.
 161     *
 162     * @return Instance of an <code>XPathFactory</code>.
 163     *
 164     * @throws XPathFactoryConfigurationException If the specified object model
 165     *      is unavailable, or if there is a configuration error.
 166     * @throws NullPointerException If <code>uri</code> is <code>null</code>.
 167     * @throws IllegalArgumentException If <code>uri</code> is <code>null</code>
 168     *   or <code>uri.length() == 0</code>.
 169     */
 170     public static XPathFactory newInstance(final String uri)
 171         throws XPathFactoryConfigurationException {
 172 
 173         if (uri == null) {
 174             throw new NullPointerException(
 175                     "XPathFactory#newInstance(String uri) cannot be called with uri == null");
 176         }
 177 
 178         if (uri.length() == 0) {
 179             throw new IllegalArgumentException(
 180                     "XPathFactory#newInstance(String uri) cannot be called with uri == \"\"");
 181         }
 182 
 183         ClassLoader classLoader = ss.getContextClassLoader();
 184 
 185         if (classLoader == null) {
 186             //use the current class loader
 187             classLoader = XPathFactory.class.getClassLoader();
 188         }
 189 
 190         XPathFactory xpathFactory = new XPathFactoryFinder(classLoader).newFactory(uri);
 191 
 192         if (xpathFactory == null) {
 193             throw new XPathFactoryConfigurationException(
 194                     "No XPathFactory implementation found for the object model: "
 195                     + uri);
 196         }
 197 
 198         return xpathFactory;
 199     }
 200 
 201     /**
 202      * <p>Obtain a new instance of a <code>XPathFactory</code> from a factory class name. <code>XPathFactory</code>
 203      * is returned if specified factory class supports the specified object model.
 204      * This function is useful when there are multiple providers in the classpath.
 205      * It gives more control to the application as it can specify which provider
 206      * should be loaded.</p>
 207      *
 208      *
 209      * <h2>Tip for Trouble-shooting</h2>
 210      * <p>Setting the <code>jaxp.debug</code> system property will cause
 211      * this method to print a lot of debug messages
 212      * to <code>System.err</code> about what it is doing and where it is looking at.</p>
 213      *
 214      * <p> If you have problems try:</p>
 215      * <pre>
 216      * java -Djaxp.debug=1 YourProgram ....
 217      * </pre>
 218      *
 219      * @param uri         Identifies the underlying object model. The specification only defines the URI
 220      *                    {@link #DEFAULT_OBJECT_MODEL_URI},<code>http://java.sun.com/jaxp/xpath/dom</code>
 221      *                    for the W3C DOM, the org.w3c.dom package, and implementations are free to introduce
 222      *                    other URIs for other object models.
 223      *
 224      * @param factoryClassName fully qualified factory class name that provides implementation of <code>javax.xml.xpath.XPathFactory</code>.
 225      *
 226      * @param classLoader <code>ClassLoader</code> used to load the factory class. If <code>null</code>
 227      *                     current <code>Thread</code>'s context classLoader is used to load the factory class.
 228      *
 229      *
 230      * @return New instance of a <code>XPathFactory</code>
 231      *
 232      * @throws XPathFactoryConfigurationException
 233      *                   if <code>factoryClassName</code> is <code>null</code>, or
 234      *                   the factory class cannot be loaded, instantiated
 235      *                   or the factory class does not support the object model specified
 236      *                   in the <code>uri</code> parameter.
 237      *
 238      * @throws NullPointerException If <code>uri</code> is <code>null</code>.
 239      * @throws IllegalArgumentException If <code>uri</code> is <code>null</code>
 240      *          or <code>uri.length() == 0</code>.
 241      *
 242      * @see #newInstance()
 243      * @see #newInstance(String uri)
 244      *
 245      * @since 1.6
 246      */
 247     public static XPathFactory newInstance(String uri, String factoryClassName, ClassLoader classLoader)
 248         throws XPathFactoryConfigurationException{
 249         ClassLoader cl = classLoader;
 250 
 251         if (uri == null) {
 252             throw new NullPointerException(
 253                     "XPathFactory#newInstance(String uri) cannot be called with uri == null");
 254         }
 255 
 256         if (uri.length() == 0) {
 257             throw new IllegalArgumentException(
 258                     "XPathFactory#newInstance(String uri) cannot be called with uri == \"\"");
 259         }
 260 
 261         if (cl == null) {
 262             cl = ss.getContextClassLoader();
 263         }
 264 
 265         XPathFactory f = new XPathFactoryFinder(cl).createInstance(factoryClassName);
 266 
 267         if (f == null) {
 268             throw new XPathFactoryConfigurationException(
 269                     "No XPathFactory implementation found for the object model: "
 270                     + uri);
 271         }
 272         //if this factory supports the given schemalanguage return this factory else thrown exception
 273         if (f.isObjectModelSupported(uri)) {
 274             return f;
 275         } else {
 276             throw new XPathFactoryConfigurationException("Factory "
 277                     + factoryClassName + " doesn't support given " + uri
 278                     + " object model");
 279         }
 280 
 281     }
 282 
 283     /**
 284      * <p>Is specified object model supported by this <code>XPathFactory</code>?</p>
 285      *
 286      * @param objectModel Specifies the object model which the returned <code>XPathFactory</code> will understand.
 287      *
 288      * @return <code>true</code> if <code>XPathFactory</code> supports <code>objectModel</code>, else <code>false</code>.
 289      *
 290      * @throws NullPointerException If <code>objectModel</code> is <code>null</code>.
 291      * @throws IllegalArgumentException If <code>objectModel.length() == 0</code>.
 292      */
 293     public abstract boolean isObjectModelSupported(String objectModel);
 294 
 295     /**
 296      * <p>Set a feature for this <code>XPathFactory</code> and
 297      * <code>XPath</code>s created by this factory.</p>
 298      *
 299      * <p>
 300      * Feature names are fully qualified {@link java.net.URI}s.
 301      * Implementations may define their own features.
 302      * An {@link XPathFactoryConfigurationException} is thrown if this
 303      * <code>XPathFactory</code> or the <code>XPath</code>s
 304      * it creates cannot support the feature.
 305      * It is possible for an <code>XPathFactory</code> to expose a feature value
 306      * but be unable to change its state.
 307      * </p>
 308      *
 309      * <p>
 310      * All implementations are required to support the {@link javax.xml.XMLConstants#FEATURE_SECURE_PROCESSING} feature.
 311      * When the feature is <code>true</code>, any reference to  an external function is an error.
 312      * Under these conditions, the implementation must not call the {@link XPathFunctionResolver}
 313      * and must throw an {@link XPathFunctionException}.
 314      * </p>
 315      *
 316      * @param name Feature name.
 317      * @param value Is feature state <code>true</code> or <code>false</code>.
 318      *
 319      * @throws XPathFactoryConfigurationException if this <code>XPathFactory</code> or the <code>XPath</code>s
 320      *   it creates cannot support this feature.
 321      * @throws NullPointerException if <code>name</code> is <code>null</code>.
 322      */
 323     public abstract void setFeature(String name, boolean value)
 324             throws XPathFactoryConfigurationException;
 325 
 326     /**
 327      * <p>Get the state of the named feature.</p>
 328      *
 329      * <p>
 330      * Feature names are fully qualified {@link java.net.URI}s.
 331      * Implementations may define their own features.
 332      * An {@link XPathFactoryConfigurationException} is thrown if this
 333      * <code>XPathFactory</code> or the <code>XPath</code>s
 334      * it creates cannot support the feature.
 335      * It is possible for an <code>XPathFactory</code> to expose a feature value
 336      * but be unable to change its state.
 337      * </p>
 338      *
 339      * @param name Feature name.
 340      *
 341      * @return State of the named feature.
 342      *
 343      * @throws XPathFactoryConfigurationException if this
 344      *   <code>XPathFactory</code> or the <code>XPath</code>s
 345      *   it creates cannot support this feature.
 346      * @throws NullPointerException if <code>name</code> is <code>null</code>.
 347      */
 348     public abstract boolean getFeature(String name)
 349             throws XPathFactoryConfigurationException;
 350 
 351     /**
 352      * <p>Establish a default variable resolver.</p>
 353      *
 354      * <p>Any <code>XPath</code> objects constructed from this factory will use
 355      * the specified resolver by default.</p>
 356      *
 357      * <p>A <code>NullPointerException</code> is thrown if <code>resolver</code>
 358      * is <code>null</code>.</p>
 359      *
 360      * @param resolver Variable resolver.
 361      *
 362      * @throws NullPointerException If <code>resolver</code> is
 363      *   <code>null</code>.
 364      */
 365     public abstract void setXPathVariableResolver(XPathVariableResolver resolver);
 366 
 367     /**
 368      * <p>Establish a default function resolver.</p>
 369      *
 370      * <p>Any <code>XPath</code> objects constructed from this factory will
 371      * use the specified resolver by default.</p>
 372      *
 373      * <p>A <code>NullPointerException</code> is thrown if
 374      * <code>resolver</code> is <code>null</code>.</p>
 375      *
 376      * @param resolver XPath function resolver.
 377      *
 378      * @throws NullPointerException If <code>resolver</code> is
 379      *   <code>null</code>.
 380      */
 381     public abstract void setXPathFunctionResolver(XPathFunctionResolver resolver);
 382 
 383     /**
 384     * <p>Return a new <code>XPath</code> using the underlying object
 385     * model determined when the <code>XPathFactory</code> was instantiated.</p>
 386     *
 387     * @return New instance of an <code>XPath</code>.
 388     */
 389     public abstract XPath newXPath();
 390 }