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
031 /**
032 * <p><code>XPathExpression</code> provides access to compiled XPath expressions.</p>
033 *
034 * <a name="XPathExpression-evaluation"/>
035 * <table border="1" cellpadding="2">
036 * <thead>
037 * <tr>
038 * <th colspan="2">Evaluation of XPath Expressions.</th>
039 * </tr>
040 * </thead>
041 * <tbody>
042 * <tr>
043 * <td>context</td>
044 * <td>
045 * If a request is made to evaluate the expression in the absence
046 * of a context item, an empty document node will be used for the context.
047 * For the purposes of evaluating XPath expressions, a DocumentFragment
048 * is treated like a Document node.
049 * </td>
050 * </tr>
051 * <tr>
052 * <td>variables</td>
053 * <td>
054 * If the expression contains a variable reference, its value will be found through the {@link XPathVariableResolver}.
055 * An {@link XPathExpressionException} is raised if the variable resolver is undefined or
056 * the resolver returns <code>null</code> for the variable.
057 * The value of a variable must be immutable through the course of any single evaluation.</p>
058 * </td>
059 * </tr>
060 * <tr>
061 * <td>functions</td>
062 * <td>
063 * If the expression contains a function reference, the function will be found through the {@link XPathFunctionResolver}.
064 * An {@link XPathExpressionException} is raised if the function resolver is undefined or
065 * the function resolver returns <code>null</code> for the function.</p>
066 * </td>
067 * </tr>
068 * <tr>
069 * <td>QNames</td>
070 * <td>
071 * QNames in the expression are resolved against the XPath namespace context.
072 * </td>
073 * </tr>
074 * <tr>
075 * <td>result</td>
076 * <td>
077 * This result of evaluating an expression is converted to an instance of the desired return type.
078 * Valid return types are defined in {@link XPathConstants}.
079 * Conversion to the return type follows XPath conversion rules.</p>
080 * </td>
081 * </tr>
082 * </table>
083 *
084 * <p>An XPath expression is not thread-safe and not reentrant.
085 * In other words, it is the application's responsibility to make
086 * sure that one {@link XPathExpression} object is not used from
087 * more than one thread at any given time, and while the <code>evaluate</code>
088 * method is invoked, applications may not recursively call
089 * the <code>evaluate</code> method.
090 * <p>
091 *
092 * @author <a href="mailto:Norman.Walsh@Sun.com">Norman Walsh</a>
093 * @author <a href="mailto:Jeff.Suttor@Sun.com">Jeff Suttor</a>
094 * @version $Revision: 1.4 $, $Date: 2005/10/13 17:00:49 $
095 * @see <a href="http://www.w3.org/TR/xpath#section-Expressions">XML Path Language (XPath) Version 1.0, Expressions</a>
096 * @since 1.5
097 */
098 public interface XPathExpression {
099
100 /**
101 * <p>Evaluate the compiled XPath expression in the specified context and return the result as the specified type.</p>
102 *
103 * <p>See <a href="#XPathExpression-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,
104 * variable, function and QName resolution and return type conversion.</p>
105 *
106 * <p>If <code>returnType</code> is not one of the types defined in {@link XPathConstants},
107 * then an <code>IllegalArgumentException</code> is thrown.</p>
108 *
109 * <p>If a <code>null</code> value is provided for
110 * <code>item</code>, an empty document will be used for the
111 * context.
112 * If <code>returnType</code> is <code>null</code>, then a <code>NullPointerException</code> is thrown.</p>
113 *
114 * @param item The starting context (a node, for example).
115 * @param returnType The desired return type.
116 *
117 * @return The <code>Object</code> that is the result of evaluating the expression and converting the result to
118 * <code>returnType</code>.
119 *
120 * @throws XPathExpressionException If the expression cannot be evaluated.
121 * @throws IllegalArgumentException If <code>returnType</code> is not one of the types defined in {@link XPathConstants}.
122 * @throws NullPointerException If <code>returnType</code> is <code>null</code>.
123 */
124 public Object evaluate(Object item, QName returnType)
125 throws XPathExpressionException;
126
127 /**
128 * <p>Evaluate the compiled XPath expression in the specified context and return the result as a <code>String</code>.</p>
129 *
130 * <p>This method calls {@link #evaluate(Object item, QName returnType)} with a <code>returnType</code> of
131 * {@link XPathConstants#STRING}.</p>
132 *
133 * <p>See <a href="#XPathExpression-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,
134 * variable, function and QName resolution and return type conversion.</p>
135 *
136 * <p>If a <code>null</code> value is provided for
137 * <code>item</code>, an empty document will be used for the
138 * context.
139 *
140 * @param item The starting context (a node, for example).
141 *
142 * @return The <code>String</code> that is the result of evaluating the expression and converting the result to a
143 * <code>String</code>.
144 *
145 * @throws XPathExpressionException If the expression cannot be evaluated.
146 */
147 public String evaluate(Object item) throws XPathExpressionException;
148
149 /**
150 * <p>Evaluate the compiled XPath expression in the context of the specified <code>InputSource</code> and return the result as the
151 * specified type.</p>
152 *
153 * <p>This method builds a data model for the {@link InputSource} and calls
154 * {@link #evaluate(Object item, QName returnType)} on the resulting document object.</p>
155 *
156 * <p>See <a href="#XPathExpression-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,
157 * variable, function and QName resolution and return type conversion.</p>
158 *
159 * <p>If <code>returnType</code> is not one of the types defined in {@link XPathConstants},
160 * then an <code>IllegalArgumentException</code> is thrown.</p>
161 *
162 * <p>If <code>source</code> or <code>returnType</code> is <code>null</code>,
163 * then a <code>NullPointerException</code> is thrown.</p>
164 *
165 * @param source The <code>InputSource</code> of the document to evaluate over.
166 * @param returnType The desired return type.
167 *
168 * @return The <code>Object</code> that is the result of evaluating the expression and converting the result to
169 * <code>returnType</code>.
170 *
171 * @throws XPathExpressionException If the expression cannot be evaluated.
172 * @throws IllegalArgumentException If <code>returnType</code> is not one of the types defined in {@link XPathConstants}.
173 * @throws NullPointerException If <code>source</code> or <code>returnType</code> is <code>null</code>.
174 */
175 public Object evaluate(InputSource source, QName returnType)
176 throws XPathExpressionException;
177
178 /**
179 * <p>Evaluate the compiled XPath expression in the context of the specified <code>InputSource</code> and return the result as a
180 * <code>String</code>.</p>
181 *
182 * <p>This method calls {@link #evaluate(InputSource source, QName returnType)} with a <code>returnType</code> of
183 * {@link XPathConstants#STRING}.</p>
184 *
185 * <p>See <a href="#XPathExpression-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,
186 * variable, function and QName resolution and return type conversion.</p>
187 *
188 * <p>If <code>source</code> is <code>null</code>, then a <code>NullPointerException</code> is thrown.</p>
189 *
190 * @param source The <code>InputSource</code> of the document to evaluate over.
191 *
192 * @return The <code>String</code> that is the result of evaluating the expression and converting the result to a
193 * <code>String</code>.
194 *
195 * @throws XPathExpressionException If the expression cannot be evaluated.
196 * @throws NullPointerException If <code>source</code> is <code>null</code>.
197 */
198 public String evaluate(InputSource source)
199 throws XPathExpressionException;
200 }
|