1. // $Id: XPath.java,v 1.12.14.2.2.3 2004/07/01 17:49:22 ndw Exp $
  2. /*
  3. * @(#)XPath.java 1.10 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. import org.xml.sax.InputSource;
  10. import javax.xml.namespace.QName;
  11. import javax.xml.namespace.NamespaceContext;
  12. /**
  13. * <p><code>XPath</code> provides access to the XPath evaluation environment and expressions.</p>
  14. *
  15. * <table id="XPath-evaluation" border="1" cellpadding="2">
  16. * <thead>
  17. * <tr>
  18. * <th colspan="2">Evaluation of XPath Expressions.</th>
  19. * </tr>
  20. * </thead>
  21. * <tbody>
  22. * <tr>
  23. * <td>context</td>
  24. * <td>
  25. * If a request is made to evaluate the expression in the absence
  26. * of a context item, an empty document node will be used for the context.
  27. * For the purposes of evaluating XPath expressions, a DocumentFragment
  28. * is treated like a Document node.
  29. * </td>
  30. * </tr>
  31. * <tr>
  32. * <td>variables</td>
  33. * <td>
  34. * If the expression contains a variable reference, its value will be found through the {@link XPathVariableResolver}
  35. * set with {@link #setXPathVariableResolver(XPathVariableResolver resolver)}.
  36. * An {@link XPathExpressionException} is raised if the variable resolver is undefined or
  37. * the resolver returns <code>null</code> for the variable.
  38. * The value of a variable must be immutable through the course of any single evaluation.</p>
  39. * </td>
  40. * </tr>
  41. * <tr>
  42. * <td>functions</td>
  43. * <td>
  44. * If the expression contains a function reference, the function will be found through the {@link XPathFunctionResolver}
  45. * set with {@link #setXPathFunctionResolver(XPathFunctionResolver resolver)}.
  46. * An {@link XPathExpressionException} is raised if the function resolver is undefined or
  47. * the function resolver returns <code>null</code> for the function.</p>
  48. * </td>
  49. * </tr>
  50. * <tr>
  51. * <td>QNames</td>
  52. * <td>
  53. * QNames in the expression are resolved against the XPath namespace context
  54. * set with {@link #setNamespaceContext(NamespaceContext nsContext)}.
  55. * </td>
  56. * </tr>
  57. * <tr>
  58. * <td>result</td>
  59. * <td>
  60. * This result of evaluating an expression is converted to an instance of the desired return type.
  61. * Valid return types are defined in {@link XPathConstants}.
  62. * Conversion to the return type follows XPath conversion rules.</p>
  63. * </td>
  64. * </tr>
  65. * </table>
  66. *
  67. * @author <a href="Norman.Walsh@Sun.com">Norman Walsh</a>
  68. * @author <a href="Jeff.Suttor@Sun.com">Jeff Suttor</a>
  69. * @version $Revision: 1.12.14.2.2.3 $, $Date: 2004/07/01 17:49:22 $
  70. * @see <a href="http://www.w3.org/TR/xpath">XML Path Language (XPath) Version 1.0</a>
  71. * @since 1.5
  72. */
  73. public interface XPath {
  74. /**
  75. * <p>Reset this <code>XPath</code> to its original configuration.</p>
  76. *
  77. * <p><code>XPath</code> is reset to the same state as when it was created with
  78. * {@link XPathFactory#newXPath()}.
  79. * <code>reset()</code> is designed to allow the reuse of existing <code>XPath</code>s
  80. * thus saving resources associated with the creation of new <code>XPath</code>s.</p>
  81. *
  82. * <p>The reset <code>XPath</code> is not guaranteed to have the same {@link XPathFunctionResolver}, {@link XPathVariableResolver}
  83. * or {@link NamespaceContext} <code>Object</code>s, e.g. {@link Object#equals(Object obj)}.
  84. * It is guaranteed to have a functionally equal <code>XPathFunctionResolver</code>, <code>XPathVariableResolver</code>
  85. * and <code>NamespaceContext</code>.</p>
  86. */
  87. public void reset();
  88. /**
  89. * <p>Establish a variable resolver.</p>
  90. *
  91. * <p>A <code>NullPointerException</code> is thrown if <code>resolver</code> is <code>null</code>.</p>
  92. *
  93. * @param resolver Variable resolver.
  94. *
  95. * @throws NullPointerException If <code>resolver</code> is <code>null</code>.
  96. */
  97. public void setXPathVariableResolver(XPathVariableResolver resolver);
  98. /**
  99. * <p>Return the current variable resolver.</p>
  100. *
  101. * <p><code>null</code> is returned in no variable resolver is in effect.</p>
  102. *
  103. * @return Current variable resolver.
  104. */
  105. public XPathVariableResolver getXPathVariableResolver();
  106. /**
  107. * <p>Establish a function resolver.</p>
  108. *
  109. * <p>A <code>NullPointerException</code> is thrown if <code>resolver</code> is <code>null</code>.</p>
  110. *
  111. * @param resolver XPath function resolver.
  112. *
  113. * @throws NullPointerException If <code>resolver</code> is <code>null</code>.
  114. */
  115. public void setXPathFunctionResolver(XPathFunctionResolver resolver);
  116. /**
  117. * <p>Return the current function resolver.</p>
  118. *
  119. * <p><code>null</code> is returned in no function resolver is in effect.</p>
  120. *
  121. * @return Current function resolver.
  122. */
  123. public XPathFunctionResolver getXPathFunctionResolver();
  124. /**
  125. * <p>Establish a namespace context.</p>
  126. *
  127. * <p>A <code>NullPointerException</code> is thrown if <code>nsContext</code> is <code>null</code>.</p>
  128. *
  129. * @param nsContext Namespace context to use.
  130. *
  131. * @throws NullPointerException If <code>nsContext</code> is <code>null</code>.
  132. */
  133. public void setNamespaceContext(NamespaceContext nsContext);
  134. /**
  135. * <p>Return the current namespace context.</p>
  136. *
  137. * <p><code>null</code> is returned in no namespace context is in effect.</p>
  138. *
  139. * @return Current Namespace context.
  140. */
  141. public NamespaceContext getNamespaceContext();
  142. /**
  143. * <p>Compile an XPath expression for later evaluation.</p>
  144. *
  145. * <p>If <code>expression</code> contains any {@link XPathFunction}s,
  146. * they must be available via the {@link XPathFunctionResolver}.
  147. * An {@link XPathExpressionException} will be thrown if the <code>XPathFunction</code>
  148. * cannot be resovled with the <code>XPathFunctionResolver</code>.</p>
  149. *
  150. * <p>If <code>expression</code> is <code>null</code>, a <code>NullPointerException</code> is thrown.</p>
  151. *
  152. * @param expression The XPath expression.
  153. *
  154. * @return Compiled XPath expression.
  155. * @throws XPathExpressionException If <code>expression</code> cannot be compiled.
  156. * @throws NullPointerException If <code>expression</code> is <code>null</code>.
  157. */
  158. public XPathExpression compile(String expression)
  159. throws XPathExpressionException;
  160. /**
  161. * <p>Evaluate an <code>XPath</code> expression in the specified context and return the result as the specified type.</p>
  162. *
  163. * <p>See <a href="#XPath-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,
  164. * variable, function and <code>QName</code> resolution and return type conversion.</p>
  165. *
  166. * <p>If <code>returnType</code> is not one of the types defined in {@link XPathConstants} (
  167. * {@link XPathConstants#NUMBER NUMBER},
  168. * {@link XPathConstants#STRING STRING},
  169. * {@link XPathConstants#BOOLEAN BOOLEAN},
  170. * {@link XPathConstants#NODE NODE} or
  171. * {@link XPathConstants#NODESET NODESET})
  172. * then an <code>IllegalArgumentException</code> is thrown.</p>
  173. *
  174. * <p>If a <code>null</code> value is provided for
  175. * <code>item</code>, an empty document will be used for the
  176. * context.
  177. * If <code>expression</code> or <code>returnType</code> is <code>null</code>, then a
  178. * <code>NullPointerException</code> is thrown.</p>
  179. *
  180. * @param expression The XPath expression.
  181. * @param item The starting context (node or node list, for example).
  182. * @param returnType The desired return type.
  183. *
  184. * @return Result of evaluating an XPath expression as an <code>Object</code> of <code>returnType</code>.
  185. *
  186. * @throws XPathExpressionException If <code>expression</code> cannot be evaluated.
  187. * @throws IllegalArgumentException If <code>returnType</code> is not one of the types defined in {@link XPathConstants}.
  188. * @throws NullPointerException If <code>expression</code> or <code>returnType</code> is <code>null</code>.
  189. */
  190. public Object evaluate(String expression, Object item, QName returnType)
  191. throws XPathExpressionException;
  192. /**
  193. * <p>Evaluate an XPath expression in the specified context and return the result as a <code>String</code>.</p>
  194. *
  195. * <p>This method calls {@link #evaluate(String expression, Object item, QName returnType)} with a <code>returnType</code> of
  196. * {@link XPathConstants#STRING}.</p>
  197. *
  198. * <p>See <a href="#XPath-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,
  199. * variable, function and QName resolution and return type conversion.</p>
  200. *
  201. * <p>If a <code>null</code> value is provided for
  202. * <code>item</code>, an empty document will be used for the
  203. * context.
  204. * If <code>expression</code> is <code>null</code>, then a <code>NullPointerException</code> is thrown.</p>
  205. *
  206. * @param expression The XPath expression.
  207. * @param item The starting context (node or node list, for example).
  208. *
  209. * @return The <code>String</code> that is the result of evaluating the expression and
  210. * converting the result to a <code>String</code>.
  211. *
  212. * @throws XPathExpressionException If <code>expression</code> cannot be evaluated.
  213. * @throws NullPointerException If <code>expression</code> is <code>null</code>.
  214. */
  215. public String evaluate(String expression, Object item)
  216. throws XPathExpressionException;
  217. /**
  218. * <p>Evaluate an XPath expression in the context of the specified <code>InputSource</code>
  219. * and return the result as the specified type.</p>
  220. *
  221. * <p>This method builds a data model for the {@link InputSource} and calls
  222. * {@link #evaluate(String expression, Object item, QName returnType)} on the resulting document object.</p>
  223. *
  224. * <p>See <a href="#XPath-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,
  225. * variable, function and QName resolution and return type conversion.</p>
  226. *
  227. * <p>If <code>returnType</code> is not one of the types defined in {@link XPathConstants},
  228. * then an <code>IllegalArgumentException</code> is thrown.</p>
  229. *
  230. * <p>If <code>expression</code>, <code>source</code> or <code>returnType</code> is <code>null</code>,
  231. * then a <code>NullPointerException</code> is thrown.</p>
  232. *
  233. * @param expression The XPath expression.
  234. * @param source The input source of the document to evaluate over.
  235. * @param returnType The desired return type.
  236. *
  237. * @return The <code>Object</code> that encapsulates the result of evaluating the expression.
  238. *
  239. * @throws XPathExpressionException If expression cannot be evaluated.
  240. * @throws IllegalArgumentException If <code>returnType</code> is not one of the types defined in {@link XPathConstants}.
  241. * @throws NullPointerException If <code>expression</code>, <code>source</code> or <code>returnType</code>
  242. * is <code>null</code>.
  243. */
  244. public Object evaluate(
  245. String expression,
  246. InputSource source,
  247. QName returnType)
  248. throws XPathExpressionException;
  249. /**
  250. * <p>Evaluate an XPath expression in the context of the specified <code>InputSource</code>
  251. * and return the result as a <code>String</code>.</p>
  252. *
  253. * <p>This method calls {@link #evaluate(String expression, InputSource source, QName returnType)} with a
  254. * <code>returnType</code> of {@link XPathConstants#STRING}.</p>
  255. *
  256. * <p>See <a href="#XPath-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,
  257. * variable, function and QName resolution and return type conversion.</p>
  258. *
  259. * <p>If <code>expression</code> or <code>source</code> is <code>null</code>,
  260. * then a <code>NullPointerException</code> is thrown.</p>
  261. *
  262. * @param expression The XPath expression.
  263. * @param source The <code>InputSource</code> of the document to evaluate over.
  264. *
  265. * @return The <code>String</code> that is the result of evaluating the expression and
  266. * converting the result to a <code>String</code>.
  267. *
  268. * @throws XPathExpressionException If expression cannot be evaluated.
  269. * @throws NullPointerException If <code>expression</code> or <code>source</code> is <code>null</code>.
  270. */
  271. public String evaluate(String expression, InputSource source)
  272. throws XPathExpressionException;
  273. }