Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxp/src/javax/xml/xpath/XPath.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;29import javax.xml.namespace.NamespaceContext;3031/**32* <p><code>XPath</code> provides access to the XPath evaluation environment and expressions.</p>33*34* <a name="XPath-evaluation"/>35* <table border="1" cellpadding="2">36* <thead>37* <tr>38* <th colspan="2">Evaluation of XPath Expressions.</th>39* </tr>40* </thead>41* <tbody>42* <tr>43* <td>context</td>44* <td>45* If a request is made to evaluate the expression in the absence46* of a context item, an empty document node will be used for the context.47* For the purposes of evaluating XPath expressions, a DocumentFragment48* 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 or57* the resolver returns <code>null</code> for the variable.58* The value of a variable must be immutable through the course of any single evaluation.</p>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 or67* the function resolver returns <code>null</code> for the function.</p>68* </td>69* </tr>70* <tr>71* <td>QNames</td>72* <td>73* QNames in the expression are resolved against the XPath namespace context74* 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.</p>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 make89* sure that one {@link XPath} object is not used from90* more than one thread at any given time, and while the <code>evaluate</code>91* method is invoked, applications may not recursively call92* the <code>evaluate</code> method.93* <p>94*95* @author <a href="[email protected]">Norman Walsh</a>96* @author <a href="[email protected]">Jeff Suttor</a>97* @see <a href="http://www.w3.org/TR/xpath">XML Path Language (XPath) Version 1.0</a>98* @since 1.599*/100public interface XPath {101102/**103* <p>Reset this <code>XPath</code> to its original configuration.</p>104*105* <p><code>XPath</code> is reset to the same state as when it was created with106* {@link XPathFactory#newXPath()}.107* <code>reset()</code> is designed to allow the reuse of existing <code>XPath</code>s108* thus saving resources associated with the creation of new <code>XPath</code>s.</p>109*110* <p>The reset <code>XPath</code> is not guaranteed to have the same {@link XPathFunctionResolver}, {@link XPathVariableResolver}111* or {@link NamespaceContext} <code>Object</code>s, e.g. {@link Object#equals(Object obj)}.112* It is guaranteed to have a functionally equal <code>XPathFunctionResolver</code>, <code>XPathVariableResolver</code>113* and <code>NamespaceContext</code>.</p>114*/115public void reset();116117/**118* <p>Establish a variable resolver.</p>119*120* <p>A <code>NullPointerException</code> is thrown if <code>resolver</code> is <code>null</code>.</p>121*122* @param resolver Variable resolver.123*124* @throws NullPointerException If <code>resolver</code> is <code>null</code>.125*/126public void setXPathVariableResolver(XPathVariableResolver resolver);127128/**129* <p>Return the current variable resolver.</p>130*131* <p><code>null</code> is returned in no variable resolver is in effect.</p>132*133* @return Current variable resolver.134*/135public XPathVariableResolver getXPathVariableResolver();136137/**138* <p>Establish a function resolver.</p>139*140* <p>A <code>NullPointerException</code> is thrown if <code>resolver</code> is <code>null</code>.</p>141*142* @param resolver XPath function resolver.143*144* @throws NullPointerException If <code>resolver</code> is <code>null</code>.145*/146public void setXPathFunctionResolver(XPathFunctionResolver resolver);147148/**149* <p>Return the current function resolver.</p>150*151* <p><code>null</code> is returned in no function resolver is in effect.</p>152*153* @return Current function resolver.154*/155public XPathFunctionResolver getXPathFunctionResolver();156157/**158* <p>Establish a namespace context.</p>159*160* <p>A <code>NullPointerException</code> is thrown if <code>nsContext</code> is <code>null</code>.</p>161*162* @param nsContext Namespace context to use.163*164* @throws NullPointerException If <code>nsContext</code> is <code>null</code>.165*/166public void setNamespaceContext(NamespaceContext nsContext);167168/**169* <p>Return the current namespace context.</p>170*171* <p><code>null</code> is returned in no namespace context is in effect.</p>172*173* @return Current Namespace context.174*/175public NamespaceContext getNamespaceContext();176177/**178* <p>Compile an XPath expression for later evaluation.</p>179*180* <p>If <code>expression</code> contains any {@link XPathFunction}s,181* they must be available via the {@link XPathFunctionResolver}.182* An {@link XPathExpressionException} will be thrown if the183* <code>XPathFunction</code>184* cannot be resovled with the <code>XPathFunctionResolver</code>.</p>185*186* <p>If <code>expression</code> contains any variables, the187* {@link XPathVariableResolver} in effect188* <strong>at compile time</strong> will be used to resolve them.</p>189*190* <p>If <code>expression</code> is <code>null</code>, a <code>NullPointerException</code> is thrown.</p>191*192* @param expression The XPath expression.193*194* @return Compiled XPath expression.195196* @throws XPathExpressionException If <code>expression</code> cannot be compiled.197* @throws NullPointerException If <code>expression</code> is <code>null</code>.198*/199public XPathExpression compile(String expression)200throws XPathExpressionException;201202/**203* <p>Evaluate an <code>XPath</code> expression in the specified context and return the result as the specified type.</p>204*205* <p>See <a href="#XPath-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,206* variable, function and <code>QName</code> resolution and return type conversion.</p>207*208* <p>If <code>returnType</code> is not one of the types defined in {@link XPathConstants} (209* {@link XPathConstants#NUMBER NUMBER},210* {@link XPathConstants#STRING STRING},211* {@link XPathConstants#BOOLEAN BOOLEAN},212* {@link XPathConstants#NODE NODE} or213* {@link XPathConstants#NODESET NODESET})214* then an <code>IllegalArgumentException</code> is thrown.</p>215*216* <p>If a <code>null</code> value is provided for217* <code>item</code>, an empty document will be used for the218* context.219* If <code>expression</code> or <code>returnType</code> is <code>null</code>, then a220* <code>NullPointerException</code> is thrown.</p>221*222* @param expression The XPath expression.223* @param item The starting context (a node, for example).224* @param returnType The desired return type.225*226* @return Result of evaluating an XPath expression as an <code>Object</code> of <code>returnType</code>.227*228* @throws XPathExpressionException If <code>expression</code> cannot be evaluated.229* @throws IllegalArgumentException If <code>returnType</code> is not one of the types defined in {@link XPathConstants}.230* @throws NullPointerException If <code>expression</code> or <code>returnType</code> is <code>null</code>.231*/232public Object evaluate(String expression, Object item, QName returnType)233throws XPathExpressionException;234235/**236* <p>Evaluate an XPath expression in the specified context and return the result as a <code>String</code>.</p>237*238* <p>This method calls {@link #evaluate(String expression, Object item, QName returnType)} with a <code>returnType</code> of239* {@link XPathConstants#STRING}.</p>240*241* <p>See <a href="#XPath-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,242* variable, function and QName resolution and return type conversion.</p>243*244* <p>If a <code>null</code> value is provided for245* <code>item</code>, an empty document will be used for the246* context.247* If <code>expression</code> is <code>null</code>, then a <code>NullPointerException</code> is thrown.</p>248*249* @param expression The XPath expression.250* @param item The starting context (a node, for example).251*252* @return The <code>String</code> that is the result of evaluating the expression and253* converting the result to a <code>String</code>.254*255* @throws XPathExpressionException If <code>expression</code> cannot be evaluated.256* @throws NullPointerException If <code>expression</code> is <code>null</code>.257*/258public String evaluate(String expression, Object item)259throws XPathExpressionException;260261/**262* <p>Evaluate an XPath expression in the context of the specified <code>InputSource</code>263* and return the result as the specified type.</p>264*265* <p>This method builds a data model for the {@link InputSource} and calls266* {@link #evaluate(String expression, Object item, QName returnType)} on the resulting document object.</p>267*268* <p>See <a href="#XPath-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,269* variable, function and QName resolution and return type conversion.</p>270*271* <p>If <code>returnType</code> is not one of the types defined in {@link XPathConstants},272* then an <code>IllegalArgumentException</code> is thrown.</p>273*274* <p>If <code>expression</code>, <code>source</code> or <code>returnType</code> is <code>null</code>,275* then a <code>NullPointerException</code> is thrown.</p>276*277* @param expression The XPath expression.278* @param source The input source of the document to evaluate over.279* @param returnType The desired return type.280*281* @return The <code>Object</code> that encapsulates the result of evaluating the expression.282*283* @throws XPathExpressionException If expression cannot be evaluated.284* @throws IllegalArgumentException If <code>returnType</code> is not one of the types defined in {@link XPathConstants}.285* @throws NullPointerException If <code>expression</code>, <code>source</code> or <code>returnType</code>286* is <code>null</code>.287*/288public Object evaluate(289String expression,290InputSource source,291QName returnType)292throws XPathExpressionException;293294/**295* <p>Evaluate an XPath expression in the context of the specified <code>InputSource</code>296* and return the result as a <code>String</code>.</p>297*298* <p>This method calls {@link #evaluate(String expression, InputSource source, QName returnType)} with a299* <code>returnType</code> of {@link XPathConstants#STRING}.</p>300*301* <p>See <a href="#XPath-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,302* variable, function and QName resolution and return type conversion.</p>303*304* <p>If <code>expression</code> or <code>source</code> is <code>null</code>,305* then a <code>NullPointerException</code> is thrown.</p>306*307* @param expression The XPath expression.308* @param source The <code>InputSource</code> of the document to evaluate over.309*310* @return The <code>String</code> that is the result of evaluating the expression and311* converting the result to a <code>String</code>.312*313* @throws XPathExpressionException If expression cannot be evaluated.314* @throws NullPointerException If <code>expression</code> or <code>source</code> is <code>null</code>.315*/316public String evaluate(String expression, InputSource source)317throws XPathExpressionException;318}319320321