1 /*
   2  * Copyright (c) 2003, 2005, Oracle and/or its affiliates. All rights reserved.
   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 
  28 import org.xml.sax.InputSource;
  29 import javax.xml.namespace.QName;
  30 import javax.xml.namespace.NamespaceContext;
  31 
  32 /**
  33  * <p><code>XPath</code> provides access to the XPath evaluation environment and expressions.</p>
  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  *   <tbody>
  43  *     <tr>
  44  *       <td>context</td>
  45  *       <td>
  46  *         If a request is made to evaluate the expression in the absence
  47  * of a context item, an empty document node will be used for the context.
  48  * For the purposes of evaluating XPath expressions, a DocumentFragment
  49  * is treated like a Document node.
  50  *      </td>
  51  *    </tr>
  52  *    <tr>
  53  *      <td>variables</td>
  54  *      <td>
  55  *        If the expression contains a variable reference, its value will be found through the {@link XPathVariableResolver}
  56  *        set with {@link #setXPathVariableResolver(XPathVariableResolver resolver)}.
  57  *        An {@link XPathExpressionException} is raised if the variable resolver is undefined or
  58  *        the resolver returns <code>null</code> for the variable.
  59  *        The value of a variable must be immutable through the course of any single evaluation.</p>
  60  *      </td>
  61  *    </tr>
  62  *    <tr>
  63  *      <td>functions</td>
  64  *      <td>
  65  *        If the expression contains a function reference, the function will be found through the {@link XPathFunctionResolver}
  66  *        set with {@link #setXPathFunctionResolver(XPathFunctionResolver resolver)}.
  67  *        An {@link XPathExpressionException} is raised if the function resolver is undefined or
  68  *        the function resolver returns <code>null</code> for the function.</p>
  69  *      </td>
  70  *    </tr>
  71  *    <tr>
  72  *      <td>QNames</td>
  73  *      <td>
  74  *        QNames in the expression are resolved against the XPath namespace context
  75  *        set with {@link #setNamespaceContext(NamespaceContext nsContext)}.
  76  *      </td>
  77  *    </tr>
  78  *    <tr>
  79  *      <td>result</td>
  80  *      <td>
  81  *        This result of evaluating an expression is converted to an instance of the desired return type.
  82  *        Valid return types are defined in {@link XPathConstants}.
  83  *        Conversion to the return type follows XPath conversion rules.</p>
  84  *      </td>
  85  *    </tr>
  86  * </table>
  87  *
  88  * <p>An XPath object is not thread-safe and not reentrant.
  89  * In other words, it is the application's responsibility to make
  90  * sure that one {@link XPath} object is not used from
  91  * more than one thread at any given time, and while the <code>evaluate</code>
  92  * method is invoked, applications may not recursively call
  93  * the <code>evaluate</code> method.
  94  * <p>
  95  *
  96  * @author  <a href="Norman.Walsh@Sun.com">Norman Walsh</a>
  97  * @author  <a href="Jeff.Suttor@Sun.com">Jeff Suttor</a>
  98  * @see <a href="http://www.w3.org/TR/xpath">XML Path Language (XPath) Version 1.0</a>
  99  * @since 1.5
 100  */
 101 public interface XPath {
 102 
 103         /**
 104          * <p>Reset this <code>XPath</code> to its original configuration.</p>
 105          *
 106          * <p><code>XPath</code> is reset to the same state as when it was created with
 107          * {@link XPathFactory#newXPath()}.
 108          * <code>reset()</code> is designed to allow the reuse of existing <code>XPath</code>s
 109          * thus saving resources associated with the creation of new <code>XPath</code>s.</p>
 110          *
 111          * <p>The reset <code>XPath</code> is not guaranteed to have the same {@link XPathFunctionResolver}, {@link XPathVariableResolver}
 112          * or {@link NamespaceContext} <code>Object</code>s, e.g. {@link Object#equals(Object obj)}.
 113          * It is guaranteed to have a functionally equal <code>XPathFunctionResolver</code>, <code>XPathVariableResolver</code>
 114          * and <code>NamespaceContext</code>.</p>
 115          */
 116         public void reset();
 117 
 118     /**
 119      * <p>Establish a variable resolver.</p>
 120      *
 121      * <p>A <code>NullPointerException</code> is thrown if <code>resolver</code> is <code>null</code>.</p>
 122      *
 123      * @param resolver Variable resolver.
 124      *
 125      *  @throws NullPointerException If <code>resolver</code> is <code>null</code>.
 126      */
 127     public void setXPathVariableResolver(XPathVariableResolver resolver);
 128 
 129     /**
 130        * <p>Return the current variable resolver.</p>
 131        *
 132        * <p><code>null</code> is returned in no variable resolver is in effect.</p>
 133        *
 134        * @return Current variable resolver.
 135        */
 136     public XPathVariableResolver getXPathVariableResolver();
 137 
 138     /**
 139        * <p>Establish a function resolver.</p>
 140        *
 141        * <p>A <code>NullPointerException</code> is thrown if <code>resolver</code> is <code>null</code>.</p>
 142        *
 143        * @param resolver XPath function resolver.
 144        *
 145        * @throws NullPointerException If <code>resolver</code> is <code>null</code>.
 146        */
 147     public void setXPathFunctionResolver(XPathFunctionResolver resolver);
 148 
 149     /**
 150        * <p>Return the current function resolver.</p>
 151        *
 152        * <p><code>null</code> is returned in no function resolver is in effect.</p>
 153        *
 154        * @return Current function resolver.
 155        */
 156     public XPathFunctionResolver getXPathFunctionResolver();
 157 
 158     /**
 159        * <p>Establish a namespace context.</p>
 160        *
 161        * <p>A <code>NullPointerException</code> is thrown if <code>nsContext</code> is <code>null</code>.</p>
 162        *
 163        * @param nsContext Namespace context to use.
 164        *
 165        * @throws NullPointerException If <code>nsContext</code> is <code>null</code>.
 166        */
 167     public void setNamespaceContext(NamespaceContext nsContext);
 168 
 169     /**
 170        * <p>Return the current namespace context.</p>
 171        *
 172        * <p><code>null</code> is returned in no namespace context is in effect.</p>
 173        *
 174        * @return Current Namespace context.
 175        */
 176     public NamespaceContext getNamespaceContext();
 177 
 178     /**
 179        * <p>Compile an XPath expression for later evaluation.</p>
 180        *
 181        * <p>If <code>expression</code> contains any {@link XPathFunction}s,
 182        * they must be available via the {@link XPathFunctionResolver}.
 183        * An {@link XPathExpressionException} will be thrown if the
 184        * <code>XPathFunction</code>
 185        * cannot be resovled with the <code>XPathFunctionResolver</code>.</p>
 186        *
 187        * <p>If <code>expression</code> contains any variables, the
 188        * {@link XPathVariableResolver} in effect
 189        * <strong>at compile time</strong> will be used to resolve them.</p>
 190        *
 191        * <p>If <code>expression</code> is <code>null</code>, a <code>NullPointerException</code> is thrown.</p>
 192        *
 193        * @param expression The XPath expression.
 194        *
 195        * @return Compiled XPath expression.
 196 
 197        * @throws XPathExpressionException If <code>expression</code> cannot be compiled.
 198        * @throws NullPointerException If <code>expression</code> is <code>null</code>.
 199        */
 200     public XPathExpression compile(String expression)
 201         throws XPathExpressionException;
 202 
 203     /**
 204      * <p>Evaluate an <code>XPath</code> expression in the specified context and return the result as the specified type.</p>
 205      *
 206      * <p>See <a href="#XPath-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,
 207      * variable, function and <code>QName</code> resolution and return type conversion.</p>
 208      *
 209      * <p>If <code>returnType</code> is not one of the types defined in {@link XPathConstants} (
 210      * {@link XPathConstants#NUMBER NUMBER},
 211      * {@link XPathConstants#STRING STRING},
 212      * {@link XPathConstants#BOOLEAN BOOLEAN},
 213      * {@link XPathConstants#NODE NODE} or
 214      * {@link XPathConstants#NODESET NODESET})
 215      * then an <code>IllegalArgumentException</code> is thrown.</p>
 216      *
 217      * <p>If a <code>null</code> value is provided for
 218      * <code>item</code>, an empty document will be used for the
 219      * context.
 220      * If <code>expression</code> or <code>returnType</code> is <code>null</code>, then a
 221      * <code>NullPointerException</code> is thrown.</p>
 222      *
 223      * @param expression The XPath expression.
 224      * @param item The starting context (a node, for example).
 225      * @param returnType The desired return type.
 226      *
 227      * @return Result of evaluating an XPath expression as an <code>Object</code> of <code>returnType</code>.
 228      *
 229      * @throws XPathExpressionException If <code>expression</code> cannot be evaluated.
 230      * @throws IllegalArgumentException If <code>returnType</code> is not one of the types defined in {@link XPathConstants}.
 231      * @throws NullPointerException If <code>expression</code> or <code>returnType</code> is <code>null</code>.
 232      */
 233     public Object evaluate(String expression, Object item, QName returnType)
 234         throws XPathExpressionException;
 235 
 236     /**
 237      * <p>Evaluate an XPath expression in the specified context and return the result as a <code>String</code>.</p>
 238      *
 239      * <p>This method calls {@link #evaluate(String expression, Object item, QName returnType)} with a <code>returnType</code> of
 240      * {@link XPathConstants#STRING}.</p>
 241      *
 242      * <p>See <a href="#XPath-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,
 243      * variable, function and QName resolution and return type conversion.</p>
 244      *
 245      * <p>If a <code>null</code> value is provided for
 246      * <code>item</code>, an empty document will be used for the
 247      * context.
 248      * If <code>expression</code> is <code>null</code>, then a <code>NullPointerException</code> is thrown.</p>
 249      *
 250      * @param expression The XPath expression.
 251      * @param item The starting context (a node, for example).
 252      *
 253      * @return The <code>String</code> that is the result of evaluating the expression and
 254      *   converting the result to a <code>String</code>.
 255      *
 256      * @throws XPathExpressionException If <code>expression</code> cannot be evaluated.
 257      * @throws NullPointerException If <code>expression</code> is <code>null</code>.
 258      */
 259     public String evaluate(String expression, Object item)
 260         throws XPathExpressionException;
 261 
 262     /**
 263      * <p>Evaluate an XPath expression in the context of the specified <code>InputSource</code>
 264      * and return the result as the specified type.</p>
 265      *
 266      * <p>This method builds a data model for the {@link InputSource} and calls
 267      * {@link #evaluate(String expression, Object item, QName returnType)} on the resulting document object.</p>
 268      *
 269      * <p>See <a href="#XPath-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,
 270      * variable, function and QName resolution and return type conversion.</p>
 271      *
 272      * <p>If <code>returnType</code> is not one of the types defined in {@link XPathConstants},
 273      * then an <code>IllegalArgumentException</code> is thrown.</p>
 274      *
 275      * <p>If <code>expression</code>, <code>source</code> or <code>returnType</code> is <code>null</code>,
 276      * then a <code>NullPointerException</code> is thrown.</p>
 277      *
 278      * @param expression The XPath expression.
 279      * @param source The input source of the document to evaluate over.
 280      * @param returnType The desired return type.
 281      *
 282      * @return The <code>Object</code> that encapsulates the result of evaluating the expression.
 283      *
 284      * @throws XPathExpressionException If expression cannot be evaluated.
 285      * @throws IllegalArgumentException If <code>returnType</code> is not one of the types defined in {@link XPathConstants}.
 286      * @throws NullPointerException If <code>expression</code>, <code>source</code> or <code>returnType</code>
 287      *   is <code>null</code>.
 288      */
 289     public Object evaluate(
 290         String expression,
 291         InputSource source,
 292         QName returnType)
 293         throws XPathExpressionException;
 294 
 295     /**
 296      * <p>Evaluate an XPath expression in the context of the specified <code>InputSource</code>
 297      * and return the result as a <code>String</code>.</p>
 298      *
 299      * <p>This method calls {@link #evaluate(String expression, InputSource source, QName returnType)} with a
 300      * <code>returnType</code> of {@link XPathConstants#STRING}.</p>
 301      *
 302      * <p>See <a href="#XPath-evaluation">Evaluation of XPath Expressions</a> for context item evaluation,
 303      * variable, function and QName resolution and return type conversion.</p>
 304      *
 305      * <p>If <code>expression</code> or <code>source</code> is <code>null</code>,
 306      * then a <code>NullPointerException</code> is thrown.</p>
 307      *
 308      * @param expression The XPath expression.
 309      * @param source The <code>InputSource</code> of the document to evaluate over.
 310      *
 311      * @return The <code>String</code> that is the result of evaluating the expression and
 312      *   converting the result to a <code>String</code>.
 313      *
 314      * @throws XPathExpressionException If expression cannot be evaluated.
 315      * @throws NullPointerException If <code>expression</code> or <code>source</code> is <code>null</code>.
 316      */
 317     public String evaluate(String expression, InputSource source)
 318         throws XPathExpressionException;
 319 }