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  * @version $Revision: 1.7 $, $Date: 2010-11-01 04:36:14 $
  49  * @since 1.5
  50  */
  51 public abstract class XPathFactory {
  52 
  53 
  54     /**
  55      * <p>The default property name according to the JAXP spec.</p>
  56      */
  57     public static final String DEFAULT_PROPERTY_NAME = "javax.xml.xpath.XPathFactory";
  58 
  59     /**
  60      * <p>Default Object Model URI.</p>
  61      */
  62     public static final String DEFAULT_OBJECT_MODEL_URI = "http://java.sun.com/jaxp/xpath/dom";
  63 
  64     /**
  65      *<p> Take care of restrictions imposed by java security model </p>
  66      */
  67     private static SecuritySupport ss = new SecuritySupport() ;
  68 
  69     /**
  70      * <p>Protected constructor as {@link #newInstance()} or {@link #newInstance(String uri)}
  71      * or {@link #newInstance(String uri, String factoryClassName, ClassLoader classLoader)}
  72      * should be used to create a new instance of an <code>XPathFactory</code>.</p>
  73      */
  74     protected XPathFactory() {
  75     }
  76 
  77     /**
  78      * <p>Get a new <code>XPathFactory</code> instance using the default object model,
  79      * {@link #DEFAULT_OBJECT_MODEL_URI},
  80      * the W3C DOM.</p>
  81      *
  82      * <p>This method is functionally equivalent to:</p>
  83      * <pre>
  84      *   newInstance(DEFAULT_OBJECT_MODEL_URI)
  85      * </pre>
  86      *
  87      * <p>Since the implementation for the W3C DOM is always available, this method will never fail.</p>
  88      *
  89      * @return Instance of an <code>XPathFactory</code>.
  90      *
  91      * @throws RuntimeException When there is a failure in creating an
  92      *   <code>XPathFactory</code> for the default object model.
  93      */
  94     public static final XPathFactory newInstance() {
  95 
  96         try {
  97                 return newInstance(DEFAULT_OBJECT_MODEL_URI);
  98         } catch (XPathFactoryConfigurationException xpathFactoryConfigurationException) {
  99                 throw new RuntimeException(
 100                         "XPathFactory#newInstance() failed to create an XPathFactory for the default object model: "
 101                         + DEFAULT_OBJECT_MODEL_URI
 102                         + " with the XPathFactoryConfigurationException: "
 103                         + xpathFactoryConfigurationException.toString()
 104                 );
 105         }
 106     }
 107 
 108     /**
 109     * <p>Get a new <code>XPathFactory</code> instance using the specified object model.</p>
 110     *
 111     * <p>To find a <code>XPathFactory</code> object,
 112     * this method looks the following places in the following order where "the class loader" refers to the context class loader:</p>
 113     * <ol>
 114     *   <li>
 115     *     If the system property {@link #DEFAULT_PROPERTY_NAME} + ":uri" is present,
 116     *     where uri is the parameter to this method, then its value is read as a class name.
 117     *     The method will try to create a new instance of this class by using the class loader,
 118     *     and returns it if it is successfully created.
 119     *   </li>
 120     *   <li>
 121     *     ${java.home}/lib/jaxp.properties is read and the value associated with the key being the system property above is looked for.
 122     *     If present, the value is processed just like above.
 123     *   </li>
 124     *   <li>
 125     *     Uses the service-provider loading facilities, defined by the {@link java.util.ServiceLoader} class, to attempt 
 126     *     to locate and load an implementation of the service. 
 127     *     Each potential service provider is required to implement the method:
 128     *     <pre>
 129     *       {@link #isObjectModelSupported(String objectModel)}
 130     *     </pre>
 131     *      A service provider is deemed as valid if it supports the specified object model.
 132     *  
 133     *  <p>In case of multiple providers, the first non-default implementation shall be 
 134     *  instantiated and returned.  The default implementation is returned if it is 
 135     *  the only one found by the service loader.</p>
 136     *  <p>
 137     *  If a misconfigured provider is encountered and {@link java.util.ServiceConfigurationError} is thrown, 
 138     *  the error will be wrapped in a {@link javax.xml.xpath.XPathFactoryConfigurationException}.</p>
 139     *   </li>
 140     *   <li>
 141     *     Platform default <code>XPathFactory</code> is located in a platform specific way.
 142     *     There must be a platform default XPathFactory for the W3C DOM, i.e. {@link #DEFAULT_OBJECT_MODEL_URI}.
 143     *   </li>
 144     * </ol>
 145     * <p>If everything fails, an <code>XPathFactoryConfigurationException</code> will be thrown.</p>
 146     *
 147     * <p>Tip for Trouble-shooting:</p>
 148     * <p>See {@link java.util.Properties#load(java.io.InputStream)} for exactly how a property file is parsed.
 149     * In particular, colons ':' need to be escaped in a property file, so make sure the URIs are properly escaped in it.
 150     * For example:</p>
 151     * <pre>
 152     *   http\://java.sun.com/jaxp/xpath/dom=org.acme.DomXPathFactory
 153     * </pre>
 154     *
 155     * @param uri Identifies the underlying object model.
 156     *   The specification only defines the URI {@link #DEFAULT_OBJECT_MODEL_URI},
 157     *   <code>http://java.sun.com/jaxp/xpath/dom</code> for the W3C DOM,
 158     *   the org.w3c.dom package, and implementations are free to introduce other URIs for other object models.
 159     *
 160     * @return Instance of an <code>XPathFactory</code>.
 161     *
 162     * @throws XPathFactoryConfigurationException If the specified object model is unavailable, or
 163     *                   if a misconfigured provider is encountered
 164     * @throws NullPointerException If <code>uri</code> is <code>null</code>.
 165         * @throws IllegalArgumentException If <code>uri</code> is <code>null</code>
 166     *   or <code>uri.length() == 0</code>.
 167     */
 168     public static final XPathFactory newInstance(final String uri)
 169         throws XPathFactoryConfigurationException {
 170 
 171         if (uri == null) {
 172                 throw new NullPointerException(
 173                         "XPathFactory#newInstance(String uri) cannot be called with uri == null"
 174                 );
 175         }
 176 
 177                 if (uri.length() == 0) {
 178                         throw new IllegalArgumentException(
 179                                 "XPathFactory#newInstance(String uri) cannot be called with uri == \"\""
 180                         );
 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 
 199                 return xpathFactory;
 200     }
 201 
 202     /**
 203      * <p>Obtain a new instance of a <code>XPathFactory</code> from a factory class name. <code>XPathFactory</code>
 204      * is returned if specified factory class supports the specified object model.
 205      * This function is useful when there are multiple providers in the classpath.
 206      * It gives more control to the application as it can specify which provider
 207      * should be loaded.</p>
 208      *
 209      *
 210      * <h2>Tip for Trouble-shooting</h2>
 211      * <p>Setting the <code>jaxp.debug</code> system property will cause
 212      * this method to print a lot of debug messages
 213      * to <code>System.err</code> about what it is doing and where it is looking at.</p>
 214      *
 215      * <p> If you have problems try:</p>
 216      * <pre>
 217      * java -Djaxp.debug=1 YourProgram ....
 218      * </pre>
 219      *
 220      * @param uri         Identifies the underlying object model. The specification only defines the URI
 221      *                    {@link #DEFAULT_OBJECT_MODEL_URI},<code>http://java.sun.com/jaxp/xpath/dom</code>
 222      *                    for the W3C DOM, the org.w3c.dom package, and implementations are free to introduce
 223      *                    other URIs for other object models.
 224      *
 225      * @param factoryClassName fully qualified factory class name that provides implementation of <code>javax.xml.xpath.XPathFactory</code>.
 226      *
 227      * @param classLoader <code>ClassLoader</code> used to load the factory class. If <code>null</code>
 228      *                     current <code>Thread</code>'s context classLoader is used to load the factory class.
 229      *
 230      *
 231      * @return New instance of a <code>XPathFactory</code>
 232      *
 233      * @throws XPathFactoryConfigurationException
 234      *                   if <code>factoryClassName</code> is <code>null</code>, or
 235      *                   the factory class cannot be loaded, instantiated
 236      *                   or the factory class does not support the object model specified
 237      *                   in the <code>uri</code> parameter.
 238      *
 239      * @throws NullPointerException If <code>uri</code> is <code>null</code>.
 240      * @throws IllegalArgumentException If <code>uri</code> is <code>null</code>
 241      *          or <code>uri.length() == 0</code>.
 242      *
 243      * @see #newInstance()
 244      * @see #newInstance(String uri)
 245      *
 246      * @since 1.6
 247      */
 248     public static XPathFactory newInstance(String uri, String factoryClassName, ClassLoader classLoader)
 249         throws XPathFactoryConfigurationException{
 250         ClassLoader cl = classLoader;
 251 
 252         if (uri == null) {
 253                 throw new NullPointerException(
 254                         "XPathFactory#newInstance(String uri) cannot be called with uri == null"
 255                 );
 256         }
 257 
 258                 if (uri.length() == 0) {
 259                         throw new IllegalArgumentException(
 260                                 "XPathFactory#newInstance(String uri) cannot be called with uri == \"\""
 261                         );
 262                 }
 263 
 264         if (cl == null) {
 265             cl = ss.getContextClassLoader();
 266         }
 267 
 268         XPathFactory f = new XPathFactoryFinder(cl).createInstance(factoryClassName);
 269 
 270         if (f == null) {
 271                         throw new XPathFactoryConfigurationException(
 272                                 "No XPathFactory implementation found for the object model: "
 273                                 + uri
 274                         );
 275         }
 276         //if this factory supports the given schemalanguage return this factory else thrown exception
 277         if(f.isObjectModelSupported(uri)){
 278             return f;
 279         }else{
 280             throw new XPathFactoryConfigurationException("Factory " + factoryClassName + " doesn't support given " + uri + " object model");
 281         }
 282 
 283     }
 284 
 285         /**
 286          * <p>Is specified object model supported by this <code>XPathFactory</code>?</p>
 287          *
 288          * @param objectModel Specifies the object model which the returned <code>XPathFactory</code> will understand.
 289          *
 290          * @return <code>true</code> if <code>XPathFactory</code> supports <code>objectModel</code>, else <code>false</code>.
 291          *
 292          * @throws NullPointerException If <code>objectModel</code> is <code>null</code>.
 293          * @throws IllegalArgumentException If <code>objectModel.length() == 0</code>.
 294          */
 295         public abstract boolean isObjectModelSupported(String objectModel);
 296 
 297     /**
 298      * <p>Set a feature for this <code>XPathFactory</code> and
 299      * <code>XPath</code>s created by this factory.</p>
 300      *
 301      * <p>
 302      * Feature names are fully qualified {@link java.net.URI}s.
 303      * Implementations may define their own features.
 304      * An {@link XPathFactoryConfigurationException} is thrown if this
 305      * <code>XPathFactory</code> or the <code>XPath</code>s
 306      * it creates cannot support the feature.
 307      * It is possible for an <code>XPathFactory</code> to expose a feature value
 308      * but be unable to change its state.
 309      * </p>
 310      *
 311      * <p>
 312      * All implementations are required to support the {@link javax.xml.XMLConstants#FEATURE_SECURE_PROCESSING} feature.
 313      * When the feature is <code>true</code>, any reference to  an external function is an error.
 314      * Under these conditions, the implementation must not call the {@link XPathFunctionResolver}
 315      * and must throw an {@link XPathFunctionException}.
 316      * </p>
 317      *
 318      * @param name Feature name.
 319      * @param value Is feature state <code>true</code> or <code>false</code>.
 320      *
 321      * @throws XPathFactoryConfigurationException if this <code>XPathFactory</code> or the <code>XPath</code>s
 322      *   it creates cannot support this feature.
 323      * @throws NullPointerException if <code>name</code> is <code>null</code>.
 324      */
 325         public abstract void setFeature(String name, boolean value)
 326                 throws XPathFactoryConfigurationException;
 327 
 328     /**
 329      * <p>Get the state of the named feature.</p>
 330      *
 331      * <p>
 332      * Feature names are fully qualified {@link java.net.URI}s.
 333      * Implementations may define their own features.
 334      * An {@link XPathFactoryConfigurationException} is thrown if this
 335      * <code>XPathFactory</code> or the <code>XPath</code>s
 336      * it creates cannot support the feature.
 337      * It is possible for an <code>XPathFactory</code> to expose a feature value
 338      * but be unable to change its state.
 339      * </p>
 340      *
 341      * @param name Feature name.
 342      *
 343      * @return State of the named feature.
 344      *
 345      * @throws XPathFactoryConfigurationException if this
 346      *   <code>XPathFactory</code> or the <code>XPath</code>s
 347      *   it creates cannot support this feature.
 348      * @throws NullPointerException if <code>name</code> is <code>null</code>.
 349      */
 350         public abstract boolean getFeature(String name)
 351                 throws XPathFactoryConfigurationException;
 352 
 353     /**
 354      * <p>Establish a default variable resolver.</p>
 355      *
 356      * <p>Any <code>XPath</code> objects constructed from this factory will use
 357      * the specified resolver by default.</p>
 358      *
 359      * <p>A <code>NullPointerException</code> is thrown if <code>resolver</code>
 360      * is <code>null</code>.</p>
 361      *
 362      * @param resolver Variable resolver.
 363      *
 364      * @throws NullPointerException If <code>resolver</code> is
 365      *   <code>null</code>.
 366      */
 367     public abstract void setXPathVariableResolver(XPathVariableResolver resolver);
 368 
 369     /**
 370        * <p>Establish a default function resolver.</p>
 371        *
 372        * <p>Any <code>XPath</code> objects constructed from this factory will
 373        * use the specified resolver by default.</p>
 374        *
 375        * <p>A <code>NullPointerException</code> is thrown if
 376        * <code>resolver</code> is <code>null</code>.</p>
 377        *
 378        * @param resolver XPath function resolver.
 379        *
 380        * @throws NullPointerException If <code>resolver</code> is
 381        *   <code>null</code>.
 382        */
 383     public abstract void setXPathFunctionResolver(XPathFunctionResolver resolver);
 384 
 385     /**
 386     * <p>Return a new <code>XPath</code> using the underlying object
 387     * model determined when the <code>XPathFactory</code> was instantiated.</p>
 388     *
 389     * @return New instance of an <code>XPath</code>.
 390     */
 391     public abstract XPath newXPath();
 392 }