1 /*
   2  * Copyright (c) 2003, 2015, 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 import javax.xml.namespace.NamespaceContext;
  29 import javax.xml.namespace.QName;
  30 import org.xml.sax.InputSource;
  31 
  32 /**
  33  * {@code XPath} provides access to the XPath evaluation environment and expressions.
  34  *
  35  * <a name="XPath-evaluation"/>
  36  * <table border="1" cellpadding="2">
  37  *   <thead>
  38  *     <tr>
  39  *       <th colspan="2">Evaluation of XPath Expressions.</th>
  40  *     </tr>
  41  *   </thead>
  42  *     <tr>
  43  *       <td>context</td>
  44  *       <td>
  45  *         If a request is made to evaluate the expression in the absence
  46  * of a context item, an empty document node will be used for the context.
  47  * For the purposes of evaluating XPath expressions, a DocumentFragment
  48  * is treated like a Document node.
  49  *      </td>
  50  *    </tr>
  51  *    <tr>
  52  *      <td>variables</td>
  53  *      <td>
  54  *        If the expression contains a variable reference, its value will be found through the {@link XPathVariableResolver}
  55  *        set with {@link #setXPathVariableResolver(XPathVariableResolver resolver)}.
  56  *        An {@link XPathExpressionException} is raised if the variable resolver is undefined or
  57  *        the resolver returns {@code null} for the variable.
  58  *        The value of a variable must be immutable through the course of any single evaluation.
  59  *      </td>
  60  *    </tr>
  61  *    <tr>
  62  *      <td>functions</td>
  63  *      <td>
  64  *        If the expression contains a function reference, the function will be found through the {@link XPathFunctionResolver}
  65  *        set with {@link #setXPathFunctionResolver(XPathFunctionResolver resolver)}.
  66  *        An {@link XPathExpressionException} is raised if the function resolver is undefined or
  67  *        the function resolver returns {@code null} for the function.
  68  *      </td>
  69  *    </tr>
  70  *    <tr>
  71  *      <td>QNames</td>
  72  *      <td>
  73  *        QNames in the expression are resolved against the XPath namespace context
  74  *        set with {@link #setNamespaceContext(NamespaceContext nsContext)}.
  75  *      </td>
  76  *    </tr>
  77  *    <tr>
  78  *      <td>result</td>
  79  *      <td>
  80  *        This result of evaluating an expression is converted to an instance of the desired return type.
  81  *        Valid return types are defined in {@link XPathConstants}.
  82  *        Conversion to the return type follows XPath conversion rules.
  83  *      </td>
  84  *    </tr>
  85  * </table>
  86  *
  87  * <p>An XPath object is not thread-safe and not reentrant.
  88  * In other words, it is the application's responsibility to make
  89  * sure that one {@link XPath} object is not used from
  90  * more than one thread at any given time, and while the {@code evaluate}
  91  * method is invoked, applications may not recursively call
  92  * the {@code evaluate} method.
  93  * <p>
  94  *
  95  * @author  <a href="Norman.Walsh@Sun.com">Norman Walsh</a>
  96  * @author  <a href="Jeff.Suttor@Sun.com">Jeff Suttor</a>
  97  * @see <a href="http://www.w3.org/TR/xpath">XML Path Language (XPath) Version 1.0</a>
  98  * @since 1.5
  99  */
 100 public interface XPath {
 101 
 102 
 103     /**
 104      * Reset this {@code XPath} to its original configuration.
 105      *
 106      * <p>{@code XPath} is reset to the same state as when it was created with
 107      * {@link XPathFactory#newXPath()}.
 108      * {@code reset()} is designed to allow the reuse of existing {@code XPath}s
 109      * thus saving resources associated with the creation of new {@code XPath}s.
 110      *
 111      * <p>The reset {@code XPath} is not guaranteed to have the same
 112      * {@link XPathFunctionResolver}, {@link XPathVariableResolver}
 113      * or {@link NamespaceContext} {@code Object}s, e.g. {@link Object#equals(Object obj)}.
 114      * It is guaranteed to have a functionally equal {@code XPathFunctionResolver},
 115      * {@code XPathVariableResolver} and {@code NamespaceContext}.
 116      */
 117     public void reset();
 118 
 119     /**
 120      * Establish a variable resolver.
 121      *
 122      * <p>A {@code NullPointerException} is thrown if {@code resolver} is {@code null}.
 123      *
 124      * @param resolver Variable resolver.
 125      *
 126      * @throws NullPointerException If {@code resolver} is {@code null}.
 127      */
 128     public void setXPathVariableResolver(XPathVariableResolver resolver);
 129 
 130     /**
 131        * Return the current variable resolver.
 132        *
 133        * <p>{@code null} is returned in no variable resolver is in effect.
 134        *
 135        * @return Current variable resolver.
 136        */
 137     public XPathVariableResolver getXPathVariableResolver();
 138 
 139     /**
 140        * Establish a function resolver.
 141        *
 142        * <p>A {@code NullPointerException} is thrown if {@code resolver} is {@code null}.
 143        *
 144        * @param resolver XPath function resolver.
 145        *
 146        * @throws NullPointerException If {@code resolver} is {@code null}.
 147        */
 148     public void setXPathFunctionResolver(XPathFunctionResolver resolver);
 149 
 150     /**
 151        * Return the current function resolver.
 152        * <p>
 153        * {@code null} is returned in no function resolver is in effect.
 154        *
 155        * @return Current function resolver.
 156        */
 157     public XPathFunctionResolver getXPathFunctionResolver();
 158 
 159     /**
 160        * Establish a namespace context.
 161        *
 162        * <p>A {@code NullPointerException} is thrown if {@code nsContext} is {@code null}.
 163        *
 164        * @param nsContext Namespace context to use.
 165        *
 166        * @throws NullPointerException If {@code nsContext} is {@code null}.
 167        */
 168     public void setNamespaceContext(NamespaceContext nsContext);
 169 
 170     /**
 171        * Return the current namespace context.
 172        *
 173        * <p>{@code null} is returned in no namespace context is in effect.
 174        *
 175        * @return Current Namespace context.
 176        */
 177     public NamespaceContext getNamespaceContext();
 178 
 179     /**
 180        * Compile an XPath expression for later evaluation.
 181        *
 182        * <p>If {@code expression} contains any {@link XPathFunction}s,
 183        * they must be available via the {@link XPathFunctionResolver}.
 184        * An {@link XPathExpressionException} will be thrown if the
 185        * {@code XPathFunction}
 186        * cannot be resovled with the {@code XPathFunctionResolver}.
 187        *
 188        * <p>If {@code expression} contains any variables, the
 189        * {@link XPathVariableResolver} in effect
 190        * <strong>at compile time</strong> will be used to resolve them.
 191        *
 192        * @param expression The XPath expression.
 193        *
 194        * @return Compiled XPath expression.
 195 
 196        * @throws XPathExpressionException If {@code expression} cannot be compiled.
 197        * @throws NullPointerException If {@code expression} is {@code null}.
 198        */
 199     public XPathExpression compile(String expression)
 200         throws XPathExpressionException;
 201 
 202     /**
 203      * Evaluate an {@code XPath} expression in the specified context and
 204      * return the result as the specified type.
 205      *
 206      * <p>
 207      * See <a href="#XPath-evaluation">Evaluation of XPath Expressions</a>
 208      * for context item evaluation, variable, function and {@code QName} resolution
 209      * and return type conversion.
 210      * <p>
 211      * The parameter {@code item} represents the context the XPath expression
 212      * will be operated on. The type of the context is implementation-dependent.
 213      * If the value is {@code null}, the operation must have no dependency on
 214      * the context, otherwise an XPathExpressionException will be thrown.
 215      *
 216      * @implNote
 217      * The type of the context is usually {@link org.w3c.dom.Node}.
 218      *
 219      * @param expression The XPath expression.
 220      * @param item The context the XPath expression will be evaluated in.
 221      * @param returnType The result type expected to be returned by the XPath expression.
 222      *
 223      * @return The result of evaluating an XPath expression as an {@code Object} of {@code returnType}.
 224      *
 225      * @throws XPathExpressionException If {@code expression} cannot be evaluated.
 226      * @throws IllegalArgumentException If {@code returnType} is not one of the types defined in {@link XPathConstants} (
 227      * {@link XPathConstants#NUMBER NUMBER},
 228      * {@link XPathConstants#STRING STRING},
 229      * {@link XPathConstants#BOOLEAN BOOLEAN},
 230      * {@link XPathConstants#NODE NODE} or
 231      * {@link XPathConstants#NODESET NODESET}).
 232      * @throws NullPointerException If {@code expression or returnType} is {@code null}.
 233      */
 234     public Object evaluate(String expression, Object item, QName returnType)
 235         throws XPathExpressionException;
 236 
 237     /**
 238      * Evaluate an XPath expression in the specified context and return the result as a {@code String}.
 239      *
 240      * <p>This method calls {@link #evaluate(String expression, Object item, QName returnType)} with a {@code returnType} of
 241      * {@link XPathConstants#STRING}.
 242      *
 243      * <p>See <a href="#XPath-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,
 244      * variable, function and QName resolution and return type conversion.
 245      *
 246      * <p>
 247      * The parameter {@code item} represents the context the XPath expression
 248      * will be operated on. The type of the context is implementation-dependent.
 249      * If the value is {@code null}, the operation must have no dependency on
 250      * the context, otherwise an XPathExpressionException will be thrown.
 251      *
 252      * @implNote
 253      * The type of the context is usually {@link org.w3c.dom.Node}.
 254      *
 255      * @param expression The XPath expression.
 256      * @param item The context the XPath expression will be evaluated in.
 257      *
 258      * @return The result of evaluating an XPath expression as a {@code String}.
 259      *
 260      * @throws XPathExpressionException If {@code expression} cannot be evaluated.
 261      * @throws NullPointerException If {@code expression} is {@code null}.
 262      */
 263     public String evaluate(String expression, Object item)
 264         throws XPathExpressionException;
 265 
 266     /**
 267      * Evaluate an XPath expression in the context of the specified {@code InputSource}
 268      * and return the result as the specified type.
 269      *
 270      * <p>This method builds a data model for the {@link InputSource} and calls
 271      * {@link #evaluate(String expression, Object item, QName returnType)} on the resulting document object.
 272      *
 273      * <p>See <a href="#XPath-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,
 274      * variable, function and QName resolution and return type conversion.
 275      *
 276      * @param expression The XPath expression.
 277      * @param source The input source of the document to evaluate over.
 278      * @param returnType The desired return type.
 279      *
 280      * @return The {@code Object} that encapsulates the result of evaluating the expression.
 281      *
 282      * @throws XPathExpressionException If expression cannot be evaluated.
 283      * @throws IllegalArgumentException If {@code returnType} is not one of the types defined in {@link XPathConstants}.
 284      * @throws NullPointerException If {@code expression, source or returnType} is {@code null}.
 285      */
 286     public Object evaluate(
 287         String expression,
 288         InputSource source,
 289         QName returnType)
 290         throws XPathExpressionException;
 291 
 292     /**
 293      * Evaluate an XPath expression in the context of the specified {@code InputSource}
 294      * and return the result as a {@code String}.
 295      *
 296      * <p>This method calls {@link #evaluate(String expression, InputSource source, QName returnType)} with a
 297      * {@code returnType} of {@link XPathConstants#STRING}.
 298      *
 299      * <p>See <a href="#XPath-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,
 300      * variable, function and QName resolution and return type conversion.
 301      *
 302      * @param expression The XPath expression.
 303      * @param source The {@code InputSource} of the document to evaluate over.
 304      *
 305      * @return The {@code String} that is the result of evaluating the expression and
 306      *   converting the result to a {@code String}.
 307      *
 308      * @throws XPathExpressionException If expression cannot be evaluated.
 309      * @throws NullPointerException If {@code expression or source} is {@code null}.
 310      */
 311     public String evaluate(String expression, InputSource source)
 312         throws XPathExpressionException;
 313 
 314     /**
 315      * Evaluate an XPath expression in the specified context and return
 316      * the result with the type specified through the {@code class type}
 317      *
 318      * <p>
 319      * The parameter {@code item} represents the context the XPath expression
 320      * will be operated on. The type of the context is implementation-dependent.
 321      * If the value is {@code null}, the operation must have no dependency on
 322      * the context, otherwise an XPathExpressionException will be thrown.
 323      *
 324      * @implNote
 325      * The type of the context is usually {@link org.w3c.dom.Node}.
 326      *
 327      * @implSpec
 328      * The default implementation in the XPath API is equivalent to:
 329      * <pre> {@code
 330      *     (T)evaluate(expression, item,
 331      *           XPathEvaluationResult.XPathResultType.getQNameType(type));
 332      * }</pre>
 333      *
 334      * Since the {@code evaluate} method does not support the
 335      * {@link XPathEvaluationResult.XPathResultType#ANY ANY} type, specifying
 336      * XPathEvaluationResult as the type will result in IllegalArgumentException.
 337      * Any implementation supporting the
 338      * {@link XPathEvaluationResult.XPathResultType#ANY ANY} type must override
 339      * this method.
 340      *
 341      * @param <T> The class type that will be returned by the XPath expression.
 342      * @param expression The XPath expression.
 343      * @param item The context the XPath expression will be evaluated in.
 344      * @param type The class type expected to be returned by the XPath expression.
 345      *
 346      * @return The result of evaluating the expression.
 347      *
 348      * @throws XPathExpressionException If the expression cannot be evaluated.
 349      * @throws IllegalArgumentException If {@code type} is not of the types
 350      * corresponding to the types defined in the {@link XPathEvaluationResult.XPathResultType},
 351      * or XPathEvaluationResult is specified as the type but an implementation supporting the
 352      * {@link XPathEvaluationResult.XPathResultType#ANY ANY} type is not available.
 353      * @throws NullPointerException If {@code expression or type} is {@code null}.
 354      *
 355      * @since 1.9
 356      */
 357     default <T>T evaluateExpression(String expression, Object item, Class<T> type)
 358         throws XPathExpressionException {
 359         return type.cast(evaluate(expression, item,
 360                 XPathEvaluationResult.XPathResultType.getQNameType(type)));
 361     }
 362 
 363     /**
 364      * Evaluate an XPath expression in the specified context. This is equivalent to
 365      * calling {@link #evaluateExpression(String expression, Object item, Class type)}
 366      * with type {@link XPathEvaluationResult}:
 367      * <pre> {@code
 368      *     evaluateExpression(expression, item, XPathEvaluationResult.class);
 369      * }</pre>
 370      * <p>
 371      * The parameter {@code item} represents the context the XPath expression
 372      * will be operated on. The type of the context is implementation-dependent.
 373      * If the value is {@code null}, the operation must have no dependency on
 374      * the context, otherwise an XPathExpressionException will be thrown.
 375      *
 376      * @implNote
 377      * The type of the context is usually {@link org.w3c.dom.Node}.
 378      *
 379      * @implSpec
 380      * The default implementation in the XPath API is equivalent to:
 381      * <pre> {@code
 382      *     evaluateExpression(expression, item, XPathEvaluationResult.class);
 383      * }</pre>
 384      *
 385      * Since the {@code evaluate} method does not support the
 386      * {@link XPathEvaluationResult.XPathResultType#ANY ANY}
 387      * type, the default implementation of this method will always throw an
 388      * IllegalArgumentException. Any implementation supporting the
 389      * {@link XPathEvaluationResult.XPathResultType#ANY ANY} type must therefore
 390      * override this method.
 391      *
 392      * @param expression The XPath expression.
 393      * @param item The context the XPath expression will be evaluated in.
 394      *
 395      * @return The result of evaluating the expression.
 396      *
 397      * @throws XPathExpressionException If the expression cannot be evaluated.
 398      * @throws IllegalArgumentException If the implementation of this method
 399      * does not support the
 400      * {@link XPathEvaluationResult.XPathResultType#ANY ANY} type.
 401      * @throws NullPointerException If {@code expression} is {@code null}.
 402      *
 403      * @since 1.9
 404      */
 405     default XPathEvaluationResult<?> evaluateExpression(String expression, Object item)
 406         throws XPathExpressionException
 407     {
 408         return evaluateExpression(expression, item, XPathEvaluationResult.class);
 409     }
 410 
 411     /**
 412      * Evaluate an XPath expression in the context of the specified {@code source}
 413      * and return the result as specified.
 414      * <p>
 415      * This method builds a data model for the {@link InputSource} and calls
 416      * {@link #evaluateExpression(String expression, Object item, Class type)}
 417      * on the resulting document object. The data model is usually
 418      * {@link org.w3c.dom.Document}
 419      *
 420      * @implSpec
 421      * The default implementation in the XPath API is equivalent to:
 422      * <pre> {@code
 423            (T)evaluate(expression, source,
 424                 XPathEvaluationResult.XPathResultType.getQNameType(type));
 425      * }</pre>
 426      *
 427      * Since the {@code evaluate} method does not support the
 428      * {@link XPathEvaluationResult.XPathResultType#ANY ANY} type, specifying
 429      * XPathEvaluationResult as the type will result in IllegalArgumentException.
 430      * Any implementation supporting the
 431      * {@link XPathEvaluationResult.XPathResultType#ANY ANY} type must override
 432      * this method.
 433      *
 434      * @param <T> The class type that will be returned by the XPath expression.
 435      * @param expression The XPath expression.
 436      * @param source The input source of the document to evaluate over.
 437      * @param type The class type expected to be returned by the XPath expression.
 438      *
 439      * @return The result of evaluating the expression.
 440      *
 441      * @throws XPathExpressionException If the expression cannot be evaluated.
 442      * @throws IllegalArgumentException If {@code type} is not of the types
 443      * corresponding to the types defined in the {@link XPathEvaluationResult.XPathResultType
 444      * XPathResultType}, or XPathEvaluationResult is specified as the type but an
 445      * implementation supporting the
 446      * {@link XPathEvaluationResult.XPathResultType#ANY ANY} type is not available.
 447      * @throws NullPointerException If {@code expression, source or type}is {@code null}.
 448      *
 449      * @since 1.9
 450      */
 451     default <T>T evaluateExpression(String expression, InputSource source, Class<T> type)
 452         throws XPathExpressionException
 453     {
 454         return type.cast(evaluate(expression, source,
 455                 XPathEvaluationResult.XPathResultType.getQNameType(type)));
 456     }
 457 
 458     /**
 459      * Evaluate an XPath expression in the specified context. This is equivalent to
 460      * calling {@link #evaluateExpression(String expression, Object item, Class type)}
 461      * with type {@link XPathEvaluationResult}:
 462      * <pre> {@code
 463      *     evaluateExpression(expression, item, XPathEvaluationResult.class);
 464      * }</pre>
 465      * <p>
 466      *
 467      * @implSpec
 468      * The default implementation in the XPath API is equivalent to:
 469      * <pre> {@code
 470      *     evaluateExpression(expression, source, XPathEvaluationResult.class);
 471      * }</pre>
 472      *
 473      * Since the {@code evaluate} method does not support the
 474      * {@link XPathEvaluationResult.XPathResultType#ANY ANY}
 475      * type, the default implementation of this method will always throw an
 476      * IllegalArgumentException. Any implementation supporting the
 477      * {@link XPathEvaluationResult.XPathResultType#ANY ANY} type must therefore
 478      * override this method.
 479      *
 480      * @param expression The XPath expression.
 481      * @param source The input source of the document to evaluate over.
 482      *
 483      * @return The result of evaluating the expression.
 484      *
 485      * @throws XPathExpressionException If the expression cannot be evaluated.
 486      * @throws IllegalArgumentException If the implementation of this method
 487      * does not support the
 488      * {@link XPathEvaluationResult.XPathResultType#ANY ANY} type.
 489      * @throws NullPointerException If {@code expression or source} is {@code null}.
 490      *
 491      * @since 1.9
 492      */
 493     default XPathEvaluationResult<?> evaluateExpression(String expression, InputSource source)
 494         throws XPathExpressionException
 495     {
 496         return evaluateExpression(expression, source, XPathEvaluationResult.class);
 497     }
 498 }