Path: blob/master/src/java.xml/share/classes/javax/xml/xpath/XPath.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.NamespaceContext;28import javax.xml.namespace.QName;29import org.xml.sax.InputSource;3031/**32* {@code XPath} provides access to the XPath evaluation environment and expressions.33* The XPath evaluation is affected by the factors described in the following table.34*35* <a id="XPath-evaluation"></a>36* <table class="striped">37* <caption>Evaluation of XPath Expressions</caption>38* <thead>39* <tr>40* <th scope="col">Factor</th>41* <th scope="col">Behavior</th>42* </tr>43* </thead>44* <tbody>45* <tr>46* <th scope="row">context</th>47* <td>48* The type of the context is implementation-dependent. If the value is49* null, the operation must have no dependency on the context, otherwise50* an XPathExpressionException will be thrown.51*52* For the purposes of evaluating XPath expressions, a DocumentFragment53* is treated like a Document node.54* </td>55* </tr>56* <tr>57* <th scope="row">variables</th>58* <td>59* If the expression contains a variable reference, its value will be found through the {@link XPathVariableResolver}60* set with {@link #setXPathVariableResolver(XPathVariableResolver resolver)}.61* An {@link XPathExpressionException} is raised if the variable resolver is undefined or62* the resolver returns {@code null} for the variable.63* The value of a variable must be immutable through the course of any single evaluation.64* </td>65* </tr>66* <tr>67* <th scope="row">functions</th>68* <td>69* If the expression contains a function reference, the function will be found through the {@link XPathFunctionResolver}70* set with {@link #setXPathFunctionResolver(XPathFunctionResolver resolver)}.71* An {@link XPathExpressionException} is raised if the function resolver is undefined or72* the function resolver returns {@code null} for the function.73* </td>74* </tr>75* <tr>76* <th scope="row">QNames</th>77* <td>78* QNames in the expression are resolved against the XPath namespace context79* set with {@link #setNamespaceContext(NamespaceContext nsContext)}.80* </td>81* </tr>82* <tr>83* <th scope="row">result</th>84* <td>85* This result of evaluating an expression is converted to an instance of the desired return type.86* Valid return types are defined in {@link XPathConstants}.87* Conversion to the return type follows XPath conversion rules.88* </td>89* </tr>90* </tbody>91* </table>92*93* <p>An XPath object is not thread-safe and not reentrant.94* In other words, it is the application's responsibility to make95* sure that one {@link XPath} object is not used from96* more than one thread at any given time, and while the {@code evaluate}97* method is invoked, applications may not recursively call98* the {@code evaluate} method.99*100* @author Norman Walsh101* @author Jeff Suttor102* @see <a href="http://www.w3.org/TR/xpath">XML Path Language (XPath) Version 1.0</a>103* @since 1.5104*/105public interface XPath {106107108/**109* Reset this {@code XPath} to its original configuration.110*111* <p>{@code XPath} is reset to the same state as when it was created with112* {@link XPathFactory#newXPath()}.113* {@code reset()} is designed to allow the reuse of existing {@code XPath}s114* thus saving resources associated with the creation of new {@code XPath}s.115*116* <p>The reset {@code XPath} is not guaranteed to have the same117* {@link XPathFunctionResolver}, {@link XPathVariableResolver}118* or {@link NamespaceContext} {@code Object}s, e.g. {@link Object#equals(Object obj)}.119* It is guaranteed to have a functionally equal {@code XPathFunctionResolver},120* {@code XPathVariableResolver} and {@code NamespaceContext}.121*/122public void reset();123124/**125* Establish a variable resolver.126*127* <p>A {@code NullPointerException} is thrown if {@code resolver} is {@code null}.128*129* @param resolver Variable resolver.130*131* @throws NullPointerException If {@code resolver} is {@code null}.132*/133public void setXPathVariableResolver(XPathVariableResolver resolver);134135/**136* Return the current variable resolver.137*138* <p>{@code null} is returned in no variable resolver is in effect.139*140* @return Current variable resolver.141*/142public XPathVariableResolver getXPathVariableResolver();143144/**145* Establish a function resolver.146*147* <p>A {@code NullPointerException} is thrown if {@code resolver} is {@code null}.148*149* @param resolver XPath function resolver.150*151* @throws NullPointerException If {@code resolver} is {@code null}.152*/153public void setXPathFunctionResolver(XPathFunctionResolver resolver);154155/**156* Return the current function resolver.157* <p>158* {@code null} is returned in no function resolver is in effect.159*160* @return Current function resolver.161*/162public XPathFunctionResolver getXPathFunctionResolver();163164/**165* Establish a namespace context.166*167* <p>A {@code NullPointerException} is thrown if {@code nsContext} is {@code null}.168*169* @param nsContext Namespace context to use.170*171* @throws NullPointerException If {@code nsContext} is {@code null}.172*/173public void setNamespaceContext(NamespaceContext nsContext);174175/**176* Return the current namespace context.177*178* <p>{@code null} is returned in no namespace context is in effect.179*180* @return Current Namespace context.181*/182public NamespaceContext getNamespaceContext();183184/**185* Compile an XPath expression for later evaluation.186*187* <p>If {@code expression} contains any {@link XPathFunction}s,188* they must be available via the {@link XPathFunctionResolver}.189* An {@link XPathExpressionException} will be thrown if the190* {@code XPathFunction}191* cannot be resovled with the {@code XPathFunctionResolver}.192*193* <p>If {@code expression} contains any variables, the194* {@link XPathVariableResolver} in effect195* <strong>at compile time</strong> will be used to resolve them.196*197* @param expression The XPath expression.198*199* @return Compiled XPath expression.200201* @throws XPathExpressionException If {@code expression} cannot be compiled.202* @throws NullPointerException If {@code expression} is {@code null}.203*/204public XPathExpression compile(String expression)205throws XPathExpressionException;206207/**208* Evaluate an {@code XPath} expression in the specified context and209* return the result as the specified type.210*211* <p>212* See <a href="#XPath-evaluation">Evaluation of XPath Expressions</a>213* for context item evaluation, variable, function and {@code QName} resolution214* and return type conversion.215* <p>216* The parameter {@code item} represents the context the XPath expression217* will be operated on. The type of the context is implementation-dependent.218* If the value is {@code null}, the operation must have no dependency on219* the context, otherwise an XPathExpressionException will be thrown.220*221* @implNote222* The type of the context is usually {@link org.w3c.dom.Node}.223*224* @param expression The XPath expression.225* @param item The context the XPath expression will be evaluated in.226* @param returnType The result type expected to be returned by the XPath expression.227*228* @return The result of evaluating an XPath expression as an {@code Object} of {@code returnType}.229*230* @throws XPathExpressionException If {@code expression} cannot be evaluated.231* @throws IllegalArgumentException If {@code returnType} is not one of the types defined in {@link XPathConstants} (232* {@link XPathConstants#NUMBER NUMBER},233* {@link XPathConstants#STRING STRING},234* {@link XPathConstants#BOOLEAN BOOLEAN},235* {@link XPathConstants#NODE NODE} or236* {@link XPathConstants#NODESET NODESET}).237* @throws NullPointerException If {@code expression or returnType} is {@code null}.238*/239public Object evaluate(String expression, Object item, QName returnType)240throws XPathExpressionException;241242/**243* Evaluate an XPath expression in the specified context and return the result as a {@code String}.244*245* <p>This method calls {@link #evaluate(String expression, Object item, QName returnType)} with a {@code returnType} of246* {@link XPathConstants#STRING}.247*248* <p>See <a href="#XPath-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,249* variable, function and QName resolution and return type conversion.250*251* <p>252* The parameter {@code item} represents the context the XPath expression253* will be operated on. The type of the context is implementation-dependent.254* If the value is {@code null}, the operation must have no dependency on255* the context, otherwise an XPathExpressionException will be thrown.256*257* @implNote258* The type of the context is usually {@link org.w3c.dom.Node}.259*260* @param expression The XPath expression.261* @param item The context the XPath expression will be evaluated in.262*263* @return The result of evaluating an XPath expression as a {@code String}.264*265* @throws XPathExpressionException If {@code expression} cannot be evaluated.266* @throws NullPointerException If {@code expression} is {@code null}.267*/268public String evaluate(String expression, Object item)269throws XPathExpressionException;270271/**272* Evaluate an XPath expression in the context of the specified {@code InputSource}273* and return the result as the specified type.274*275* <p>This method builds a data model for the {@link InputSource} and calls276* {@link #evaluate(String expression, Object item, QName returnType)} on the resulting document object.277*278* <p>See <a href="#XPath-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,279* variable, function and QName resolution and return type conversion.280*281* @param expression The XPath expression.282* @param source The input source of the document to evaluate over.283* @param returnType The desired return type.284*285* @return The {@code Object} that encapsulates the result of evaluating the expression.286*287* @throws XPathExpressionException If expression cannot be evaluated.288* @throws IllegalArgumentException If {@code returnType} is not one of the types defined in {@link XPathConstants}.289* @throws NullPointerException If {@code expression, source or returnType} is {@code null}.290*/291public Object evaluate(292String expression,293InputSource source,294QName returnType)295throws XPathExpressionException;296297/**298* Evaluate an XPath expression in the context of the specified {@code InputSource}299* and return the result as a {@code String}.300*301* <p>This method calls {@link #evaluate(String expression, InputSource source, QName returnType)} with a302* {@code returnType} of {@link XPathConstants#STRING}.303*304* <p>See <a href="#XPath-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,305* variable, function and QName resolution and return type conversion.306*307* @param expression The XPath expression.308* @param source The {@code InputSource} of the document to evaluate over.309*310* @return The {@code String} that is the result of evaluating the expression and311* converting the result to a {@code String}.312*313* @throws XPathExpressionException If expression cannot be evaluated.314* @throws NullPointerException If {@code expression or source} is {@code null}.315*/316public String evaluate(String expression, InputSource source)317throws XPathExpressionException;318319/**320* Evaluate an XPath expression in the specified context and return321* the result with the type specified through the {@code class type}322*323* <p>324* The parameter {@code item} represents the context the XPath expression325* will be operated on. The type of the context is implementation-dependent.326* If the value is {@code null}, the operation must have no dependency on327* the context, otherwise an XPathExpressionException will be thrown.328*329* @implNote330* The type of the context is usually {@link org.w3c.dom.Node}.331*332* @implSpec333* The default implementation in the XPath API is equivalent to:334* <pre> {@code335* (T)evaluate(expression, item,336* XPathEvaluationResult.XPathResultType.getQNameType(type));337* }</pre>338*339* Since the {@code evaluate} method does not support the340* {@link XPathEvaluationResult.XPathResultType#ANY ANY} type, specifying341* XPathEvaluationResult as the type will result in IllegalArgumentException.342* Any implementation supporting the343* {@link XPathEvaluationResult.XPathResultType#ANY ANY} type must override344* this method.345*346* @param <T> The class type that will be returned by the XPath expression.347* @param expression The XPath expression.348* @param item The context the XPath expression will be evaluated in.349* @param type The class type expected to be returned by the XPath expression,350* must be one of the types described in section351* <a href="package-summary.html#XPath.Datatypes.Class">3.2 Class types</a>352* in the package summary.353*354*355* @return The result of evaluating the expression.356*357* @throws XPathExpressionException If the expression cannot be evaluated.358* @throws IllegalArgumentException If {@code type} is not of the types359* corresponding to the types defined in the {@link XPathEvaluationResult.XPathResultType},360* or XPathEvaluationResult is specified as the type but an implementation supporting the361* {@link XPathEvaluationResult.XPathResultType#ANY ANY} type is not available.362* @throws NullPointerException If {@code expression or type} is {@code null}.363*364* @since 9365*/366default <T>T evaluateExpression(String expression, Object item, Class<T> type)367throws XPathExpressionException {368return type.cast(evaluate(expression, item,369XPathEvaluationResult.XPathResultType.getQNameType(type)));370}371372/**373* Evaluate an XPath expression in the specified context. This is equivalent to374* calling {@link #evaluateExpression(String expression, Object item, Class type)}375* with type {@link XPathEvaluationResult}:376* <pre> {@code377* evaluateExpression(expression, item, XPathEvaluationResult.class);378* }</pre>379* <p>380* The parameter {@code item} represents the context the XPath expression381* will be operated on. The type of the context is implementation-dependent.382* If the value is {@code null}, the operation must have no dependency on383* the context, otherwise an XPathExpressionException will be thrown.384*385* @implNote386* The type of the context is usually {@link org.w3c.dom.Node}.387*388* @implSpec389* The default implementation in the XPath API is equivalent to:390* <pre> {@code391* evaluateExpression(expression, item, XPathEvaluationResult.class);392* }</pre>393*394* Since the {@code evaluate} method does not support the395* {@link XPathEvaluationResult.XPathResultType#ANY ANY}396* type, the default implementation of this method will always throw an397* IllegalArgumentException. Any implementation supporting the398* {@link XPathEvaluationResult.XPathResultType#ANY ANY} type must therefore399* override this method.400*401* @param expression The XPath expression.402* @param item The context the XPath expression will be evaluated in.403*404* @return The result of evaluating the expression.405*406* @throws XPathExpressionException If the expression cannot be evaluated.407* @throws IllegalArgumentException If the implementation of this method408* does not support the409* {@link XPathEvaluationResult.XPathResultType#ANY ANY} type.410* @throws NullPointerException If {@code expression} is {@code null}.411*412* @since 9413*/414default XPathEvaluationResult<?> evaluateExpression(String expression, Object item)415throws XPathExpressionException416{417return evaluateExpression(expression, item, XPathEvaluationResult.class);418}419420/**421* Evaluate an XPath expression in the context of the specified {@code source}422* and return the result as specified.423* <p>424* This method builds a data model for the {@link InputSource} and calls425* {@link #evaluateExpression(String expression, Object item, Class type)}426* on the resulting document object. The data model is usually427* {@link org.w3c.dom.Document}428*429* @implSpec430* The default implementation in the XPath API is equivalent to:431* <pre> {@code432(T)evaluate(expression, source,433XPathEvaluationResult.XPathResultType.getQNameType(type));434* }</pre>435*436* Since the {@code evaluate} method does not support the437* {@link XPathEvaluationResult.XPathResultType#ANY ANY} type, specifying438* XPathEvaluationResult as the type will result in IllegalArgumentException.439* Any implementation supporting the440* {@link XPathEvaluationResult.XPathResultType#ANY ANY} type must override441* this method.442*443* @param <T> The class type that will be returned by the XPath expression.444* @param expression The XPath expression.445* @param source The input source of the document to evaluate over.446* @param type The class type expected to be returned by the XPath expression,447* must be one of the types described in section448* <a href="package-summary.html#XPath.Datatypes.Class">3.2 Class types</a>449* in the package summary.450*451* @return The result of evaluating the expression.452*453* @throws XPathExpressionException If the expression cannot be evaluated.454* @throws IllegalArgumentException If {@code type} is not of the types455* corresponding to the types defined in the {@link XPathEvaluationResult.XPathResultType456* XPathResultType}, or XPathEvaluationResult is specified as the type but an457* implementation supporting the458* {@link XPathEvaluationResult.XPathResultType#ANY ANY} type is not available.459* @throws NullPointerException If {@code expression, source or type}is {@code null}.460*461* @since 9462*/463default <T>T evaluateExpression(String expression, InputSource source, Class<T> type)464throws XPathExpressionException465{466return type.cast(evaluate(expression, source,467XPathEvaluationResult.XPathResultType.getQNameType(type)));468}469470/**471* Evaluate an XPath expression in the specified context. This is equivalent to472* calling {@link #evaluateExpression(String expression, Object item, Class type)}473* with type {@link XPathEvaluationResult}:474* <pre> {@code475* evaluateExpression(expression, item, XPathEvaluationResult.class);476* }</pre>477*478* @implSpec479* The default implementation in the XPath API is equivalent to:480* <pre> {@code481* evaluateExpression(expression, source, XPathEvaluationResult.class);482* }</pre>483*484* Since the {@code evaluate} method does not support the485* {@link XPathEvaluationResult.XPathResultType#ANY ANY}486* type, the default implementation of this method will always throw an487* IllegalArgumentException. Any implementation supporting the488* {@link XPathEvaluationResult.XPathResultType#ANY ANY} type must therefore489* override this method.490*491* @param expression The XPath expression.492* @param source The input source of the document to evaluate over.493*494* @return The result of evaluating the expression.495*496* @throws XPathExpressionException If the expression cannot be evaluated.497* @throws IllegalArgumentException If the implementation of this method498* does not support the499* {@link XPathEvaluationResult.XPathResultType#ANY ANY} type.500* @throws NullPointerException If {@code expression or source} is {@code null}.501*502* @since 9503*/504default XPathEvaluationResult<?> evaluateExpression(String expression, InputSource source)505throws XPathExpressionException506{507return evaluateExpression(expression, source, XPathEvaluationResult.class);508}509}510511512