1 /*
   2  * Copyright (c) 2003, 2006, 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}/lib/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.
 127     *     <br>
 128     *     Each potential service provider is required to implement the method
 129     *     {@link #isObjectModelSupported(String objectModel)}.
 130     *     The first service provider found that supports the specified object
 131     *     model is returned.
 132     *     <br>
 133     *     In case of {@link java.util.ServiceConfigurationError} an
 134     *     {@link XPathFactoryConfigurationException} will be thrown.
 135     *   </li>
 136     *   <li>
 137     *     Platform default <code>XPathFactory</code> is located in a platform specific way.
 138     *     There must be a platform default XPathFactory for the W3C DOM, i.e. {@link #DEFAULT_OBJECT_MODEL_URI}.
 139     *   </li>
 140     * </ol>
 141     * <p>If everything fails, an <code>XPathFactoryConfigurationException</code> will be thrown.</p>
 142     *
 143     * <p>Tip for Trouble-shooting:</p>
 144     * <p>See {@link java.util.Properties#load(java.io.InputStream)} for exactly how a property file is parsed.
 145     * In particular, colons ':' need to be escaped in a property file, so make sure the URIs are properly escaped in it.
 146     * For example:</p>
 147     * <pre>
 148     *   http\://java.sun.com/jaxp/xpath/dom=org.acme.DomXPathFactory
 149     * </pre>
 150     *
 151     * @param uri Identifies the underlying object model.
 152     *   The specification only defines the URI {@link #DEFAULT_OBJECT_MODEL_URI},
 153     *   <code>http://java.sun.com/jaxp/xpath/dom</code> for the W3C DOM,
 154     *   the org.w3c.dom package, and implementations are free to introduce other URIs for other object models.
 155     *
 156     * @return Instance of an <code>XPathFactory</code>.
 157     *
 158     * @throws XPathFactoryConfigurationException If the specified object model
 159     *      is unavailable, or if there is a configuration error.
 160     * @throws NullPointerException If <code>uri</code> is <code>null</code>.
 161     * @throws IllegalArgumentException If <code>uri</code> is <code>null</code>
 162     *   or <code>uri.length() == 0</code>.
 163     */
 164     public static XPathFactory newInstance(final String uri)
 165         throws XPathFactoryConfigurationException {
 166 
 167         if (uri == null) {
 168             throw new NullPointerException(
 169                     "XPathFactory#newInstance(String uri) cannot be called with uri == null");
 170         }
 171 
 172         if (uri.length() == 0) {
 173             throw new IllegalArgumentException(
 174                     "XPathFactory#newInstance(String uri) cannot be called with uri == \"\"");
 175         }
 176 
 177         ClassLoader classLoader = ss.getContextClassLoader();
 178 
 179         if (classLoader == null) {
 180             //use the current class loader
 181             classLoader = XPathFactory.class.getClassLoader();
 182         }
 183 
 184         XPathFactory xpathFactory = new XPathFactoryFinder(classLoader).newFactory(uri);
 185 
 186         if (xpathFactory == null) {
 187             throw new XPathFactoryConfigurationException(
 188                     "No XPathFactory implementation found for the object model: "
 189                     + uri);
 190         }
 191 
 192         return xpathFactory;
 193     }
 194 
 195     /**
 196      * <p>Obtain a new instance of a <code>XPathFactory</code> from a factory class name. <code>XPathFactory</code>
 197      * is returned if specified factory class supports the specified object model.
 198      * This function is useful when there are multiple providers in the classpath.
 199      * It gives more control to the application as it can specify which provider
 200      * should be loaded.</p>
 201      *
 202      *
 203      * <h2>Tip for Trouble-shooting</h2>
 204      * <p>Setting the <code>jaxp.debug</code> system property will cause
 205      * this method to print a lot of debug messages
 206      * to <code>System.err</code> about what it is doing and where it is looking at.</p>
 207      *
 208      * <p> If you have problems try:</p>
 209      * <pre>
 210      * java -Djaxp.debug=1 YourProgram ....
 211      * </pre>
 212      *
 213      * @param uri         Identifies the underlying object model. The specification only defines the URI
 214      *                    {@link #DEFAULT_OBJECT_MODEL_URI},<code>http://java.sun.com/jaxp/xpath/dom</code>
 215      *                    for the W3C DOM, the org.w3c.dom package, and implementations are free to introduce
 216      *                    other URIs for other object models.
 217      *
 218      * @param factoryClassName fully qualified factory class name that provides implementation of <code>javax.xml.xpath.XPathFactory</code>.
 219      *
 220      * @param classLoader <code>ClassLoader</code> used to load the factory class. If <code>null</code>
 221      *                     current <code>Thread</code>'s context classLoader is used to load the factory class.
 222      *
 223      *
 224      * @return New instance of a <code>XPathFactory</code>
 225      *
 226      * @throws XPathFactoryConfigurationException
 227      *                   if <code>factoryClassName</code> is <code>null</code>, or
 228      *                   the factory class cannot be loaded, instantiated
 229      *                   or the factory class does not support the object model specified
 230      *                   in the <code>uri</code> parameter.
 231      *
 232      * @throws NullPointerException If <code>uri</code> is <code>null</code>.
 233      * @throws IllegalArgumentException If <code>uri</code> is <code>null</code>
 234      *          or <code>uri.length() == 0</code>.
 235      *
 236      * @see #newInstance()
 237      * @see #newInstance(String uri)
 238      *
 239      * @since 1.6
 240      */
 241     public static XPathFactory newInstance(String uri, String factoryClassName, ClassLoader classLoader)
 242         throws XPathFactoryConfigurationException{
 243         ClassLoader cl = classLoader;
 244 
 245         if (uri == null) {
 246             throw new NullPointerException(
 247                     "XPathFactory#newInstance(String uri) cannot be called with uri == null");
 248         }
 249 
 250         if (uri.length() == 0) {
 251             throw new IllegalArgumentException(
 252                     "XPathFactory#newInstance(String uri) cannot be called with uri == \"\"");
 253         }
 254 
 255         if (cl == null) {
 256             cl = ss.getContextClassLoader();
 257         }
 258 
 259         XPathFactory f = new XPathFactoryFinder(cl).createInstance(factoryClassName);
 260 
 261         if (f == null) {
 262             throw new XPathFactoryConfigurationException(
 263                     "No XPathFactory implementation found for the object model: "
 264                     + uri);
 265         }
 266         //if this factory supports the given schemalanguage return this factory else thrown exception
 267         if (f.isObjectModelSupported(uri)) {
 268             return f;
 269         } else {
 270             throw new XPathFactoryConfigurationException("Factory "
 271                     + factoryClassName + " doesn't support given " + uri
 272                     + " object model");
 273         }
 274 
 275     }
 276 
 277     /**
 278      * <p>Is specified object model supported by this <code>XPathFactory</code>?</p>
 279      *
 280      * @param objectModel Specifies the object model which the returned <code>XPathFactory</code> will understand.
 281      *
 282      * @return <code>true</code> if <code>XPathFactory</code> supports <code>objectModel</code>, else <code>false</code>.
 283      *
 284      * @throws NullPointerException If <code>objectModel</code> is <code>null</code>.
 285      * @throws IllegalArgumentException If <code>objectModel.length() == 0</code>.
 286      */
 287     public abstract boolean isObjectModelSupported(String objectModel);
 288 
 289     /**
 290      * <p>Set a feature for this <code>XPathFactory</code> and
 291      * <code>XPath</code>s created by this factory.</p>
 292      *
 293      * <p>
 294      * Feature names are fully qualified {@link java.net.URI}s.
 295      * Implementations may define their own features.
 296      * An {@link XPathFactoryConfigurationException} is thrown if this
 297      * <code>XPathFactory</code> or the <code>XPath</code>s
 298      * it creates cannot support the feature.
 299      * It is possible for an <code>XPathFactory</code> to expose a feature value
 300      * but be unable to change its state.
 301      * </p>
 302      *
 303      * <p>
 304      * All implementations are required to support the {@link javax.xml.XMLConstants#FEATURE_SECURE_PROCESSING} feature.
 305      * When the feature is <code>true</code>, any reference to  an external function is an error.
 306      * Under these conditions, the implementation must not call the {@link XPathFunctionResolver}
 307      * and must throw an {@link XPathFunctionException}.
 308      * </p>
 309      *
 310      * @param name Feature name.
 311      * @param value Is feature state <code>true</code> or <code>false</code>.
 312      *
 313      * @throws XPathFactoryConfigurationException if this <code>XPathFactory</code> or the <code>XPath</code>s
 314      *   it creates cannot support this feature.
 315      * @throws NullPointerException if <code>name</code> is <code>null</code>.
 316      */
 317     public abstract void setFeature(String name, boolean value)
 318             throws XPathFactoryConfigurationException;
 319 
 320     /**
 321      * <p>Get the state of the named feature.</p>
 322      *
 323      * <p>
 324      * Feature names are fully qualified {@link java.net.URI}s.
 325      * Implementations may define their own features.
 326      * An {@link XPathFactoryConfigurationException} is thrown if this
 327      * <code>XPathFactory</code> or the <code>XPath</code>s
 328      * it creates cannot support the feature.
 329      * It is possible for an <code>XPathFactory</code> to expose a feature value
 330      * but be unable to change its state.
 331      * </p>
 332      *
 333      * @param name Feature name.
 334      *
 335      * @return State of the named feature.
 336      *
 337      * @throws XPathFactoryConfigurationException if this
 338      *   <code>XPathFactory</code> or the <code>XPath</code>s
 339      *   it creates cannot support this feature.
 340      * @throws NullPointerException if <code>name</code> is <code>null</code>.
 341      */
 342     public abstract boolean getFeature(String name)
 343             throws XPathFactoryConfigurationException;
 344 
 345     /**
 346      * <p>Establish a default variable resolver.</p>
 347      *
 348      * <p>Any <code>XPath</code> objects constructed from this factory will use
 349      * the specified resolver by default.</p>
 350      *
 351      * <p>A <code>NullPointerException</code> is thrown if <code>resolver</code>
 352      * is <code>null</code>.</p>
 353      *
 354      * @param resolver Variable resolver.
 355      *
 356      * @throws NullPointerException If <code>resolver</code> is
 357      *   <code>null</code>.
 358      */
 359     public abstract void setXPathVariableResolver(XPathVariableResolver resolver);
 360 
 361     /**
 362      * <p>Establish a default function resolver.</p>
 363      *
 364      * <p>Any <code>XPath</code> objects constructed from this factory will
 365      * use the specified resolver by default.</p>
 366      *
 367      * <p>A <code>NullPointerException</code> is thrown if
 368      * <code>resolver</code> is <code>null</code>.</p>
 369      *
 370      * @param resolver XPath function resolver.
 371      *
 372      * @throws NullPointerException If <code>resolver</code> is
 373      *   <code>null</code>.
 374      */
 375     public abstract void setXPathFunctionResolver(XPathFunctionResolver resolver);
 376 
 377     /**
 378     * <p>Return a new <code>XPath</code> using the underlying object
 379     * model determined when the <code>XPathFactory</code> was instantiated.</p>
 380     *
 381     * @return New instance of an <code>XPath</code>.
 382     */
 383     public abstract XPath newXPath();
 384 }