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 final 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     *     The class loader is asked for service provider provider-configuration files matching <code>javax.xml.xpath.XPathFactory</code>
 125     *     in the resource directory META-INF/services.
 126     *     See the JAR File Specification for file format and parsing rules.
 127     *     Each potential service provider is required to implement the method:
 128     *     <pre>
 129     *       {@link #isObjectModelSupported(String objectModel)}
 130     *     </pre>
 131     *     The first service provider found in class loader order that supports the specified object model is returned.
 132     *   </li>
 133     *   <li>
 134     *     Platform default <code>XPathFactory</code> is located in a platform specific way.
 135     *     There must be a platform default XPathFactory for the W3C DOM, i.e. {@link #DEFAULT_OBJECT_MODEL_URI}.
 136     *   </li>
 137     * </ol>
 138     * <p>If everything fails, an <code>XPathFactoryConfigurationException</code> will be thrown.</p>
 139     *
 140     * <p>Tip for Trouble-shooting:</p>
 141     * <p>See {@link java.util.Properties#load(java.io.InputStream)} for exactly how a property file is parsed.
 142     * In particular, colons ':' need to be escaped in a property file, so make sure the URIs are properly escaped in it.
 143     * For example:</p>
 144     * <pre>
 145     *   http\://java.sun.com/jaxp/xpath/dom=org.acme.DomXPathFactory
 146     * </pre>
 147     *
 148     * @param uri Identifies the underlying object model.
 149     *   The specification only defines the URI {@link #DEFAULT_OBJECT_MODEL_URI},
 150     *   <code>http://java.sun.com/jaxp/xpath/dom</code> for the W3C DOM,
 151     *   the org.w3c.dom package, and implementations are free to introduce other URIs for other object models.
 152     *
 153     * @return Instance of an <code>XPathFactory</code>.
 154     *
 155     * @throws XPathFactoryConfigurationException If the specified object model is unavailable.
 156     * @throws NullPointerException If <code>uri</code> is <code>null</code>.
 157         * @throws IllegalArgumentException If <code>uri</code> is <code>null</code>
 158     *   or <code>uri.length() == 0</code>.
 159     */
 160     public static final XPathFactory newInstance(final String uri)
 161         throws XPathFactoryConfigurationException {
 162 
 163         if (uri == null) {
 164                 throw new NullPointerException(
 165                         "XPathFactory#newInstance(String uri) cannot be called with uri == null"
 166                 );
 167         }
 168 
 169                 if (uri.length() == 0) {
 170                         throw new IllegalArgumentException(
 171                                 "XPathFactory#newInstance(String uri) cannot be called with uri == \"\""
 172                         );
 173                 }
 174 
 175                 ClassLoader classLoader = ss.getContextClassLoader();
 176 
 177         if (classLoader == null) {
 178             //use the current class loader
 179             classLoader = XPathFactory.class.getClassLoader();
 180         }
 181 
 182                 XPathFactory xpathFactory = new XPathFactoryFinder(classLoader).newFactory(uri);
 183 
 184                 if (xpathFactory == null) {
 185                         throw new XPathFactoryConfigurationException(
 186                                 "No XPathFactory implementation found for the object model: "
 187                                 + uri
 188                         );
 189                 }
 190 
 191                 return xpathFactory;
 192     }
 193 
 194     /**
 195      * <p>Obtain a new instance of a <code>XPathFactory</code> from a factory class name. <code>XPathFactory</code>
 196      * is returned if specified factory class supports the specified object model.
 197      * This function is useful when there are multiple providers in the classpath.
 198      * It gives more control to the application as it can specify which provider
 199      * should be loaded.</p>
 200      *
 201      *
 202      * <h2>Tip for Trouble-shooting</h2>
 203      * <p>Setting the <code>jaxp.debug</code> system property will cause
 204      * this method to print a lot of debug messages
 205      * to <code>System.err</code> about what it is doing and where it is looking at.</p>
 206      *
 207      * <p> If you have problems try:</p>
 208      * <pre>
 209      * java -Djaxp.debug=1 YourProgram ....
 210      * </pre>
 211      *
 212      * @param uri         Identifies the underlying object model. The specification only defines the URI
 213      *                    {@link #DEFAULT_OBJECT_MODEL_URI},<code>http://java.sun.com/jaxp/xpath/dom</code>
 214      *                    for the W3C DOM, the org.w3c.dom package, and implementations are free to introduce
 215      *                    other URIs for other object models.
 216      *
 217      * @param factoryClassName fully qualified factory class name that provides implementation of <code>javax.xml.xpath.XPathFactory</code>.
 218      *
 219      * @param classLoader <code>ClassLoader</code> used to load the factory class. If <code>null</code>
 220      *                     current <code>Thread</code>'s context classLoader is used to load the factory class.
 221      *
 222      *
 223      * @return New instance of a <code>XPathFactory</code>
 224      *
 225      * @throws XPathFactoryConfigurationException
 226      *                   if <code>factoryClassName</code> is <code>null</code>, or
 227      *                   the factory class cannot be loaded, instantiated
 228      *                   or the factory class does not support the object model specified
 229      *                   in the <code>uri</code> parameter.
 230      *
 231      * @throws NullPointerException If <code>uri</code> is <code>null</code>.
 232      * @throws IllegalArgumentException If <code>uri</code> is <code>null</code>
 233      *          or <code>uri.length() == 0</code>.
 234      *
 235      * @see #newInstance()
 236      * @see #newInstance(String uri)
 237      *
 238      * @since 1.6
 239      */
 240     public static XPathFactory newInstance(String uri, String factoryClassName, ClassLoader classLoader)
 241         throws XPathFactoryConfigurationException{
 242         ClassLoader cl = classLoader;
 243 
 244         if (uri == null) {
 245                 throw new NullPointerException(
 246                         "XPathFactory#newInstance(String uri) cannot be called with uri == null"
 247                 );
 248         }
 249 
 250                 if (uri.length() == 0) {
 251                         throw new IllegalArgumentException(
 252                                 "XPathFactory#newInstance(String uri) cannot be called with uri == \"\""
 253                         );
 254                 }
 255 
 256         if (cl == null) {
 257             cl = ss.getContextClassLoader();
 258         }
 259 
 260         XPathFactory f = new XPathFactoryFinder(cl).createInstance(factoryClassName);
 261 
 262         if (f == null) {
 263                         throw new XPathFactoryConfigurationException(
 264                                 "No XPathFactory implementation found for the object model: "
 265                                 + uri
 266                         );
 267         }
 268         //if this factory supports the given schemalanguage return this factory else thrown exception
 269         if(f.isObjectModelSupported(uri)){
 270             return f;
 271         }else{
 272             throw new XPathFactoryConfigurationException("Factory " + factoryClassName + " doesn't support given " + uri + " 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 }