1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 // $Id: XPath.java 569998 2007-08-27 04:40:02Z mrglavas $
18 
19 package javax.xml.xpath;
20 
21 import javax.xml.namespace.NamespaceContext;
22 import javax.xml.namespace.QName;
23 import org.xml.sax.InputSource;
24 
25 /**
26  * <p><code>XPath</code> provides access to the XPath evaluation environment and expressions.</p>
27  *
28  * <table id="XPath-evaluation" border="1" cellpadding="2">
29  *   <thead>
30  *     <tr>
31  *       <th colspan="2">Evaluation of XPath Expressions.</th>
32  *     </tr>
33  *   </thead>
34  *   <tbody>
35  *     <tr>
36  *       <td>context</td>
37  *       <td>
38  *         If a request is made to evaluate the expression in the absence
39  * of a context item, an empty document node will be used for the context.
40  * For the purposes of evaluating XPath expressions, a DocumentFragment
41  * is treated like a Document node.
42  *      </td>
43  *    </tr>
44  *    <tr>
45  *      <td>variables</td>
46  *      <td>
47  *        If the expression contains a variable reference, its value will be found through the {@link XPathVariableResolver}
48  *        set with {@link #setXPathVariableResolver(XPathVariableResolver resolver)}.
49  *        An {@link XPathExpressionException} is raised if the variable resolver is undefined or
50  *        the resolver returns <code>null</code> for the variable.
51  *        The value of a variable must be immutable through the course of any single evaluation.</p>
52  *      </td>
53  *    </tr>
54  *    <tr>
55  *      <td>functions</td>
56  *      <td>
57  *        If the expression contains a function reference, the function will be found through the {@link XPathFunctionResolver}
58  *        set with {@link #setXPathFunctionResolver(XPathFunctionResolver resolver)}.
59  *        An {@link XPathExpressionException} is raised if the function resolver is undefined or
60  *        the function resolver returns <code>null</code> for the function.</p>
61  *      </td>
62  *    </tr>
63  *    <tr>
64  *      <td>QNames</td>
65  *      <td>
66  *        QNames in the expression are resolved against the XPath namespace context
67  *        set with {@link #setNamespaceContext(NamespaceContext nsContext)}.
68  *      </td>
69  *    </tr>
70  *    <tr>
71  *      <td>result</td>
72  *      <td>
73  *        This result of evaluating an expression is converted to an instance of the desired return type.
74  *        Valid return types are defined in {@link XPathConstants}.
75  *        Conversion to the return type follows XPath conversion rules.</p>
76  *      </td>
77  *    </tr>
78  * </table>
79  *
80  * @author  <a href="Norman.Walsh@Sun.com">Norman Walsh</a>
81  * @author  <a href="Jeff.Suttor@Sun.com">Jeff Suttor</a>
82  * @version $Revision: 569998 $, $Date: 2007-08-26 21:40:02 -0700 (Sun, 26 Aug 2007) $
83  * @see <a href="http://www.w3.org/TR/xpath">XML Path Language (XPath) Version 1.0</a>
84  * @since 1.5
85  */
86 public interface XPath {
87 
88     /**
89      * <p>Reset this <code>XPath</code> to its original configuration.</p>
90      *
91      * <p><code>XPath</code> is reset to the same state as when it was created with
92      * {@link XPathFactory#newXPath()}.
93      * <code>reset()</code> is designed to allow the reuse of existing <code>XPath</code>s
94      * thus saving resources associated with the creation of new <code>XPath</code>s.</p>
95      *
96      * <p>The reset <code>XPath</code> is not guaranteed to have the same {@link XPathFunctionResolver}, {@link XPathVariableResolver}
97      * or {@link NamespaceContext} <code>Object</code>s, e.g. {@link Object#equals(Object obj)}.
98      * It is guaranteed to have a functionally equal <code>XPathFunctionResolver</code>, <code>XPathVariableResolver</code>
99      * and <code>NamespaceContext</code>.</p>
100      */
reset()101     public void reset();
102 
103     /**
104      * <p>Establish a variable resolver.</p>
105      *
106      * <p>A <code>NullPointerException</code> is thrown if <code>resolver</code> is <code>null</code>.</p>
107      *
108      * @param resolver Variable resolver.
109      *
110      *  @throws NullPointerException If <code>resolver</code> is <code>null</code>.
111      */
setXPathVariableResolver(XPathVariableResolver resolver)112     public void setXPathVariableResolver(XPathVariableResolver resolver);
113 
114     /**
115        * <p>Return the current variable resolver.</p>
116        *
117        * <p><code>null</code> is returned in no variable resolver is in effect.</p>
118        *
119        * @return Current variable resolver.
120        */
getXPathVariableResolver()121     public XPathVariableResolver getXPathVariableResolver();
122 
123     /**
124        * <p>Establish a function resolver.</p>
125        *
126        * <p>A <code>NullPointerException</code> is thrown if <code>resolver</code> is <code>null</code>.</p>
127        *
128        * @param resolver XPath function resolver.
129        *
130        * @throws NullPointerException If <code>resolver</code> is <code>null</code>.
131        */
setXPathFunctionResolver(XPathFunctionResolver resolver)132     public void setXPathFunctionResolver(XPathFunctionResolver resolver);
133 
134     /**
135        * <p>Return the current function resolver.</p>
136        *
137        * <p><code>null</code> is returned in no function resolver is in effect.</p>
138        *
139        * @return Current function resolver.
140        */
getXPathFunctionResolver()141     public XPathFunctionResolver getXPathFunctionResolver();
142 
143     /**
144        * <p>Establish a namespace context.</p>
145        *
146        * <p>A <code>NullPointerException</code> is thrown if <code>nsContext</code> is <code>null</code>.</p>
147        *
148        * @param nsContext Namespace context to use.
149        *
150        * @throws NullPointerException If <code>nsContext</code> is <code>null</code>.
151        */
setNamespaceContext(NamespaceContext nsContext)152     public void setNamespaceContext(NamespaceContext nsContext);
153 
154     /**
155        * <p>Return the current namespace context.</p>
156        *
157        * <p><code>null</code> is returned in no namespace context is in effect.</p>
158        *
159        * @return Current Namespace context.
160        */
getNamespaceContext()161     public NamespaceContext getNamespaceContext();
162 
163     /**
164        * <p>Compile an XPath expression for later evaluation.</p>
165        *
166        * <p>If <code>expression</code> contains any {@link XPathFunction}s,
167        * they must be available via the {@link XPathFunctionResolver}.
168        * An {@link XPathExpressionException} will be thrown if the <code>XPathFunction</code>
169        * cannot be resolved with the <code>XPathFunctionResolver</code>.</p>
170        *
171        * <p>If <code>expression</code> is <code>null</code>, a <code>NullPointerException</code> is thrown.</p>
172        *
173        * @param expression The XPath expression.
174        *
175        * @return Compiled XPath expression.
176 
177        * @throws XPathExpressionException If <code>expression</code> cannot be compiled.
178        * @throws NullPointerException If <code>expression</code> is <code>null</code>.
179        */
compile(String expression)180     public XPathExpression compile(String expression)
181         throws XPathExpressionException;
182 
183     /**
184      * <p>Evaluate an <code>XPath</code> expression in the specified context and return the result as the specified type.</p>
185      *
186      * <p>See <a href="#XPath-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,
187      * variable, function and <code>QName</code> resolution and return type conversion.</p>
188      *
189      * <p>If <code>returnType</code> is not one of the types defined in {@link XPathConstants} (
190      * {@link XPathConstants#NUMBER NUMBER},
191      * {@link XPathConstants#STRING STRING},
192      * {@link XPathConstants#BOOLEAN BOOLEAN},
193      * {@link XPathConstants#NODE NODE} or
194      * {@link XPathConstants#NODESET NODESET})
195      * then an <code>IllegalArgumentException</code> is thrown.</p>
196      *
197      * <p>If a <code>null</code> value is provided for
198      * <code>item</code>, an empty document will be used for the
199      * context.
200      * If <code>expression</code> or <code>returnType</code> is <code>null</code>, then a
201      * <code>NullPointerException</code> is thrown.</p>
202      *
203      * @param expression The XPath expression.
204      * @param item The starting context (node or node list, for example).
205      * @param returnType The desired return type.
206      *
207      * @return Result of evaluating an XPath expression as an <code>Object</code> of <code>returnType</code>.
208      *
209      * @throws XPathExpressionException If <code>expression</code> cannot be evaluated.
210      * @throws IllegalArgumentException If <code>returnType</code> is not one of the types defined in {@link XPathConstants}.
211      * @throws NullPointerException If <code>expression</code> or <code>returnType</code> is <code>null</code>.
212      */
evaluate(String expression, Object item, QName returnType)213     public Object evaluate(String expression, Object item, QName returnType)
214         throws XPathExpressionException;
215 
216     /**
217      * <p>Evaluate an XPath expression in the specified context and return the result as a <code>String</code>.</p>
218      *
219      * <p>This method calls {@link #evaluate(String expression, Object item, QName returnType)} with a <code>returnType</code> of
220      * {@link XPathConstants#STRING}.</p>
221      *
222      * <p>See <a href="#XPath-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,
223      * variable, function and QName resolution and return type conversion.</p>
224      *
225      * <p>If a <code>null</code> value is provided for
226      * <code>item</code>, an empty document will be used for the
227      * context.
228      * If <code>expression</code> is <code>null</code>, then a <code>NullPointerException</code> is thrown.</p>
229      *
230      * @param expression The XPath expression.
231      * @param item The starting context (node or node list, for example).
232      *
233      * @return The <code>String</code> that is the result of evaluating the expression and
234      *   converting the result to a <code>String</code>.
235      *
236      * @throws XPathExpressionException If <code>expression</code> cannot be evaluated.
237      * @throws NullPointerException If <code>expression</code> is <code>null</code>.
238      */
evaluate(String expression, Object item)239     public String evaluate(String expression, Object item)
240         throws XPathExpressionException;
241 
242     /**
243      * <p>Evaluate an XPath expression in the context of the specified <code>InputSource</code>
244      * and return the result as the specified type.</p>
245      *
246      * <p>This method builds a data model for the {@link InputSource} and calls
247      * {@link #evaluate(String expression, Object item, QName returnType)} on the resulting document object.</p>
248      *
249      * <p>See <a href="#XPath-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,
250      * variable, function and QName resolution and return type conversion.</p>
251      *
252      * <p>If <code>returnType</code> is not one of the types defined in {@link XPathConstants},
253      * then an <code>IllegalArgumentException</code> is thrown.</p>
254      *
255      * <p>If <code>expression</code>, <code>source</code> or <code>returnType</code> is <code>null</code>,
256      * then a <code>NullPointerException</code> is thrown.</p>
257      *
258      * @param expression The XPath expression.
259      * @param source The input source of the document to evaluate over.
260      * @param returnType The desired return type.
261      *
262      * @return The <code>Object</code> that encapsulates the result of evaluating the expression.
263      *
264      * @throws XPathExpressionException If expression cannot be evaluated.
265      * @throws IllegalArgumentException If <code>returnType</code> is not one of the types defined in {@link XPathConstants}.
266      * @throws NullPointerException If <code>expression</code>, <code>source</code> or <code>returnType</code>
267      *   is <code>null</code>.
268      */
evaluate( String expression, InputSource source, QName returnType)269     public Object evaluate(
270         String expression,
271         InputSource source,
272         QName returnType)
273         throws XPathExpressionException;
274 
275     /**
276      * <p>Evaluate an XPath expression in the context of the specified <code>InputSource</code>
277      * and return the result as a <code>String</code>.</p>
278      *
279      * <p>This method calls {@link #evaluate(String expression, InputSource source, QName returnType)} with a
280      * <code>returnType</code> of {@link XPathConstants#STRING}.</p>
281      *
282      * <p>See <a href="#XPath-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,
283      * variable, function and QName resolution and return type conversion.</p>
284      *
285      * <p>If <code>expression</code> or <code>source</code> is <code>null</code>,
286      * then a <code>NullPointerException</code> is thrown.</p>
287      *
288      * @param expression The XPath expression.
289      * @param source The <code>InputSource</code> of the document to evaluate over.
290      *
291      * @return The <code>String</code> that is the result of evaluating the expression and
292      *   converting the result to a <code>String</code>.
293      *
294      * @throws XPathExpressionException If expression cannot be evaluated.
295      * @throws NullPointerException If <code>expression</code> or <code>source</code> is <code>null</code>.
296      */
evaluate(String expression, InputSource source)297     public String evaluate(String expression, InputSource source)
298         throws XPathExpressionException;
299 }
300