001 /*
002 * Copyright 2003-2005 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.xml.xpath;
027
028 import org.xml.sax.InputSource;
029 import javax.xml.namespace.QName;
030 import javax.xml.namespace.NamespaceContext;
031
032 /**
033 * <p><code>XPath</code> provides access to the XPath evaluation environment and expressions.</p>
034 *
035 * <a name="XPath-evaluation"/>
036 * <table border="1" cellpadding="2">
037 * <thead>
038 * <tr>
039 * <th colspan="2">Evaluation of XPath Expressions.</th>
040 * </tr>
041 * </thead>
042 * <tbody>
043 * <tr>
044 * <td>context</td>
045 * <td>
046 * If a request is made to evaluate the expression in the absence
047 * of a context item, an empty document node will be used for the context.
048 * For the purposes of evaluating XPath expressions, a DocumentFragment
049 * is treated like a Document node.
050 * </td>
051 * </tr>
052 * <tr>
053 * <td>variables</td>
054 * <td>
055 * If the expression contains a variable reference, its value will be found through the {@link XPathVariableResolver}
056 * set with {@link #setXPathVariableResolver(XPathVariableResolver resolver)}.
057 * An {@link XPathExpressionException} is raised if the variable resolver is undefined or
058 * the resolver returns <code>null</code> for the variable.
059 * The value of a variable must be immutable through the course of any single evaluation.</p>
060 * </td>
061 * </tr>
062 * <tr>
063 * <td>functions</td>
064 * <td>
065 * If the expression contains a function reference, the function will be found through the {@link XPathFunctionResolver}
066 * set with {@link #setXPathFunctionResolver(XPathFunctionResolver resolver)}.
067 * An {@link XPathExpressionException} is raised if the function resolver is undefined or
068 * the function resolver returns <code>null</code> for the function.</p>
069 * </td>
070 * </tr>
071 * <tr>
072 * <td>QNames</td>
073 * <td>
074 * QNames in the expression are resolved against the XPath namespace context
075 * set with {@link #setNamespaceContext(NamespaceContext nsContext)}.
076 * </td>
077 * </tr>
078 * <tr>
079 * <td>result</td>
080 * <td>
081 * This result of evaluating an expression is converted to an instance of the desired return type.
082 * Valid return types are defined in {@link XPathConstants}.
083 * Conversion to the return type follows XPath conversion rules.</p>
084 * </td>
085 * </tr>
086 * </table>
087 *
088 * <p>An XPath object is not thread-safe and not reentrant.
089 * In other words, it is the application's responsibility to make
090 * sure that one {@link XPath} object is not used from
091 * more than one thread at any given time, and while the <code>evaluate</code>
092 * method is invoked, applications may not recursively call
093 * the <code>evaluate</code> method.
094 * <p>
095 *
096 * @author <a href="Norman.Walsh@Sun.com">Norman Walsh</a>
097 * @author <a href="Jeff.Suttor@Sun.com">Jeff Suttor</a>
098 * @version $Revision: 1.5 $, $Date: 2005/10/13 17:00:49 $
099 * @see <a href="http://www.w3.org/TR/xpath">XML Path Language (XPath) Version 1.0</a>
100 * @since 1.5
101 */
102 public interface XPath {
103
104 /**
105 * <p>Reset this <code>XPath</code> to its original configuration.</p>
106 *
107 * <p><code>XPath</code> is reset to the same state as when it was created with
108 * {@link XPathFactory#newXPath()}.
109 * <code>reset()</code> is designed to allow the reuse of existing <code>XPath</code>s
110 * thus saving resources associated with the creation of new <code>XPath</code>s.</p>
111 *
112 * <p>The reset <code>XPath</code> is not guaranteed to have the same {@link XPathFunctionResolver}, {@link XPathVariableResolver}
113 * or {@link NamespaceContext} <code>Object</code>s, e.g. {@link Object#equals(Object obj)}.
114 * It is guaranteed to have a functionally equal <code>XPathFunctionResolver</code>, <code>XPathVariableResolver</code>
115 * and <code>NamespaceContext</code>.</p>
116 */
117 public void reset();
118
119 /**
120 * <p>Establish a variable resolver.</p>
121 *
122 * <p>A <code>NullPointerException</code> is thrown if <code>resolver</code> is <code>null</code>.</p>
123 *
124 * @param resolver Variable resolver.
125 *
126 * @throws NullPointerException If <code>resolver</code> is <code>null</code>.
127 */
128 public void setXPathVariableResolver(XPathVariableResolver resolver);
129
130 /**
131 * <p>Return the current variable resolver.</p>
132 *
133 * <p><code>null</code> is returned in no variable resolver is in effect.</p>
134 *
135 * @return Current variable resolver.
136 */
137 public XPathVariableResolver getXPathVariableResolver();
138
139 /**
140 * <p>Establish a function resolver.</p>
141 *
142 * <p>A <code>NullPointerException</code> is thrown if <code>resolver</code> is <code>null</code>.</p>
143 *
144 * @param resolver XPath function resolver.
145 *
146 * @throws NullPointerException If <code>resolver</code> is <code>null</code>.
147 */
148 public void setXPathFunctionResolver(XPathFunctionResolver resolver);
149
150 /**
151 * <p>Return the current function resolver.</p>
152 *
153 * <p><code>null</code> is returned in no function resolver is in effect.</p>
154 *
155 * @return Current function resolver.
156 */
157 public XPathFunctionResolver getXPathFunctionResolver();
158
159 /**
160 * <p>Establish a namespace context.</p>
161 *
162 * <p>A <code>NullPointerException</code> is thrown if <code>nsContext</code> is <code>null</code>.</p>
163 *
164 * @param nsContext Namespace context to use.
165 *
166 * @throws NullPointerException If <code>nsContext</code> is <code>null</code>.
167 */
168 public void setNamespaceContext(NamespaceContext nsContext);
169
170 /**
171 * <p>Return the current namespace context.</p>
172 *
173 * <p><code>null</code> is returned in no namespace context is in effect.</p>
174 *
175 * @return Current Namespace context.
176 */
177 public NamespaceContext getNamespaceContext();
178
179 /**
180 * <p>Compile an XPath expression for later evaluation.</p>
181 *
182 * <p>If <code>expression</code> contains any {@link XPathFunction}s,
183 * they must be available via the {@link XPathFunctionResolver}.
184 * An {@link XPathExpressionException} will be thrown if the
185 * <code>XPathFunction</code>
186 * cannot be resovled with the <code>XPathFunctionResolver</code>.</p>
187 *
188 * <p>If <code>expression</code> contains any variables, the
189 * {@link XPathVariableResolver} in effect
190 * <strong>at compile time</strong> will be used to resolve them.</p>
191 *
192 * <p>If <code>expression</code> is <code>null</code>, a <code>NullPointerException</code> is thrown.</p>
193 *
194 * @param expression The XPath expression.
195 *
196 * @return Compiled XPath expression.
197
198 * @throws XPathExpressionException If <code>expression</code> cannot be compiled.
199 * @throws NullPointerException If <code>expression</code> is <code>null</code>.
200 */
201 public XPathExpression compile(String expression)
202 throws XPathExpressionException;
203
204 /**
205 * <p>Evaluate an <code>XPath</code> expression in the specified context and return the result as the specified type.</p>
206 *
207 * <p>See <a href="#XPath-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,
208 * variable, function and <code>QName</code> resolution and return type conversion.</p>
209 *
210 * <p>If <code>returnType</code> is not one of the types defined in {@link XPathConstants} (
211 * {@link XPathConstants#NUMBER NUMBER},
212 * {@link XPathConstants#STRING STRING},
213 * {@link XPathConstants#BOOLEAN BOOLEAN},
214 * {@link XPathConstants#NODE NODE} or
215 * {@link XPathConstants#NODESET NODESET})
216 * then an <code>IllegalArgumentException</code> is thrown.</p>
217 *
218 * <p>If a <code>null</code> value is provided for
219 * <code>item</code>, an empty document will be used for the
220 * context.
221 * If <code>expression</code> or <code>returnType</code> is <code>null</code>, then a
222 * <code>NullPointerException</code> is thrown.</p>
223 *
224 * @param expression The XPath expression.
225 * @param item The starting context (a node, for example).
226 * @param returnType The desired return type.
227 *
228 * @return Result of evaluating an XPath expression as an <code>Object</code> of <code>returnType</code>.
229 *
230 * @throws XPathExpressionException If <code>expression</code> cannot be evaluated.
231 * @throws IllegalArgumentException If <code>returnType</code> is not one of the types defined in {@link XPathConstants}.
232 * @throws NullPointerException If <code>expression</code> or <code>returnType</code> is <code>null</code>.
233 */
234 public Object evaluate(String expression, Object item,
235 QName returnType) throws XPathExpressionException;
236
237 /**
238 * <p>Evaluate an XPath expression in the specified context and return the result as a <code>String</code>.</p>
239 *
240 * <p>This method calls {@link #evaluate(String expression, Object item, QName returnType)} with a <code>returnType</code> of
241 * {@link XPathConstants#STRING}.</p>
242 *
243 * <p>See <a href="#XPath-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,
244 * variable, function and QName resolution and return type conversion.</p>
245 *
246 * <p>If a <code>null</code> value is provided for
247 * <code>item</code>, an empty document will be used for the
248 * context.
249 * If <code>expression</code> is <code>null</code>, then a <code>NullPointerException</code> is thrown.</p>
250 *
251 * @param expression The XPath expression.
252 * @param item The starting context (a node, for example).
253 *
254 * @return The <code>String</code> that is the result of evaluating the expression and
255 * converting the result to a <code>String</code>.
256 *
257 * @throws XPathExpressionException If <code>expression</code> cannot be evaluated.
258 * @throws NullPointerException If <code>expression</code> is <code>null</code>.
259 */
260 public String evaluate(String expression, Object item)
261 throws XPathExpressionException;
262
263 /**
264 * <p>Evaluate an XPath expression in the context of the specified <code>InputSource</code>
265 * and return the result as the specified type.</p>
266 *
267 * <p>This method builds a data model for the {@link InputSource} and calls
268 * {@link #evaluate(String expression, Object item, QName returnType)} on the resulting document object.</p>
269 *
270 * <p>See <a href="#XPath-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,
271 * variable, function and QName resolution and return type conversion.</p>
272 *
273 * <p>If <code>returnType</code> is not one of the types defined in {@link XPathConstants},
274 * then an <code>IllegalArgumentException</code> is thrown.</p>
275 *
276 * <p>If <code>expression</code>, <code>source</code> or <code>returnType</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 input source of the document to evaluate over.
281 * @param returnType The desired return type.
282 *
283 * @return The <code>Object</code> that encapsulates the result of evaluating the expression.
284 *
285 * @throws XPathExpressionException If expression cannot be evaluated.
286 * @throws IllegalArgumentException If <code>returnType</code> is not one of the types defined in {@link XPathConstants}.
287 * @throws NullPointerException If <code>expression</code>, <code>source</code> or <code>returnType</code>
288 * is <code>null</code>.
289 */
290 public Object evaluate(String expression, InputSource source,
291 QName returnType) throws XPathExpressionException;
292
293 /**
294 * <p>Evaluate an XPath expression in the context of the specified <code>InputSource</code>
295 * and return the result as a <code>String</code>.</p>
296 *
297 * <p>This method calls {@link #evaluate(String expression, InputSource source, QName returnType)} with a
298 * <code>returnType</code> of {@link XPathConstants#STRING}.</p>
299 *
300 * <p>See <a href="#XPath-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,
301 * variable, function and QName resolution and return type conversion.</p>
302 *
303 * <p>If <code>expression</code> or <code>source</code> is <code>null</code>,
304 * then a <code>NullPointerException</code> is thrown.</p>
305 *
306 * @param expression The XPath expression.
307 * @param source The <code>InputSource</code> of the document to evaluate over.
308 *
309 * @return The <code>String</code> that is the result of evaluating the expression and
310 * converting the result to a <code>String</code>.
311 *
312 * @throws XPathExpressionException If expression cannot be evaluated.
313 * @throws NullPointerException If <code>expression</code> or <code>source</code> is <code>null</code>.
314 */
315 public String evaluate(String expression, InputSource source)
316 throws XPathExpressionException;
317 }
|