001    /*
002     * Copyright (c) 2004 World Wide Web Consortium,
003     *
004     * (Massachusetts Institute of Technology, European Research Consortium for
005     * Informatics and Mathematics, Keio University). All Rights Reserved. This
006     * work is distributed under the W3C(r) Software License [1] in the hope that
007     * it will be useful, but WITHOUT ANY WARRANTY; without even the implied
008     * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
009     *
010     * [1] http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231
011     */
012    
013    package org.w3c.dom.xpath;
014    
015    import org.w3c.dom.Node;
016    import org.w3c.dom.DOMException;
017    
018    /**
019     *  The evaluation of XPath expressions is provided by 
020     * <code>XPathEvaluator</code>. In a DOM implementation which supports the 
021     * XPath 3.0 feature, as described above, the <code>XPathEvaluator</code> 
022     * interface will be implemented on the same object which implements the 
023     * <code>Document</code> interface permitting it to be obtained by the usual 
024     * binding-specific method such as casting or by using the DOM Level 3 
025     * getInterface method. In this case the implementation obtained from the 
026     * Document supports the XPath DOM module and is compatible with the XPath 
027     * 1.0 specification. 
028     * <p>Evaluation of expressions with specialized extension functions or 
029     * variables may not work in all implementations and is, therefore, not 
030     * portable. <code>XPathEvaluator</code> implementations may be available 
031     * from other sources that could provide specific support for specialized 
032     * extension functions or variables as would be defined by other 
033     * specifications. 
034     * <p>See also the <a href='http://www.w3.org/TR/2004/NOTE-DOM-Level-3-XPath-20040226'>Document Object Model (DOM) Level 3 XPath Specification</a>.
035     */
036    public interface XPathEvaluator {
037        /**
038         * Creates a parsed XPath expression with resolved namespaces. This is 
039         * useful when an expression will be reused in an application since it 
040         * makes it possible to compile the expression string into a more 
041         * efficient internal form and preresolve all namespace prefixes which 
042         * occur within the expression.
043         * @param expression The XPath expression string to be parsed.
044         * @param resolver The <code>resolver</code> permits translation of all 
045         *   prefixes, including the <code>xml</code> namespace prefix, within 
046         *   the XPath expression into appropriate namespace URIs. If this is 
047         *   specified as <code>null</code>, any namespace prefix within the 
048         *   expression will result in <code>DOMException</code> being thrown 
049         *   with the code <code>NAMESPACE_ERR</code>.
050         * @return The compiled form of the XPath expression.
051         * @exception XPathException
052         *   INVALID_EXPRESSION_ERR: Raised if the expression is not legal 
053         *   according to the rules of the <code>XPathEvaluator</code>.
054         * @exception DOMException
055         *   NAMESPACE_ERR: Raised if the expression contains namespace prefixes 
056         *   which cannot be resolved by the specified 
057         *   <code>XPathNSResolver</code>.
058         */
059        public XPathExpression createExpression(String expression, 
060                                                XPathNSResolver resolver)
061                                                throws XPathException, DOMException;
062    
063        /**
064         * Adapts any DOM node to resolve namespaces so that an XPath expression 
065         * can be easily evaluated relative to the context of the node where it 
066         * appeared within the document. This adapter works like the DOM Level 3 
067         * method <code>lookupNamespaceURI</code> on nodes in resolving the 
068         * namespaceURI from a given prefix using the current information 
069         * available in the node's hierarchy at the time lookupNamespaceURI is 
070         * called. also correctly resolving the implicit xml prefix.
071         * @param nodeResolver The node to be used as a context for namespace 
072         *   resolution.
073         * @return <code>XPathNSResolver</code> which resolves namespaces with 
074         *   respect to the definitions in scope for a specified node.
075         */
076        public XPathNSResolver createNSResolver(Node nodeResolver);
077    
078        /**
079         * Evaluates an XPath expression string and returns a result of the 
080         * specified type if possible.
081         * @param expression The XPath expression string to be parsed and 
082         *   evaluated.
083         * @param contextNode The <code>context</code> is context node for the 
084         *   evaluation of this XPath expression. If the XPathEvaluator was 
085         *   obtained by casting the <code>Document</code> then this must be 
086         *   owned by the same document and must be a <code>Document</code>, 
087         *   <code>Element</code>, <code>Attribute</code>, <code>Text</code>, 
088         *   <code>CDATASection</code>, <code>Comment</code>, 
089         *   <code>ProcessingInstruction</code>, or <code>XPathNamespace</code> 
090         *   node. If the context node is a <code>Text</code> or a 
091         *   <code>CDATASection</code>, then the context is interpreted as the 
092         *   whole logical text node as seen by XPath, unless the node is empty 
093         *   in which case it may not serve as the XPath context.
094         * @param resolver The <code>resolver</code> permits translation of all 
095         *   prefixes, including the <code>xml</code> namespace prefix, within 
096         *   the XPath expression into appropriate namespace URIs. If this is 
097         *   specified as <code>null</code>, any namespace prefix within the 
098         *   expression will result in <code>DOMException</code> being thrown 
099         *   with the code <code>NAMESPACE_ERR</code>.
100         * @param type If a specific <code>type</code> is specified, then the 
101         *   result will be returned as the corresponding type.For XPath 1.0 
102         *   results, this must be one of the codes of the 
103         *   <code>XPathResult</code> interface.
104         * @param result The <code>result</code> specifies a specific result 
105         *   object which may be reused and returned by this method. If this is 
106         *   specified as <code>null</code>or the implementation does not reuse 
107         *   the specified result, a new result object will be constructed and 
108         *   returned.For XPath 1.0 results, this object will be of type 
109         *   <code>XPathResult</code>.
110         * @return The result of the evaluation of the XPath expression.For XPath 
111         *   1.0 results, this object will be of type <code>XPathResult</code>.
112         * @exception XPathException
113         *   INVALID_EXPRESSION_ERR: Raised if the expression is not legal 
114         *   according to the rules of the <code>XPathEvaluator</code>i
115         *   <br>TYPE_ERR: Raised if the result cannot be converted to return the 
116         *   specified type.
117         * @exception DOMException
118         *   NAMESPACE_ERR: Raised if the expression contains namespace prefixes 
119         *   which cannot be resolved by the specified 
120         *   <code>XPathNSResolver</code>.
121         *   <br>WRONG_DOCUMENT_ERR: The Node is from a document that is not 
122         *   supported by this <code>XPathEvaluator</code>.
123         *   <br>NOT_SUPPORTED_ERR: The Node is not a type permitted as an XPath 
124         *   context node or the request type is not permitted by this 
125         *   <code>XPathEvaluator</code>.
126         */
127        public Object evaluate(String expression, 
128                               Node contextNode, 
129                               XPathNSResolver resolver, 
130                               short type, 
131                               Object result)
132                               throws XPathException, DOMException;
133    
134    }