Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxp/src/javax/xml/xpath/XPathExpression.java
32285 views
/*1* Copyright (c) 2003, 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.xml.xpath;2627import org.xml.sax.InputSource;28import javax.xml.namespace.QName;2930/**31* <p><code>XPathExpression</code> provides access to compiled XPath expressions.</p>32*33* <a name="XPathExpression-evaluation"/>34* <table border="1" cellpadding="2">35* <thead>36* <tr>37* <th colspan="2">Evaluation of XPath Expressions.</th>38* </tr>39* </thead>40* <tbody>41* <tr>42* <td>context</td>43* <td>44* If a request is made to evaluate the expression in the absence45* of a context item, an empty document node will be used for the context.46* For the purposes of evaluating XPath expressions, a DocumentFragment47* is treated like a Document node.48* </td>49* </tr>50* <tr>51* <td>variables</td>52* <td>53* If the expression contains a variable reference, its value will be found through the {@link XPathVariableResolver}.54* An {@link XPathExpressionException} is raised if the variable resolver is undefined or55* the resolver returns <code>null</code> for the variable.56* The value of a variable must be immutable through the course of any single evaluation.</p>57* </td>58* </tr>59* <tr>60* <td>functions</td>61* <td>62* If the expression contains a function reference, the function will be found through the {@link XPathFunctionResolver}.63* An {@link XPathExpressionException} is raised if the function resolver is undefined or64* the function resolver returns <code>null</code> for the function.</p>65* </td>66* </tr>67* <tr>68* <td>QNames</td>69* <td>70* QNames in the expression are resolved against the XPath namespace context.71* </td>72* </tr>73* <tr>74* <td>result</td>75* <td>76* This result of evaluating an expression is converted to an instance of the desired return type.77* Valid return types are defined in {@link XPathConstants}.78* Conversion to the return type follows XPath conversion rules.</p>79* </td>80* </tr>81* </table>82*83* <p>An XPath expression is not thread-safe and not reentrant.84* In other words, it is the application's responsibility to make85* sure that one {@link XPathExpression} object is not used from86* more than one thread at any given time, and while the <code>evaluate</code>87* method is invoked, applications may not recursively call88* the <code>evaluate</code> method.89* <p>90*91* @author <a href="mailto:[email protected]">Norman Walsh</a>92* @author <a href="mailto:[email protected]">Jeff Suttor</a>93* @see <a href="http://www.w3.org/TR/xpath#section-Expressions">XML Path Language (XPath) Version 1.0, Expressions</a>94* @since 1.595*/96public interface XPathExpression {9798/**99* <p>Evaluate the compiled XPath expression in the specified context and return the result as the specified type.</p>100*101* <p>See <a href="#XPathExpression-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,102* variable, function and QName resolution and return type conversion.</p>103*104* <p>If <code>returnType</code> is not one of the types defined in {@link XPathConstants},105* then an <code>IllegalArgumentException</code> is thrown.</p>106*107* <p>If a <code>null</code> value is provided for108* <code>item</code>, an empty document will be used for the109* context.110* If <code>returnType</code> is <code>null</code>, then a <code>NullPointerException</code> is thrown.</p>111*112* @param item The starting context (a node, for example).113* @param returnType The desired return type.114*115* @return The <code>Object</code> that is the result of evaluating the expression and converting the result to116* <code>returnType</code>.117*118* @throws XPathExpressionException If the expression cannot be evaluated.119* @throws IllegalArgumentException If <code>returnType</code> is not one of the types defined in {@link XPathConstants}.120* @throws NullPointerException If <code>returnType</code> is <code>null</code>.121*/122public Object evaluate(Object item, QName returnType)123throws XPathExpressionException;124125/**126* <p>Evaluate the compiled XPath expression in the specified context and return the result as a <code>String</code>.</p>127*128* <p>This method calls {@link #evaluate(Object item, QName returnType)} with a <code>returnType</code> of129* {@link XPathConstants#STRING}.</p>130*131* <p>See <a href="#XPathExpression-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,132* variable, function and QName resolution and return type conversion.</p>133*134* <p>If a <code>null</code> value is provided for135* <code>item</code>, an empty document will be used for the136* context.137*138* @param item The starting context (a node, for example).139*140* @return The <code>String</code> that is the result of evaluating the expression and converting the result to a141* <code>String</code>.142*143* @throws XPathExpressionException If the expression cannot be evaluated.144*/145public String evaluate(Object item)146throws XPathExpressionException;147148/**149* <p>Evaluate the compiled XPath expression in the context of the specified <code>InputSource</code> and return the result as the150* specified type.</p>151*152* <p>This method builds a data model for the {@link InputSource} and calls153* {@link #evaluate(Object item, QName returnType)} on the resulting document object.</p>154*155* <p>See <a href="#XPathExpression-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,156* variable, function and QName resolution and return type conversion.</p>157*158* <p>If <code>returnType</code> is not one of the types defined in {@link XPathConstants},159* then an <code>IllegalArgumentException</code> is thrown.</p>160*161* <p>If <code>source</code> or <code>returnType</code> is <code>null</code>,162* then a <code>NullPointerException</code> is thrown.</p>163*164* @param source The <code>InputSource</code> of the document to evaluate over.165* @param returnType The desired return type.166*167* @return The <code>Object</code> that is the result of evaluating the expression and converting the result to168* <code>returnType</code>.169*170* @throws XPathExpressionException If the expression cannot be evaluated.171* @throws IllegalArgumentException If <code>returnType</code> is not one of the types defined in {@link XPathConstants}.172* @throws NullPointerException If <code>source</code> or <code>returnType</code> is <code>null</code>.173*/174public Object evaluate(InputSource source, QName returnType)175throws XPathExpressionException;176177/**178* <p>Evaluate the compiled XPath expression in the context of the specified <code>InputSource</code> and return the result as a179* <code>String</code>.</p>180*181* <p>This method calls {@link #evaluate(InputSource source, QName returnType)} with a <code>returnType</code> of182* {@link XPathConstants#STRING}.</p>183*184* <p>See <a href="#XPathExpression-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,185* variable, function and QName resolution and return type conversion.</p>186*187* <p>If <code>source</code> is <code>null</code>, then a <code>NullPointerException</code> is thrown.</p>188*189* @param source The <code>InputSource</code> of the document to evaluate over.190*191* @return The <code>String</code> that is the result of evaluating the expression and converting the result to a192* <code>String</code>.193*194* @throws XPathExpressionException If the expression cannot be evaluated.195* @throws NullPointerException If <code>source</code> is <code>null</code>.196*/197public String evaluate(InputSource source)198throws XPathExpressionException;199}200201202