1. // $Id: XPathFactory.java,v 1.13.10.1 2004/05/07 00:34:05 jsuttor Exp $
  2. /*
  3. * @(#)XPathFactory.java 1.7 04/07/26
  4. *
  5. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  6. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  7. */
  8. package javax.xml.xpath;
  9. /**
  10. * <p>An <code>XPathFactory</code> instance can be used to create
  11. * {@link javax.xml.xpath.XPath} objects.</p>
  12. *
  13. *<p>See {@link #newInstance(String uri)} for lookup mechanism.</p>
  14. *
  15. * @author <a href="mailto:Norman.Walsh@Sun.com">Norman Walsh</a>
  16. * @author <a href="mailto:Jeff.Suttor@Sun.com">Jeff Suttor</a>
  17. * @version $Revision: 1.13.10.1 $, $Date: 2004/05/07 00:34:05 $
  18. * @since 1.5
  19. */
  20. public abstract class XPathFactory {
  21. /**
  22. * <p>The default property name according to the JAXP spec.</p>
  23. */
  24. public static final String DEFAULT_PROPERTY_NAME = "javax.xml.xpath.XPathFactory";
  25. /**
  26. * <p>Default Object Model URI.</p>
  27. */
  28. public static final String DEFAULT_OBJECT_MODEL_URI = "http://java.sun.com/jaxp/xpath/dom";
  29. /**
  30. * <p>Protected constructor as {@link #newInstance()} or {@link #newInstance(String uri)}
  31. * should be used to create a new instance of an <code>XPathFactory</code>.</p>
  32. */
  33. protected XPathFactory() {
  34. }
  35. /**
  36. * <p>Get a new <code>XPathFactory</code> instance using the default object model,
  37. * {@link #DEFAULT_OBJECT_MODEL_URI},
  38. * the W3C DOM.</p>
  39. *
  40. * <p>This method is functionally equivalent to:</p>
  41. * <pre>
  42. * newInstance(DEFAULT_OBJECT_MODEL_URI)
  43. * </pre>
  44. *
  45. * <p>Since the implementation for the W3C DOM is always available, this method will never fail.</p>
  46. *
  47. * @return Instance of an <code>XPathFactory</code>.
  48. */
  49. public static final XPathFactory newInstance() {
  50. try {
  51. return newInstance(DEFAULT_OBJECT_MODEL_URI);
  52. } catch (XPathFactoryConfigurationException xpathFactoryConfigurationException) {
  53. throw new RuntimeException(
  54. "XPathFactory#newInstance() failed to create an XPathFactory for the default object model: "
  55. + DEFAULT_OBJECT_MODEL_URI
  56. + " with the XPathFactoryConfigurationException: "
  57. + xpathFactoryConfigurationException.toString()
  58. );
  59. }
  60. }
  61. /**
  62. * <p>Get a new <code>XPathFactory</code> instance using the specified object model.</p>
  63. *
  64. * <p>To find a <code>XPathFactory</code> object,
  65. * this method looks the following places in the following order where "the class loader" refers to the context class loader:</p>
  66. * <ol>
  67. * <li>
  68. * If the system property {@link #DEFAULT_PROPERTY_NAME} + ":uri" is present,
  69. * where uri is the parameter to this method, then its value is read as a class name.
  70. * The method will try to create a new instance of this class by using the class loader,
  71. * and returns it if it is successfully created.
  72. * </li>
  73. * <li>
  74. * ${java.home}/lib/jaxp.properties is read and the value associated with the key being the system property above is looked for.
  75. * If present, the value is processed just like above.
  76. * </li>
  77. * <li>
  78. * The class loader is asked for service provider provider-configuration files matching <code>javax.xml.xpath.XPathFactory</code>
  79. * in the resource directory META-INF/services.
  80. * See the JAR File Specification for file format and parsing rules.
  81. * Each potential service provider is required to implement the method:
  82. * <pre>
  83. * {@link #isObjectModelSupported(String objectModel)}
  84. * </pre>
  85. * The first service provider found in class loader order that supports the specified object model is returned.
  86. * </li>
  87. * <li>
  88. * Platform default <code>XPathFactory</code> is located in a platform specific way.
  89. * There must be a platform default XPathFactory for the W3C DOM, i.e. {@link #DEFAULT_OBJECT_MODEL_URI}.
  90. * </li>
  91. * </ol>
  92. * <p>If everything fails, an <code>XPathFactoryConfigurationException</code> will be thrown.</p>
  93. *
  94. * <p>Tip for Trouble-shooting:</p>
  95. * <p>See {@link java.util.Properties#load(java.io.InputStream)} for exactly how a property file is parsed.
  96. * In particular, colons ':' need to be escaped in a property file, so make sure the URIs are properly escaped in it.
  97. * For example:</p>
  98. * <pre>
  99. * http\://java.sun.com/jaxp/xpath/dom=org.acme.DomXPathFactory
  100. * </pre>
  101. *
  102. * @param uri Identifies the underlying object model.
  103. * The specification only defines the URI {@link #DEFAULT_OBJECT_MODEL_URI},
  104. * <code>http://java.sun.com/jaxp/xpath/dom</code> for the W3C DOM,
  105. * the org.w3c.dom package, and implementations are free to introduce other URIs for other object models.
  106. *
  107. * @return Instance of an <code>XPathFactory</code>.
  108. *
  109. * @throws XPathFactoryConfigurationException If the specified object model is unavailable.
  110. * @throws NullPointerException If <code>uri</code> is <code>null</code>.
  111. * @throws IllegalArgumentException If <code>uri.length() == 0</code>.
  112. */
  113. public static final XPathFactory newInstance(final String uri)
  114. throws XPathFactoryConfigurationException {
  115. if (uri == null) {
  116. throw new NullPointerException(
  117. "XPathFactory#newInstance(String uri) cannot be called with uri == null"
  118. );
  119. }
  120. if (uri.length() == 0) {
  121. throw new IllegalArgumentException(
  122. "XPathFactory#newInstance(String uri) cannot be called with uri == \"\""
  123. );
  124. }
  125. ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
  126. XPathFactory xpathFactory = new XPathFactoryFinder(classLoader).newFactory(uri);
  127. if (xpathFactory == null) {
  128. throw new XPathFactoryConfigurationException(
  129. "No XPathFctory implementation found for the object model: "
  130. + uri
  131. );
  132. }
  133. return xpathFactory;
  134. }
  135. /**
  136. * <p>Is specified object model supported by this <code>XPathFactory</code>?</p>
  137. *
  138. * @param objectModel Specifies the object model which the returned <code>XPathFactory</code> will understand.
  139. *
  140. * @return <code>true</code> if <code>XPathFactory</code> supports <code>objectModel</code>, else <code>false</code>.
  141. *
  142. * @throws NullPointerException If <code>objectModel</code> is <code>null</code>.
  143. * @throws IllegalArgumentException If <code>objectModel.length() == 0</code>.
  144. */
  145. public abstract boolean isObjectModelSupported(String objectModel);
  146. /**
  147. * <p>Set a feature for this <code>XPathFactory</code> and <code>XPath</code>s created by this factory.</p>
  148. *
  149. * <p>
  150. * Feature names are fully qualified {@link java.net.URI}s.
  151. * Implementations may define their own features.
  152. * An {@link XPathFactoryConfigurationException} is thrown if this <code>XPathFactory</code> or the <code>XPath</code>s
  153. * it creates cannot support the feature.
  154. * It is possible for an <code>XPathFactory</code> to expose a feature value but be unable to change its state.
  155. * </p>
  156. *
  157. * <p>
  158. * All implementations are required to support the {@link javax.xml.XMLConstants#FEATURE_SECURE_PROCESSING} feature.
  159. * When the feature is <code>true</code>, any reference to an external function is an error.
  160. * Under these conditions, the implementation must not call the {@link XPathFunctionResolver}
  161. * and must throw an {@link XPathFunctionException}.
  162. * </p>
  163. *
  164. * @param name Feature name.
  165. * @param value Is feature state <code>true</code> or <code>false</code>.
  166. *
  167. * @throws XPathFactoryConfigurationException if this <code>XPathFactory</code> or the <code>XPath</code>s
  168. * it creates cannot support this feature.
  169. * @throws NullPointerException if <code>name</code> is <code>null</code>.
  170. */
  171. public abstract void setFeature(String name, boolean value)
  172. throws XPathFactoryConfigurationException;
  173. /**
  174. * <p>Get the state of the named feature.</p>
  175. *
  176. * <p>
  177. * Feature names are fully qualified {@link java.net.URI}s.
  178. * Implementations may define their own features.
  179. * An {@link XPathFactoryConfigurationException} is thrown if this <code>XPathFactory</code> or the <code>XPath</code>s
  180. * it creates cannot support the feature.
  181. * It is possible for an <code>XPathFactory</code> to expose a feature value but be unable to change its state.
  182. * </p>
  183. *
  184. * @param name Feature name.
  185. *
  186. * @return State of the named feature.
  187. *
  188. * @throws XPathFactoryConfigurationException if this <code>XPathFactory</code> or the <code>XPath</code>s
  189. * it creates cannot support this feature.
  190. * @throws NullPointerException if <code>name</code> is <code>null</code>.
  191. */
  192. public abstract boolean getFeature(String name)
  193. throws XPathFactoryConfigurationException;
  194. /**
  195. * <p>Establish a default variable resolver.</p>
  196. *
  197. * <p>Any <code>XPath</code> objects constructed from this factory will use
  198. * the specified resolver by default.</p>
  199. *
  200. * <p>A <code>NullPointerException</code> is thrown if <code>resolver</code> is <code>null</code>.</p>
  201. *
  202. * @param resolver Variable resolver.
  203. *
  204. * @throws NullPointerException If <code>resolver</code> is <code>null</code>.
  205. */
  206. public abstract void setXPathVariableResolver(XPathVariableResolver resolver);
  207. /**
  208. * <p>Establish a default function resolver.</p>
  209. *
  210. * <p>Any <code>XPath</code> objects constructed from this factory will use
  211. * the specified resolver by default.</p>
  212. *
  213. * <p>A <code>NullPointerException</code> is thrown if <code>resolver</code> is <code>null</code>.</p>
  214. *
  215. * @param resolver XPath function resolver.
  216. *
  217. * @throws NullPointerException If <code>resolver</code> is <code>null</code>.
  218. */
  219. public abstract void setXPathFunctionResolver(XPathFunctionResolver resolver);
  220. /**
  221. * <p>Return a new <code>XPath</code> using the underlying object
  222. * model determined when the <code>XPathFactory</code> was instantiated.</p>
  223. *
  224. * @return New instance of an <code>XPath</code>.
  225. */
  226. public abstract XPath newXPath();
  227. }