src/javax/xml/xpath/XPathFactory.java

Print this page




  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 


 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();




  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 


 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();