12005
|
1 |
/*
|
28695
|
2 |
* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
|
12005
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package javax.xml.xpath;
|
|
27 |
|
28695
|
28 |
import javax.xml.namespace.NamespaceContext;
|
12005
|
29 |
import javax.xml.namespace.QName;
|
28695
|
30 |
import org.xml.sax.InputSource;
|
12005
|
31 |
|
|
32 |
/**
|
28695
|
33 |
* {@code XPath} provides access to the XPath evaluation environment and expressions.
|
12005
|
34 |
*
|
|
35 |
* <a name="XPath-evaluation"/>
|
|
36 |
* <table border="1" cellpadding="2">
|
|
37 |
* <thead>
|
|
38 |
* <tr>
|
|
39 |
* <th colspan="2">Evaluation of XPath Expressions.</th>
|
|
40 |
* </tr>
|
|
41 |
* </thead>
|
|
42 |
* <tr>
|
|
43 |
* <td>context</td>
|
|
44 |
* <td>
|
|
45 |
* If a request is made to evaluate the expression in the absence
|
|
46 |
* of a context item, an empty document node will be used for the context.
|
|
47 |
* For the purposes of evaluating XPath expressions, a DocumentFragment
|
|
48 |
* 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 or
|
28695
|
57 |
* the resolver returns {@code null} for the variable.
|
|
58 |
* The value of a variable must be immutable through the course of any single evaluation.
|
12005
|
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 or
|
28695
|
67 |
* the function resolver returns {@code null} for the function.
|
12005
|
68 |
* </td>
|
|
69 |
* </tr>
|
|
70 |
* <tr>
|
|
71 |
* <td>QNames</td>
|
|
72 |
* <td>
|
|
73 |
* QNames in the expression are resolved against the XPath namespace context
|
|
74 |
* 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}.
|
28695
|
82 |
* Conversion to the return type follows XPath conversion rules.
|
12005
|
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 make
|
|
89 |
* sure that one {@link XPath} object is not used from
|
28695
|
90 |
* more than one thread at any given time, and while the {@code evaluate}
|
12005
|
91 |
* method is invoked, applications may not recursively call
|
28695
|
92 |
* the {@code evaluate} method.
|
12005
|
93 |
* <p>
|
|
94 |
*
|
|
95 |
* @author <a href="Norman.Walsh@Sun.com">Norman Walsh</a>
|
|
96 |
* @author <a href="Jeff.Suttor@Sun.com">Jeff Suttor</a>
|
|
97 |
* @see <a href="http://www.w3.org/TR/xpath">XML Path Language (XPath) Version 1.0</a>
|
|
98 |
* @since 1.5
|
|
99 |
*/
|
|
100 |
public interface XPath {
|
|
101 |
|
|
102 |
|
|
103 |
/**
|
28695
|
104 |
* Reset this {@code XPath} to its original configuration.
|
|
105 |
*
|
|
106 |
* <p>{@code XPath} is reset to the same state as when it was created with
|
|
107 |
* {@link XPathFactory#newXPath()}.
|
|
108 |
* {@code reset()} is designed to allow the reuse of existing {@code XPath}s
|
|
109 |
* thus saving resources associated with the creation of new {@code XPath}s.
|
12005
|
110 |
*
|
28695
|
111 |
* <p>The reset {@code XPath} is not guaranteed to have the same
|
|
112 |
* {@link XPathFunctionResolver}, {@link XPathVariableResolver}
|
|
113 |
* or {@link NamespaceContext} {@code Object}s, e.g. {@link Object#equals(Object obj)}.
|
|
114 |
* It is guaranteed to have a functionally equal {@code XPathFunctionResolver},
|
|
115 |
* {@code XPathVariableResolver} and {@code NamespaceContext}.
|
|
116 |
*/
|
|
117 |
public void reset();
|
|
118 |
|
|
119 |
/**
|
|
120 |
* Establish a variable resolver.
|
|
121 |
*
|
|
122 |
* <p>A {@code NullPointerException} is thrown if {@code resolver} is {@code null}.
|
12005
|
123 |
*
|
|
124 |
* @param resolver Variable resolver.
|
|
125 |
*
|
28695
|
126 |
* @throws NullPointerException If {@code resolver} is {@code null}.
|
12005
|
127 |
*/
|
|
128 |
public void setXPathVariableResolver(XPathVariableResolver resolver);
|
|
129 |
|
|
130 |
/**
|
28695
|
131 |
* Return the current variable resolver.
|
12005
|
132 |
*
|
28695
|
133 |
* <p>{@code null} is returned in no variable resolver is in effect.
|
12005
|
134 |
*
|
|
135 |
* @return Current variable resolver.
|
|
136 |
*/
|
|
137 |
public XPathVariableResolver getXPathVariableResolver();
|
|
138 |
|
|
139 |
/**
|
28695
|
140 |
* Establish a function resolver.
|
12005
|
141 |
*
|
28695
|
142 |
* <p>A {@code NullPointerException} is thrown if {@code resolver} is {@code null}.
|
12005
|
143 |
*
|
|
144 |
* @param resolver XPath function resolver.
|
|
145 |
*
|
28695
|
146 |
* @throws NullPointerException If {@code resolver} is {@code null}.
|
12005
|
147 |
*/
|
|
148 |
public void setXPathFunctionResolver(XPathFunctionResolver resolver);
|
|
149 |
|
|
150 |
/**
|
28695
|
151 |
* Return the current function resolver.
|
|
152 |
* <p>
|
|
153 |
* {@code null} is returned in no function resolver is in effect.
|
12005
|
154 |
*
|
|
155 |
* @return Current function resolver.
|
|
156 |
*/
|
|
157 |
public XPathFunctionResolver getXPathFunctionResolver();
|
|
158 |
|
|
159 |
/**
|
28695
|
160 |
* Establish a namespace context.
|
12005
|
161 |
*
|
28695
|
162 |
* <p>A {@code NullPointerException} is thrown if {@code nsContext} is {@code null}.
|
12005
|
163 |
*
|
|
164 |
* @param nsContext Namespace context to use.
|
|
165 |
*
|
28695
|
166 |
* @throws NullPointerException If {@code nsContext} is {@code null}.
|
12005
|
167 |
*/
|
|
168 |
public void setNamespaceContext(NamespaceContext nsContext);
|
|
169 |
|
|
170 |
/**
|
28695
|
171 |
* Return the current namespace context.
|
12005
|
172 |
*
|
28695
|
173 |
* <p>{@code null} is returned in no namespace context is in effect.
|
12005
|
174 |
*
|
|
175 |
* @return Current Namespace context.
|
|
176 |
*/
|
|
177 |
public NamespaceContext getNamespaceContext();
|
|
178 |
|
|
179 |
/**
|
28695
|
180 |
* Compile an XPath expression for later evaluation.
|
12005
|
181 |
*
|
28695
|
182 |
* <p>If {@code expression} contains any {@link XPathFunction}s,
|
12005
|
183 |
* they must be available via the {@link XPathFunctionResolver}.
|
|
184 |
* An {@link XPathExpressionException} will be thrown if the
|
28695
|
185 |
* {@code XPathFunction}
|
|
186 |
* cannot be resovled with the {@code XPathFunctionResolver}.
|
12005
|
187 |
*
|
28695
|
188 |
* <p>If {@code expression} contains any variables, the
|
12005
|
189 |
* {@link XPathVariableResolver} in effect
|
28695
|
190 |
* <strong>at compile time</strong> will be used to resolve them.
|
12005
|
191 |
*
|
|
192 |
* @param expression The XPath expression.
|
|
193 |
*
|
|
194 |
* @return Compiled XPath expression.
|
|
195 |
|
28695
|
196 |
* @throws XPathExpressionException If {@code expression} cannot be compiled.
|
|
197 |
* @throws NullPointerException If {@code expression} is {@code null}.
|
12005
|
198 |
*/
|
|
199 |
public XPathExpression compile(String expression)
|
|
200 |
throws XPathExpressionException;
|
|
201 |
|
|
202 |
/**
|
28695
|
203 |
* Evaluate an {@code XPath} expression in the specified context and
|
|
204 |
* return the result as the specified type.
|
12005
|
205 |
*
|
28695
|
206 |
* <p>
|
|
207 |
* See <a href="#XPath-evaluation">Evaluation of XPath Expressions</a>
|
|
208 |
* for context item evaluation, variable, function and {@code QName} resolution
|
|
209 |
* and return type conversion.
|
|
210 |
* <p>
|
|
211 |
* The parameter {@code item} represents the context the XPath expression
|
|
212 |
* will be operated on. The type of the context is implementation-dependent.
|
|
213 |
* If the value is {@code null}, the operation must have no dependency on
|
|
214 |
* the context, otherwise an XPathExpressionException will be thrown.
|
12005
|
215 |
*
|
28695
|
216 |
* @implNote
|
|
217 |
* The type of the context is usually {@link org.w3c.dom.Node}.
|
|
218 |
*
|
|
219 |
* @param expression The XPath expression.
|
|
220 |
* @param item The context the XPath expression will be evaluated in.
|
|
221 |
* @param returnType The result type expected to be returned by the XPath expression.
|
|
222 |
*
|
|
223 |
* @return The result of evaluating an XPath expression as an {@code Object} of {@code returnType}.
|
|
224 |
*
|
|
225 |
* @throws XPathExpressionException If {@code expression} cannot be evaluated.
|
|
226 |
* @throws IllegalArgumentException If {@code returnType} is not one of the types defined in {@link XPathConstants} (
|
12005
|
227 |
* {@link XPathConstants#NUMBER NUMBER},
|
|
228 |
* {@link XPathConstants#STRING STRING},
|
|
229 |
* {@link XPathConstants#BOOLEAN BOOLEAN},
|
|
230 |
* {@link XPathConstants#NODE NODE} or
|
28695
|
231 |
* {@link XPathConstants#NODESET NODESET}).
|
|
232 |
* @throws NullPointerException If {@code expression or returnType} is {@code null}.
|
12005
|
233 |
*/
|
|
234 |
public Object evaluate(String expression, Object item, QName returnType)
|
|
235 |
throws XPathExpressionException;
|
|
236 |
|
|
237 |
/**
|
28695
|
238 |
* Evaluate an XPath expression in the specified context and return the result as a {@code String}.
|
12005
|
239 |
*
|
28695
|
240 |
* <p>This method calls {@link #evaluate(String expression, Object item, QName returnType)} with a {@code returnType} of
|
|
241 |
* {@link XPathConstants#STRING}.
|
12005
|
242 |
*
|
|
243 |
* <p>See <a href="#XPath-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,
|
28695
|
244 |
* variable, function and QName resolution and return type conversion.
|
12005
|
245 |
*
|
28695
|
246 |
* <p>
|
|
247 |
* The parameter {@code item} represents the context the XPath expression
|
|
248 |
* will be operated on. The type of the context is implementation-dependent.
|
|
249 |
* If the value is {@code null}, the operation must have no dependency on
|
|
250 |
* the context, otherwise an XPathExpressionException will be thrown.
|
|
251 |
*
|
|
252 |
* @implNote
|
|
253 |
* The type of the context is usually {@link org.w3c.dom.Node}.
|
12005
|
254 |
*
|
|
255 |
* @param expression The XPath expression.
|
28695
|
256 |
* @param item The context the XPath expression will be evaluated in.
|
12005
|
257 |
*
|
28695
|
258 |
* @return The result of evaluating an XPath expression as a {@code String}.
|
12005
|
259 |
*
|
28695
|
260 |
* @throws XPathExpressionException If {@code expression} cannot be evaluated.
|
|
261 |
* @throws NullPointerException If {@code expression} is {@code null}.
|
12005
|
262 |
*/
|
|
263 |
public String evaluate(String expression, Object item)
|
|
264 |
throws XPathExpressionException;
|
|
265 |
|
|
266 |
/**
|
28695
|
267 |
* Evaluate an XPath expression in the context of the specified {@code InputSource}
|
|
268 |
* and return the result as the specified type.
|
12005
|
269 |
*
|
|
270 |
* <p>This method builds a data model for the {@link InputSource} and calls
|
28695
|
271 |
* {@link #evaluate(String expression, Object item, QName returnType)} on the resulting document object.
|
12005
|
272 |
*
|
|
273 |
* <p>See <a href="#XPath-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,
|
28695
|
274 |
* variable, function and QName resolution and return type conversion.
|
12005
|
275 |
*
|
|
276 |
* @param expression The XPath expression.
|
|
277 |
* @param source The input source of the document to evaluate over.
|
|
278 |
* @param returnType The desired return type.
|
|
279 |
*
|
28695
|
280 |
* @return The {@code Object} that encapsulates the result of evaluating the expression.
|
12005
|
281 |
*
|
|
282 |
* @throws XPathExpressionException If expression cannot be evaluated.
|
28695
|
283 |
* @throws IllegalArgumentException If {@code returnType} is not one of the types defined in {@link XPathConstants}.
|
|
284 |
* @throws NullPointerException If {@code expression, source or returnType} is {@code null}.
|
12005
|
285 |
*/
|
|
286 |
public Object evaluate(
|
|
287 |
String expression,
|
|
288 |
InputSource source,
|
|
289 |
QName returnType)
|
|
290 |
throws XPathExpressionException;
|
|
291 |
|
|
292 |
/**
|
28695
|
293 |
* Evaluate an XPath expression in the context of the specified {@code InputSource}
|
|
294 |
* and return the result as a {@code String}.
|
12005
|
295 |
*
|
|
296 |
* <p>This method calls {@link #evaluate(String expression, InputSource source, QName returnType)} with a
|
28695
|
297 |
* {@code returnType} of {@link XPathConstants#STRING}.
|
12005
|
298 |
*
|
|
299 |
* <p>See <a href="#XPath-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,
|
28695
|
300 |
* variable, function and QName resolution and return type conversion.
|
12005
|
301 |
*
|
|
302 |
* @param expression The XPath expression.
|
28695
|
303 |
* @param source The {@code InputSource} of the document to evaluate over.
|
12005
|
304 |
*
|
28695
|
305 |
* @return The {@code String} that is the result of evaluating the expression and
|
|
306 |
* converting the result to a {@code String}.
|
12005
|
307 |
*
|
|
308 |
* @throws XPathExpressionException If expression cannot be evaluated.
|
28695
|
309 |
* @throws NullPointerException If {@code expression or source} is {@code null}.
|
12005
|
310 |
*/
|
|
311 |
public String evaluate(String expression, InputSource source)
|
|
312 |
throws XPathExpressionException;
|
28695
|
313 |
|
|
314 |
/**
|
|
315 |
* Evaluate an XPath expression in the specified context and return
|
|
316 |
* the result with the type specified through the {@code class type}
|
|
317 |
*
|
|
318 |
* <p>
|
|
319 |
* The parameter {@code item} represents the context the XPath expression
|
|
320 |
* will be operated on. The type of the context is implementation-dependent.
|
|
321 |
* If the value is {@code null}, the operation must have no dependency on
|
|
322 |
* the context, otherwise an XPathExpressionException will be thrown.
|
|
323 |
*
|
|
324 |
* @implNote
|
|
325 |
* The type of the context is usually {@link org.w3c.dom.Node}.
|
|
326 |
*
|
|
327 |
* @implSpec
|
|
328 |
* The default implementation in the XPath API is equivalent to:
|
|
329 |
* <pre> {@code
|
|
330 |
* (T)evaluate(expression, item,
|
|
331 |
* XPathEvaluationResult.XPathResultType.getQNameType(type));
|
|
332 |
* }</pre>
|
|
333 |
*
|
|
334 |
* Since the {@code evaluate} method does not support the
|
|
335 |
* {@link XPathEvaluationResult.XPathResultType#ANY ANY} type, specifying
|
|
336 |
* XPathEvaluationResult as the type will result in IllegalArgumentException.
|
|
337 |
* Any implementation supporting the
|
|
338 |
* {@link XPathEvaluationResult.XPathResultType#ANY ANY} type must override
|
|
339 |
* this method.
|
|
340 |
*
|
|
341 |
* @param <T> The class type that will be returned by the XPath expression.
|
|
342 |
* @param expression The XPath expression.
|
|
343 |
* @param item The context the XPath expression will be evaluated in.
|
|
344 |
* @param type The class type expected to be returned by the XPath expression.
|
|
345 |
*
|
|
346 |
* @return The result of evaluating the expression.
|
|
347 |
*
|
|
348 |
* @throws XPathExpressionException If the expression cannot be evaluated.
|
|
349 |
* @throws IllegalArgumentException If {@code type} is not of the types
|
|
350 |
* corresponding to the types defined in the {@link XPathEvaluationResult.XPathResultType},
|
|
351 |
* or XPathEvaluationResult is specified as the type but an implementation supporting the
|
|
352 |
* {@link XPathEvaluationResult.XPathResultType#ANY ANY} type is not available.
|
|
353 |
* @throws NullPointerException If {@code expression or type} is {@code null}.
|
|
354 |
*
|
|
355 |
* @since 1.9
|
|
356 |
*/
|
|
357 |
default <T>T evaluateExpression(String expression, Object item, Class<T> type)
|
|
358 |
throws XPathExpressionException {
|
|
359 |
return type.cast(evaluate(expression, item,
|
|
360 |
XPathEvaluationResult.XPathResultType.getQNameType(type)));
|
|
361 |
}
|
|
362 |
|
|
363 |
/**
|
|
364 |
* Evaluate an XPath expression in the specified context. This is equivalent to
|
|
365 |
* calling {@link #evaluateExpression(String expression, Object item, Class type)}
|
|
366 |
* with type {@link XPathEvaluationResult}:
|
|
367 |
* <pre> {@code
|
|
368 |
* evaluateExpression(expression, item, XPathEvaluationResult.class);
|
|
369 |
* }</pre>
|
|
370 |
* <p>
|
|
371 |
* The parameter {@code item} represents the context the XPath expression
|
|
372 |
* will be operated on. The type of the context is implementation-dependent.
|
|
373 |
* If the value is {@code null}, the operation must have no dependency on
|
|
374 |
* the context, otherwise an XPathExpressionException will be thrown.
|
|
375 |
*
|
|
376 |
* @implNote
|
|
377 |
* The type of the context is usually {@link org.w3c.dom.Node}.
|
|
378 |
*
|
|
379 |
* @implSpec
|
|
380 |
* The default implementation in the XPath API is equivalent to:
|
|
381 |
* <pre> {@code
|
|
382 |
* evaluateExpression(expression, item, XPathEvaluationResult.class);
|
|
383 |
* }</pre>
|
|
384 |
*
|
|
385 |
* Since the {@code evaluate} method does not support the
|
|
386 |
* {@link XPathEvaluationResult.XPathResultType#ANY ANY}
|
|
387 |
* type, the default implementation of this method will always throw an
|
|
388 |
* IllegalArgumentException. Any implementation supporting the
|
|
389 |
* {@link XPathEvaluationResult.XPathResultType#ANY ANY} type must therefore
|
|
390 |
* override this method.
|
|
391 |
*
|
|
392 |
* @param expression The XPath expression.
|
|
393 |
* @param item The context the XPath expression will be evaluated in.
|
|
394 |
*
|
|
395 |
* @return The result of evaluating the expression.
|
|
396 |
*
|
|
397 |
* @throws XPathExpressionException If the expression cannot be evaluated.
|
|
398 |
* @throws IllegalArgumentException If the implementation of this method
|
|
399 |
* does not support the
|
|
400 |
* {@link XPathEvaluationResult.XPathResultType#ANY ANY} type.
|
|
401 |
* @throws NullPointerException If {@code expression} is {@code null}.
|
|
402 |
*
|
|
403 |
* @since 1.9
|
|
404 |
*/
|
|
405 |
default XPathEvaluationResult<?> evaluateExpression(String expression, Object item)
|
|
406 |
throws XPathExpressionException
|
|
407 |
{
|
|
408 |
return evaluateExpression(expression, item, XPathEvaluationResult.class);
|
|
409 |
}
|
|
410 |
|
|
411 |
/**
|
|
412 |
* Evaluate an XPath expression in the context of the specified {@code source}
|
|
413 |
* and return the result as specified.
|
|
414 |
* <p>
|
|
415 |
* This method builds a data model for the {@link InputSource} and calls
|
|
416 |
* {@link #evaluateExpression(String expression, Object item, Class type)}
|
|
417 |
* on the resulting document object. The data model is usually
|
|
418 |
* {@link org.w3c.dom.Document}
|
|
419 |
*
|
|
420 |
* @implSpec
|
|
421 |
* The default implementation in the XPath API is equivalent to:
|
|
422 |
* <pre> {@code
|
|
423 |
(T)evaluate(expression, source,
|
|
424 |
XPathEvaluationResult.XPathResultType.getQNameType(type));
|
|
425 |
* }</pre>
|
|
426 |
*
|
|
427 |
* Since the {@code evaluate} method does not support the
|
|
428 |
* {@link XPathEvaluationResult.XPathResultType#ANY ANY} type, specifying
|
|
429 |
* XPathEvaluationResult as the type will result in IllegalArgumentException.
|
|
430 |
* Any implementation supporting the
|
|
431 |
* {@link XPathEvaluationResult.XPathResultType#ANY ANY} type must override
|
|
432 |
* this method.
|
|
433 |
*
|
|
434 |
* @param <T> The class type that will be returned by the XPath expression.
|
|
435 |
* @param expression The XPath expression.
|
|
436 |
* @param source The input source of the document to evaluate over.
|
|
437 |
* @param type The class type expected to be returned by the XPath expression.
|
|
438 |
*
|
|
439 |
* @return The result of evaluating the expression.
|
|
440 |
*
|
|
441 |
* @throws XPathExpressionException If the expression cannot be evaluated.
|
|
442 |
* @throws IllegalArgumentException If {@code type} is not of the types
|
|
443 |
* corresponding to the types defined in the {@link XPathEvaluationResult.XPathResultType
|
|
444 |
* XPathResultType}, or XPathEvaluationResult is specified as the type but an
|
|
445 |
* implementation supporting the
|
|
446 |
* {@link XPathEvaluationResult.XPathResultType#ANY ANY} type is not available.
|
|
447 |
* @throws NullPointerException If {@code expression, source or type}is {@code null}.
|
|
448 |
*
|
|
449 |
* @since 1.9
|
|
450 |
*/
|
|
451 |
default <T>T evaluateExpression(String expression, InputSource source, Class<T> type)
|
|
452 |
throws XPathExpressionException
|
|
453 |
{
|
|
454 |
return type.cast(evaluate(expression, source,
|
|
455 |
XPathEvaluationResult.XPathResultType.getQNameType(type)));
|
|
456 |
}
|
|
457 |
|
|
458 |
/**
|
|
459 |
* Evaluate an XPath expression in the specified context. This is equivalent to
|
|
460 |
* calling {@link #evaluateExpression(String expression, Object item, Class type)}
|
|
461 |
* with type {@link XPathEvaluationResult}:
|
|
462 |
* <pre> {@code
|
|
463 |
* evaluateExpression(expression, item, XPathEvaluationResult.class);
|
|
464 |
* }</pre>
|
|
465 |
* <p>
|
|
466 |
*
|
|
467 |
* @implSpec
|
|
468 |
* The default implementation in the XPath API is equivalent to:
|
|
469 |
* <pre> {@code
|
|
470 |
* evaluateExpression(expression, source, XPathEvaluationResult.class);
|
|
471 |
* }</pre>
|
|
472 |
*
|
|
473 |
* Since the {@code evaluate} method does not support the
|
|
474 |
* {@link XPathEvaluationResult.XPathResultType#ANY ANY}
|
|
475 |
* type, the default implementation of this method will always throw an
|
|
476 |
* IllegalArgumentException. Any implementation supporting the
|
|
477 |
* {@link XPathEvaluationResult.XPathResultType#ANY ANY} type must therefore
|
|
478 |
* override this method.
|
|
479 |
*
|
|
480 |
* @param expression The XPath expression.
|
|
481 |
* @param source The input source of the document to evaluate over.
|
|
482 |
*
|
|
483 |
* @return The result of evaluating the expression.
|
|
484 |
*
|
|
485 |
* @throws XPathExpressionException If the expression cannot be evaluated.
|
|
486 |
* @throws IllegalArgumentException If the implementation of this method
|
|
487 |
* does not support the
|
|
488 |
* {@link XPathEvaluationResult.XPathResultType#ANY ANY} type.
|
|
489 |
* @throws NullPointerException If {@code expression or source} is {@code null}.
|
|
490 |
*
|
|
491 |
* @since 1.9
|
|
492 |
*/
|
|
493 |
default XPathEvaluationResult<?> evaluateExpression(String expression, InputSource source)
|
|
494 |
throws XPathExpressionException
|
|
495 |
{
|
|
496 |
return evaluateExpression(expression, source, XPathEvaluationResult.class);
|
|
497 |
}
|
12005
|
498 |
}
|