1. // $Id: XPathExpression.java,v 1.10.16.1 2004/07/01 17:49:23 ndw Exp $
  2. /*
  3. * @(#)XPathExpression.java 1.6 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. /**
  12. * <p><code>XPathExpression</code> provides access to compiled XPath expressions.</p>
  13. *
  14. * <table id="XPathExpression-evaluation" border="1" cellpadding="2">
  15. * <thead>
  16. * <tr>
  17. * <th colspan="2">Evaluation of XPath Expressions.</th>
  18. * </tr>
  19. * </thead>
  20. * <tbody>
  21. * <tr>
  22. * <td>context</td>
  23. * <td>
  24. * If a request is made to evaluate the expression in the absence
  25. * of a context item, an empty document node will be used for the context.
  26. * For the purposes of evaluating XPath expressions, a DocumentFragment
  27. * is treated like a Document node.
  28. * </td>
  29. * </tr>
  30. * <tr>
  31. * <td>variables</td>
  32. * <td>
  33. * If the expression contains a variable reference, its value will be found through the {@link XPathVariableResolver}.
  34. * An {@link XPathExpressionException} is raised if the variable resolver is undefined or
  35. * the resolver returns <code>null</code> for the variable.
  36. * The value of a variable must be immutable through the course of any single evaluation.</p>
  37. * </td>
  38. * </tr>
  39. * <tr>
  40. * <td>functions</td>
  41. * <td>
  42. * If the expression contains a function reference, the function will be found through the {@link XPathFunctionResolver}.
  43. * An {@link XPathExpressionException} is raised if the function resolver is undefined or
  44. * the function resolver returns <code>null</code> for the function.</p>
  45. * </td>
  46. * </tr>
  47. * <tr>
  48. * <td>QNames</td>
  49. * <td>
  50. * QNames in the expression are resolved against the XPath namespace context.
  51. * </td>
  52. * </tr>
  53. * <tr>
  54. * <td>result</td>
  55. * <td>
  56. * This result of evaluating an expression is converted to an instance of the desired return type.
  57. * Valid return types are defined in {@link XPathConstants}.
  58. * Conversion to the return type follows XPath conversion rules.</p>
  59. * </td>
  60. * </tr>
  61. * </table>
  62. *
  63. * @author <a href="mailto:Norman.Walsh@Sun.com">Norman Walsh</a>
  64. * @author <a href="mailto:Jeff.Suttor@Sun.com">Jeff Suttor</a>
  65. * @version $Revision: 1.10.16.1 $, $Date: 2004/07/01 17:49:23 $
  66. * @see <a href="http://www.w3.org/TR/xpath#section-Expressions">XML Path Language (XPath) Version 1.0, Expressions</a>
  67. * @since 1.5
  68. */
  69. public interface XPathExpression {
  70. /**
  71. * <p>Evaluate the compiled XPath expression in the specified context and return the result as the specified type.</p>
  72. *
  73. * <p>See <a href="#XPathExpression-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,
  74. * variable, function and QName resolution and return type conversion.</p>
  75. *
  76. * <p>If <code>returnType</code> is not one of the types defined in {@link XPathConstants},
  77. * then an <code>IllegalArgumentException</code> is thrown.</p>
  78. *
  79. * <p>If a <code>null</code> value is provided for
  80. * <code>item</code>, an empty document will be used for the
  81. * context.
  82. * If <code>returnType</code> is <code>null</code>, then a <code>NullPointerException</code> is thrown.</p>
  83. *
  84. * @param item The starting context (node or node list, for example).
  85. * @param returnType The desired return type.
  86. *
  87. * @return The <code>Object</code> that is the result of evaluating the expression and converting the result to
  88. * <code>returnType</code>.
  89. *
  90. * @throws XPathExpressionException If the expression cannot be evaluated.
  91. * @throws IllegalArgumentException If <code>returnType</code> is not one of the types defined in {@link XPathConstants}.
  92. * @throws NullPointerException If <code>returnType</code> is <code>null</code>.
  93. */
  94. public Object evaluate(Object item, QName returnType)
  95. throws XPathExpressionException;
  96. /**
  97. * <p>Evaluate the compiled XPath expression in the specified context and return the result as a <code>String</code>.</p>
  98. *
  99. * <p>This method calls {@link #evaluate(Object item, QName returnType)} with a <code>returnType</code> of
  100. * {@link XPathConstants#STRING}.</p>
  101. *
  102. * <p>See <a href="#XPathExpression-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,
  103. * variable, function and QName resolution and return type conversion.</p>
  104. *
  105. * <p>If a <code>null</code> value is provided for
  106. * <code>item</code>, an empty document will be used for the
  107. * context.
  108. *
  109. * @param item The starting context (node or node list, for example).
  110. *
  111. * @return The <code>String</code> that is the result of evaluating the expression and converting the result to a
  112. * <code>String</code>.
  113. *
  114. * @throws XPathExpressionException If the expression cannot be evaluated.
  115. */
  116. public String evaluate(Object item)
  117. throws XPathExpressionException;
  118. /**
  119. * <p>Evaluate the compiled XPath expression in the context of the specified <code>InputSource</code> and return the result as the
  120. * specified type.</p>
  121. *
  122. * <p>This method builds a data model for the {@link InputSource} and calls
  123. * {@link #evaluate(Object item, QName returnType)} on the resulting document object.</p>
  124. *
  125. * <p>See <a href="#XPathExpression-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,
  126. * variable, function and QName resolution and return type conversion.</p>
  127. *
  128. * <p>If <code>returnType</code> is not one of the types defined in {@link XPathConstants},
  129. * then an <code>IllegalArgumentException</code> is thrown.</p>
  130. *
  131. * <p>If <code>source</code> or <code>returnType</code> is <code>null</code>,
  132. * then a <code>NullPointerException</code> is thrown.</p>
  133. *
  134. * @param source The <code>InputSource</code> of the document to evaluate over.
  135. * @param returnType The desired return type.
  136. *
  137. * @return The <code>Object</code> that is the result of evaluating the expression and converting the result to
  138. * <code>returnType</code>.
  139. *
  140. * @throws XPathExpressionException If the expression cannot be evaluated.
  141. * @throws IllegalArgumentException If <code>returnType</code> is not one of the types defined in {@link XPathConstants}.
  142. * @throws NullPointerException If <code>source</code> or <code>returnType</code> is <code>null</code>.
  143. */
  144. public Object evaluate(InputSource source, QName returnType)
  145. throws XPathExpressionException;
  146. /**
  147. * <p>Evaluate the compiled XPath expression in the context of the specified <code>InputSource</code> and return the result as a
  148. * <code>String</code>.</p>
  149. *
  150. * <p>This method calls {@link #evaluate(InputSource source, QName returnType)} with a <code>returnType</code> of
  151. * {@link XPathConstants#STRING}.</p>
  152. *
  153. * <p>See <a href="#XPathExpression-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,
  154. * variable, function and QName resolution and return type conversion.</p>
  155. *
  156. * <p>If <code>source</code> is <code>null</code>, then a <code>NullPointerException</code> is thrown.</p>
  157. *
  158. * @param source The <code>InputSource</code> of the document to evaluate over.
  159. *
  160. * @return The <code>String</code> that is the result of evaluating the expression and converting the result to a
  161. * <code>String</code>.
  162. *
  163. * @throws XPathExpressionException If the expression cannot be evaluated.
  164. * @throws NullPointerException If <code>source</code> is <code>null</code>.
  165. */
  166. public String evaluate(InputSource source)
  167. throws XPathExpressionException;
  168. }