Path: blob/master/src/java.xml/share/classes/javax/xml/xpath/XPathExpression.java
40948 views
/*1* Copyright (c) 2003, 2021, 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 javax.xml.namespace.QName;28import org.xml.sax.InputSource;2930/**31* {@code XPathExpression} provides access to compiled XPath expressions.32* The XPath evaluation is affected by the factors described in the following table.33*34* <a id="XPathExpression-evaluation"></a>35* <table class="striped">36* <caption>Evaluation of XPath Expressions</caption>37* <thead>38* <tr>39* <th scope="col">Factor</th>40* <th scope="col">Behavior</th>41* </tr>42* </thead>43* <tbody>44* <tr>45* <th scope="row">context</th>46* <td>47* The type of the context is implementation-dependent. If the value is48* null, the operation must have no dependency on the context, otherwise49* an XPathExpressionException will be thrown.50*51* For the purposes of evaluating XPath expressions, a DocumentFragment52* is treated like a Document node.53* </td>54* </tr>55* <tr>56* <th scope="row">variables</th>57* <td>58* If the expression contains a variable reference, its value will be found through the {@link XPathVariableResolver}.59* An {@link XPathExpressionException} is raised if the variable resolver is undefined or60* the resolver returns {@code null} for the variable.61* The value of a variable must be immutable through the course of any single evaluation.62* </td>63* </tr>64* <tr>65* <th scope="row">functions</th>66* <td>67* If the expression contains a function reference, the function will be found through the {@link XPathFunctionResolver}.68* An {@link XPathExpressionException} is raised if the function resolver is undefined or69* the function resolver returns {@code null} for the function.70* </td>71* </tr>72* <tr>73* <th scope="row">QNames</th>74* <td>75* QNames in the expression are resolved against the XPath namespace context.76* </td>77* </tr>78* <tr>79* <th scope="row">result</th>80* <td>81* This result of evaluating an expression is converted to an instance of the desired return type.82* Valid return types are defined in {@link XPathConstants}.83* Conversion to the return type follows XPath conversion rules.84* </td>85* </tr>86* </tbody>87* </table>88*89* <p>An XPath expression is not thread-safe and not reentrant.90* In other words, it is the application's responsibility to make91* sure that one {@link XPathExpression} object is not used from92* more than one thread at any given time, and while the {@code evaluate}93* method is invoked, applications may not recursively call94* the {@code evaluate} method.95*96* @author Norman Walsh97* @author Jeff Suttor98* @see <a href="http://www.w3.org/TR/xpath#section-Expressions">XML Path Language (XPath) Version 1.0, Expressions</a>99* @since 1.5100*/101public interface XPathExpression {102103104/**105* Evaluate the compiled XPath expression in the specified context and return the result as the specified type.106*107* <p>See <a href="#XPathExpression-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,108* variable, function and QName resolution and return type conversion.109*110* <p>111* The parameter {@code item} represents the context the XPath expression112* will be operated on. The type of the context is implementation-dependent.113* If the value is {@code null}, the operation must have no dependency on114* the context, otherwise an XPathExpressionException will be thrown.115*116* @implNote117* The type of the context is usually {@link org.w3c.dom.Node}.118*119* @param item The context the XPath expression will be evaluated in.120* @param returnType The result type expected to be returned by the XPath expression.121*122* @return The {@code Object} that is the result of evaluating the expression and converting the result to123* {@code returnType}.124*125* @throws XPathExpressionException If the expression cannot be evaluated.126* @throws IllegalArgumentException If {@code returnType} is not one of the types defined in {@link XPathConstants}.127* @throws NullPointerException If {@code returnType} is {@code null}.128*/129public Object evaluate(Object item, QName returnType)130throws XPathExpressionException;131132/**133* Evaluate the compiled XPath expression in the specified context and return the result as a {@code String}.134*135* <p>This method calls {@link #evaluate(Object item, QName returnType)} with a {@code returnType} of136* {@link XPathConstants#STRING}.137*138* <p>See <a href="#XPathExpression-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,139* variable, function and QName resolution and return type conversion.140*141* <p>142* The parameter {@code item} represents the context the XPath expression143* will be operated on. The type of the context is implementation-dependent.144* If the value is {@code null}, the operation must have no dependency on145* the context, otherwise an XPathExpressionException will be thrown.146*147* @implNote148* The type of the context is usually {@link org.w3c.dom.Node}.149*150* @param item The context the XPath expression will be evaluated in.151*152* @return The result of evaluating an XPath expression as a {@code String}.153*154* @throws XPathExpressionException If the expression cannot be evaluated.155*/156public String evaluate(Object item)157throws XPathExpressionException;158159/**160* Evaluate the compiled XPath expression in the context161* of the specified {@code InputSource} and return the result as the162* specified type.163*164* <p>This method builds a data model for the {@link InputSource} and calls165* {@link #evaluate(Object item, QName returnType)} on the resulting document object.166*167* <p>See <a href="#XPathExpression-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,168* variable, function and QName resolution and return type conversion.169*170* <p>If {@code returnType} is not one of the types defined in {@link XPathConstants},171* then an {@code IllegalArgumentException} is thrown.172*173* <p>If {@code source} or {@code returnType} is {@code null},174* then a {@code NullPointerException} is thrown.175*176* @param source The {@code InputSource} of the document to evaluate over.177* @param returnType The desired return type.178*179* @return The {@code Object} that is the result of evaluating the expression and converting the result to180* {@code returnType}.181*182* @throws XPathExpressionException If the expression cannot be evaluated.183* @throws IllegalArgumentException If {@code returnType} is not one of the types defined in {@link XPathConstants}.184* @throws NullPointerException If {@code source or returnType} is {@code null}.185*/186public Object evaluate(InputSource source, QName returnType)187throws XPathExpressionException;188189/**190* Evaluate the compiled XPath expression in the context191* of the specified {@code InputSource} and return the result as a192* {@code String}.193*194* <p>This method calls {@link #evaluate(InputSource source, QName returnType)} with a {@code returnType} of195* {@link XPathConstants#STRING}.196*197* <p>See <a href="#XPathExpression-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,198* variable, function and QName resolution and return type conversion.199*200* <p>If {@code source} is {@code null}, then a {@code NullPointerException} is thrown.201*202* @param source The {@code InputSource} of the document to evaluate over.203*204* @return The {@code String} that is the result of evaluating the expression and converting the result to a205* {@code String}.206*207* @throws XPathExpressionException If the expression cannot be evaluated.208* @throws NullPointerException If {@code source} is {@code null}.209*/210public String evaluate(InputSource source)211throws XPathExpressionException;212213/**214* Evaluate the compiled XPath expression in the specified context, and return215* the result with the type specified through the {@code class type}.216*217* <p>218* The parameter {@code item} represents the context the XPath expression219* will be operated on. The type of the context is implementation-dependent.220* If the value is {@code null}, the operation must have no dependency on221* the context, otherwise an XPathExpressionException will be thrown.222*223* @implNote224* The type of the context is usually {@link org.w3c.dom.Node}.225*226* @implSpec227* The default implementation in the XPath API is equivalent to:228* <pre> {@code229* (T)evaluate(item, XPathEvaluationResult.XPathResultType.getQNameType(type));230* }</pre>231*232* Since the {@code evaluate} method does not support the233* {@link XPathEvaluationResult.XPathResultType#ANY ANY} type, specifying234* XPathEvaluationResult as the type will result in IllegalArgumentException.235* Any implementation supporting the236* {@link XPathEvaluationResult.XPathResultType#ANY ANY} type must override237* this method.238*239* @param <T> The class type that will be returned by the XPath expression.240* @param item The context the XPath expression will be evaluated in.241* @param type The class type expected to be returned by the XPath expression,242* must be one of the types described in section243* <a href="package-summary.html#XPath.Datatypes.Class">3.2 Class types</a>244* in the package summary.245*246* @return The result of evaluating the expression.247*248* @throws XPathExpressionException If the expression cannot be evaluated.249* @throws IllegalArgumentException If {@code type} is not of the types250* corresponding to the types defined in the {@link XPathEvaluationResult.XPathResultType251* XPathResultType}, or XPathEvaluationResult is specified as the type but an252* implementation supporting the253* {@link XPathEvaluationResult.XPathResultType#ANY ANY} type is not available.254* @throws NullPointerException If {@code type} is {@code null}.255*256* @since 9257*/258default <T>T evaluateExpression(Object item, Class<T> type)259throws XPathExpressionException260{261return type.cast(evaluate(item, XPathEvaluationResult.XPathResultType.getQNameType(type)));262}263264/**265* Evaluate the compiled XPath expression in the specified context. This is266* equivalent to calling {@link #evaluateExpression(Object item, Class type)}267* with type {@link XPathEvaluationResult}:268* <pre> {@code269* evaluateExpression(item, XPathEvaluationResult.class);270* }</pre>271* <p>272* The parameter {@code item} represents the context the XPath expression273* will be operated on. The type of the context is implementation-dependent.274* If the value is {@code null}, the operation must have no dependency on275* the context, otherwise an XPathExpressionException will be thrown.276*277* @implNote278* The type of the context is usually {@link org.w3c.dom.Node}.279*280* @implSpec281* The default implementation in the XPath API is equivalent to:282* <pre> {@code283* evaluateExpression(item, XPathEvaluationResult.class);284* }</pre>285*286* Since the {@code evaluate} method does not support the287* {@link XPathEvaluationResult.XPathResultType#ANY ANY}288* type, the default implementation of this method will always throw an289* IllegalArgumentException. Any implementation supporting the290* {@link XPathEvaluationResult.XPathResultType#ANY ANY} type must therefore291* override this method.292*293* @param item The context the XPath expression will be evaluated in.294*295* @return The result of evaluating the expression.296*297* @throws XPathExpressionException If the expression cannot be evaluated.298* @throws IllegalArgumentException If the implementation of this method299* does not support the300* {@link XPathEvaluationResult.XPathResultType#ANY ANY} type.301*302* @since 9303*/304default XPathEvaluationResult<?> evaluateExpression(Object item)305throws XPathExpressionException306{307return evaluateExpression(item, XPathEvaluationResult.class);308}309310/**311* Evaluate the compiled XPath expression in the specified context,312* and return the result with the type specified through the {@code class type}313* <p>314* This method builds a data model for the {@link InputSource} and calls315* {@link #evaluateExpression(Object item, Class type)} on the resulting316* document object.317* <P>318* By default, the JDK's data model is {@link org.w3c.dom.Document}.319*320* @implSpec321* The default implementation in the XPath API is equivalent to:322* <pre> {@code323(T)evaluate(source, XPathEvaluationResult.XPathResultType.getQNameType(type));324* }</pre>325*326* Since the {@code evaluate} method does not support the327* {@link XPathEvaluationResult.XPathResultType#ANY ANY} type, specifying328* XPathEvaluationResult as the type will result in IllegalArgumentException.329* Any implementation supporting the330* {@link XPathEvaluationResult.XPathResultType#ANY ANY} type must override331* this method.332*333* @param <T> The class type that will be returned by the XPath expression.334* @param source The {@code InputSource} of the document to evaluate over.335* @param type The class type expected to be returned by the XPath expression,336* must be one of the types described in section337* <a href="package-summary.html#XPath.Datatypes.Class">3.2 Class types</a>338* in the package summary.339*340* @return The result of evaluating the expression.341*342* @throws XPathExpressionException If the expression cannot be evaluated.343* @throws IllegalArgumentException If {@code type} is not of the types344* corresponding to the types defined in the {@link XPathEvaluationResult.XPathResultType345* XPathResultType}, or XPathEvaluationResult is specified as the type but an346* implementation supporting the347* {@link XPathEvaluationResult.XPathResultType#ANY ANY} type348* is not available.349* @throws NullPointerException If {@code source or type} is {@code null}.350*351* @since 9352*/353default <T>T evaluateExpression(InputSource source, Class<T> type)354throws XPathExpressionException355{356return type.cast(evaluate(source, XPathEvaluationResult.XPathResultType.getQNameType(type)));357}358359/**360* Evaluate the compiled XPath expression in the specified context. This is361* equivalent to calling {@link #evaluateExpression(InputSource source, Class type)}362* with type {@link XPathEvaluationResult}:363* <pre> {@code364* evaluateExpression(source, XPathEvaluationResult.class);365* }</pre>366*367* @implSpec368* The default implementation in the XPath API is equivalent to:369* <pre> {@code370* (XPathEvaluationResult)evaluateExpression(source, XPathEvaluationResult.class);371* }</pre>372*373* Since the {@code evaluate} method does not support the374* {@link XPathEvaluationResult.XPathResultType#ANY ANY}375* type, the default implementation of this method will always throw an376* IllegalArgumentException. Any implementation supporting the377* {@link XPathEvaluationResult.XPathResultType#ANY ANY} type must therefore378* override this method.379*380* @param source The {@code InputSource} of the document to evaluate over.381*382* @return The result of evaluating the expression.383*384* @throws XPathExpressionException If the expression cannot be evaluated.385* @throws IllegalArgumentException If the implementation of this method386* does not support the387* {@link XPathEvaluationResult.XPathResultType#ANY ANY} type.388* @throws NullPointerException If {@code source} is {@code null}.389*390* @since 9391*/392default XPathEvaluationResult<?> evaluateExpression(InputSource source)393throws XPathExpressionException394{395return evaluateExpression(source, XPathEvaluationResult.class);396}397}398399400