KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > xml > xpath > XPath


1 // $Id: XPath.java,v 1.12.14.2.2.3 2004/07/01 17:49:22 ndw Exp $
2

3 /*
4  * @(#)XPath.java 1.10 04/07/26
5  *
6  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
7  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
8  */

9
10 package javax.xml.xpath;
11
12 import org.xml.sax.InputSource JavaDoc;
13 import javax.xml.namespace.QName JavaDoc;
14 import javax.xml.namespace.NamespaceContext JavaDoc;
15
16 /**
17  * <p><code>XPath</code> provides access to the XPath evaluation environment and expressions.</p>
18  *
19  * <table id="XPath-evaluation" border="1" cellpadding="2">
20  * <thead>
21  * <tr>
22  * <th colspan="2">Evaluation of XPath Expressions.</th>
23  * </tr>
24  * </thead>
25  * <tbody>
26  * <tr>
27  * <td>context</td>
28  * <td>
29  * If a request is made to evaluate the expression in the absence
30  * of a context item, an empty document node will be used for the context.
31  * For the purposes of evaluating XPath expressions, a DocumentFragment
32  * is treated like a Document node.
33  * </td>
34  * </tr>
35  * <tr>
36  * <td>variables</td>
37  * <td>
38  * If the expression contains a variable reference, its value will be found through the {@link XPathVariableResolver}
39  * set with {@link #setXPathVariableResolver(XPathVariableResolver resolver)}.
40  * An {@link XPathExpressionException} is raised if the variable resolver is undefined or
41  * the resolver returns <code>null</code> for the variable.
42  * The value of a variable must be immutable through the course of any single evaluation.</p>
43  * </td>
44  * </tr>
45  * <tr>
46  * <td>functions</td>
47  * <td>
48  * If the expression contains a function reference, the function will be found through the {@link XPathFunctionResolver}
49  * set with {@link #setXPathFunctionResolver(XPathFunctionResolver resolver)}.
50  * An {@link XPathExpressionException} is raised if the function resolver is undefined or
51  * the function resolver returns <code>null</code> for the function.</p>
52  * </td>
53  * </tr>
54  * <tr>
55  * <td>QNames</td>
56  * <td>
57  * QNames in the expression are resolved against the XPath namespace context
58  * set with {@link #setNamespaceContext(NamespaceContext nsContext)}.
59  * </td>
60  * </tr>
61  * <tr>
62  * <td>result</td>
63  * <td>
64  * This result of evaluating an expression is converted to an instance of the desired return type.
65  * Valid return types are defined in {@link XPathConstants}.
66  * Conversion to the return type follows XPath conversion rules.</p>
67  * </td>
68  * </tr>
69  * </table>
70  *
71  * @author <a HREF="Norman.Walsh@Sun.com">Norman Walsh</a>
72  * @author <a HREF="Jeff.Suttor@Sun.com">Jeff Suttor</a>
73  * @version $Revision: 1.12.14.2.2.3 $, $Date: 2004/07/01 17:49:22 $
74  * @see <a HREF="http://www.w3.org/TR/xpath">XML Path Language (XPath) Version 1.0</a>
75  * @since 1.5
76  */

77 public interface XPath {
78     
79     /**
80      * <p>Reset this <code>XPath</code> to its original configuration.</p>
81      *
82      * <p><code>XPath</code> is reset to the same state as when it was created with
83      * {@link XPathFactory#newXPath()}.
84      * <code>reset()</code> is designed to allow the reuse of existing <code>XPath</code>s
85      * thus saving resources associated with the creation of new <code>XPath</code>s.</p>
86      *
87      * <p>The reset <code>XPath</code> is not guaranteed to have the same {@link XPathFunctionResolver}, {@link XPathVariableResolver}
88      * or {@link NamespaceContext} <code>Object</code>s, e.g. {@link Object#equals(Object obj)}.
89      * It is guaranteed to have a functionally equal <code>XPathFunctionResolver</code>, <code>XPathVariableResolver</code>
90      * and <code>NamespaceContext</code>.</p>
91      */

92     public void reset();
93
94     /**
95      * <p>Establish a variable resolver.</p>
96      *
97      * <p>A <code>NullPointerException</code> is thrown if <code>resolver</code> is <code>null</code>.</p>
98      *
99      * @param resolver Variable resolver.
100      *
101      * @throws NullPointerException If <code>resolver</code> is <code>null</code>.
102      */

103     public void setXPathVariableResolver(XPathVariableResolver JavaDoc resolver);
104
105     /**
106        * <p>Return the current variable resolver.</p>
107        *
108        * <p><code>null</code> is returned in no variable resolver is in effect.</p>
109        *
110        * @return Current variable resolver.
111        */

112     public XPathVariableResolver JavaDoc getXPathVariableResolver();
113
114     /**
115        * <p>Establish a function resolver.</p>
116        *
117        * <p>A <code>NullPointerException</code> is thrown if <code>resolver</code> is <code>null</code>.</p>
118        *
119        * @param resolver XPath function resolver.
120        *
121        * @throws NullPointerException If <code>resolver</code> is <code>null</code>.
122        */

123     public void setXPathFunctionResolver(XPathFunctionResolver JavaDoc resolver);
124
125     /**
126        * <p>Return the current function resolver.</p>
127        *
128        * <p><code>null</code> is returned in no function resolver is in effect.</p>
129        *
130        * @return Current function resolver.
131        */

132     public XPathFunctionResolver JavaDoc getXPathFunctionResolver();
133
134     /**
135        * <p>Establish a namespace context.</p>
136        *
137        * <p>A <code>NullPointerException</code> is thrown if <code>nsContext</code> is <code>null</code>.</p>
138        *
139        * @param nsContext Namespace context to use.
140        *
141        * @throws NullPointerException If <code>nsContext</code> is <code>null</code>.
142        */

143     public void setNamespaceContext(NamespaceContext JavaDoc nsContext);
144
145     /**
146        * <p>Return the current namespace context.</p>
147        *
148        * <p><code>null</code> is returned in no namespace context is in effect.</p>
149        *
150        * @return Current Namespace context.
151        */

152     public NamespaceContext JavaDoc getNamespaceContext();
153
154     /**
155        * <p>Compile an XPath expression for later evaluation.</p>
156        *
157        * <p>If <code>expression</code> contains any {@link XPathFunction}s,
158        * they must be available via the {@link XPathFunctionResolver}.
159        * An {@link XPathExpressionException} will be thrown if the <code>XPathFunction</code>
160        * cannot be resovled with the <code>XPathFunctionResolver</code>.</p>
161        *
162        * <p>If <code>expression</code> is <code>null</code>, a <code>NullPointerException</code> is thrown.</p>
163        *
164        * @param expression The XPath expression.
165        *
166        * @return Compiled XPath expression.
167     
168        * @throws XPathExpressionException If <code>expression</code> cannot be compiled.
169        * @throws NullPointerException If <code>expression</code> is <code>null</code>.
170        */

171     public XPathExpression JavaDoc compile(String JavaDoc expression)
172         throws XPathExpressionException JavaDoc;
173
174     /**
175      * <p>Evaluate an <code>XPath</code> expression in the specified context and return the result as the specified type.</p>
176      *
177      * <p>See <a HREF="#XPath-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,
178      * variable, function and <code>QName</code> resolution and return type conversion.</p>
179      *
180      * <p>If <code>returnType</code> is not one of the types defined in {@link XPathConstants} (
181      * {@link XPathConstants#NUMBER NUMBER},
182      * {@link XPathConstants#STRING STRING},
183      * {@link XPathConstants#BOOLEAN BOOLEAN},
184      * {@link XPathConstants#NODE NODE} or
185      * {@link XPathConstants#NODESET NODESET})
186      * then an <code>IllegalArgumentException</code> is thrown.</p>
187      *
188      * <p>If a <code>null</code> value is provided for
189      * <code>item</code>, an empty document will be used for the
190      * context.
191      * If <code>expression</code> or <code>returnType</code> is <code>null</code>, then a
192      * <code>NullPointerException</code> is thrown.</p>
193      *
194      * @param expression The XPath expression.
195      * @param item The starting context (node or node list, for example).
196      * @param returnType The desired return type.
197      *
198      * @return Result of evaluating an XPath expression as an <code>Object</code> of <code>returnType</code>.
199      *
200      * @throws XPathExpressionException If <code>expression</code> cannot be evaluated.
201      * @throws IllegalArgumentException If <code>returnType</code> is not one of the types defined in {@link XPathConstants}.
202      * @throws NullPointerException If <code>expression</code> or <code>returnType</code> is <code>null</code>.
203      */

204     public Object JavaDoc evaluate(String JavaDoc expression, Object JavaDoc item, QName JavaDoc returnType)
205         throws XPathExpressionException JavaDoc;
206
207     /**
208      * <p>Evaluate an XPath expression in the specified context and return the result as a <code>String</code>.</p>
209      *
210      * <p>This method calls {@link #evaluate(String expression, Object item, QName returnType)} with a <code>returnType</code> of
211      * {@link XPathConstants#STRING}.</p>
212      *
213      * <p>See <a HREF="#XPath-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,
214      * variable, function and QName resolution and return type conversion.</p>
215      *
216      * <p>If a <code>null</code> value is provided for
217      * <code>item</code>, an empty document will be used for the
218      * context.
219      * If <code>expression</code> is <code>null</code>, then a <code>NullPointerException</code> is thrown.</p>
220      *
221      * @param expression The XPath expression.
222      * @param item The starting context (node or node list, for example).
223      *
224      * @return The <code>String</code> that is the result of evaluating the expression and
225      * converting the result to a <code>String</code>.
226      *
227      * @throws XPathExpressionException If <code>expression</code> cannot be evaluated.
228      * @throws NullPointerException If <code>expression</code> is <code>null</code>.
229      */

230     public String JavaDoc evaluate(String JavaDoc expression, Object JavaDoc item)
231         throws XPathExpressionException JavaDoc;
232
233     /**
234      * <p>Evaluate an XPath expression in the context of the specified <code>InputSource</code>
235      * and return the result as the specified type.</p>
236      *
237      * <p>This method builds a data model for the {@link InputSource} and calls
238      * {@link #evaluate(String expression, Object item, QName returnType)} on the resulting document object.</p>
239      *
240      * <p>See <a HREF="#XPath-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,
241      * variable, function and QName resolution and return type conversion.</p>
242      *
243      * <p>If <code>returnType</code> is not one of the types defined in {@link XPathConstants},
244      * then an <code>IllegalArgumentException</code> is thrown.</p>
245      *
246      * <p>If <code>expression</code>, <code>source</code> or <code>returnType</code> is <code>null</code>,
247      * then a <code>NullPointerException</code> is thrown.</p>
248      *
249      * @param expression The XPath expression.
250      * @param source The input source of the document to evaluate over.
251      * @param returnType The desired return type.
252      *
253      * @return The <code>Object</code> that encapsulates the result of evaluating the expression.
254      *
255      * @throws XPathExpressionException If expression cannot be evaluated.
256      * @throws IllegalArgumentException If <code>returnType</code> is not one of the types defined in {@link XPathConstants}.
257      * @throws NullPointerException If <code>expression</code>, <code>source</code> or <code>returnType</code>
258      * is <code>null</code>.
259      */

260     public Object JavaDoc evaluate(
261         String JavaDoc expression,
262         InputSource JavaDoc source,
263         QName JavaDoc returnType)
264         throws XPathExpressionException JavaDoc;
265
266     /**
267      * <p>Evaluate an XPath expression in the context of the specified <code>InputSource</code>
268      * and return the result as a <code>String</code>.</p>
269      *
270      * <p>This method calls {@link #evaluate(String expression, InputSource source, QName returnType)} with a
271      * <code>returnType</code> of {@link XPathConstants#STRING}.</p>
272      *
273      * <p>See <a HREF="#XPath-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,
274      * variable, function and QName resolution and return type conversion.</p>
275      *
276      * <p>If <code>expression</code> or <code>source</code> is <code>null</code>,
277      * then a <code>NullPointerException</code> is thrown.</p>
278      *
279      * @param expression The XPath expression.
280      * @param source The <code>InputSource</code> of the document to evaluate over.
281      *
282      * @return The <code>String</code> that is the result of evaluating the expression and
283      * converting the result to a <code>String</code>.
284      *
285      * @throws XPathExpressionException If expression cannot be evaluated.
286      * @throws NullPointerException If <code>expression</code> or <code>source</code> is <code>null</code>.
287      */

288     public String JavaDoc evaluate(String JavaDoc expression, InputSource JavaDoc source)
289         throws XPathExpressionException JavaDoc;
290 }
291
Popular Tags