KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xpath > internal > domapi > XPathExpressionImpl


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: XPathExpressionImpl.java,v 1.2.4.1 2005/09/10 04:06:55 jeffsuttor Exp $
18  */

19
20
21 package com.sun.org.apache.xpath.internal.domapi;
22
23 import javax.xml.transform.TransformerException JavaDoc;
24
25 import com.sun.org.apache.xpath.internal.XPath;
26 import com.sun.org.apache.xpath.internal.XPathContext;
27 import com.sun.org.apache.xpath.internal.objects.XObject;
28 import com.sun.org.apache.xpath.internal.res.XPATHErrorResources;
29 import com.sun.org.apache.xpath.internal.res.XPATHMessages;
30 import org.w3c.dom.DOMException JavaDoc;
31 import org.w3c.dom.Document JavaDoc;
32 import org.w3c.dom.Node JavaDoc;
33 import org.w3c.dom.xpath.XPathException;
34 import org.w3c.dom.xpath.XPathExpression;
35 import org.w3c.dom.xpath.XPathNamespace;
36
37 /**
38  *
39  * The class provides an implementation of XPathExpression according
40  * to the DOM L3 XPath Specification, Working Group Note 26 February 2004.
41  *
42  * <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>.</p>
43  *
44  * <p>The <code>XPathExpression</code> interface represents a parsed and resolved
45  * XPath expression.</p>
46  *
47  * @see org.w3c.dom.xpath.XPathExpression
48  *
49  * @xsl.usage internal
50  */

51 class XPathExpressionImpl implements XPathExpression {
52
53   /**
54    * The xpath object that this expression wraps
55    */

56   final private XPath m_xpath;
57   
58   /**
59    * The document to be searched to parallel the case where the XPathEvaluator
60    * is obtained by casting a Document.
61    */

62   final private Document JavaDoc m_doc;
63
64     /**
65      * Constructor for XPathExpressionImpl.
66      *
67      * @param xpath The wrapped XPath object.
68      * @param doc The document to be searched, to parallel the case where''
69      * the XPathEvaluator is obtained by casting the document.
70      */

71     XPathExpressionImpl(XPath xpath, Document JavaDoc doc) {
72         m_xpath = xpath;
73         m_doc = doc;
74     }
75
76     /**
77      *
78      * This method provides an implementation XPathResult.evaluate according
79      * to the DOM L3 XPath Specification, Working Group Note 26 February 2004.
80      *
81      * <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>.</p>
82      *
83      * <p>Evaluates this XPath expression and returns a result.</p>
84      * @param contextNode The <code>context</code> is context node for the
85      * evaluation of this XPath expression.If the XPathEvaluator was
86      * obtained by casting the <code>Document</code> then this must be
87      * owned by the same document and must be a <code>Document</code>,
88      * <code>Element</code>, <code>Attribute</code>, <code>Text</code>,
89      * <code>CDATASection</code>, <code>Comment</code>,
90      * <code>ProcessingInstruction</code>, or <code>XPathNamespace</code>
91      * node.If the context node is a <code>Text</code> or a
92      * <code>CDATASection</code>, then the context is interpreted as the
93      * whole logical text node as seen by XPath, unless the node is empty
94      * in which case it may not serve as the XPath context.
95      * @param type If a specific <code>type</code> is specified, then the
96      * result will be coerced to return the specified type relying on
97      * XPath conversions and fail if the desired coercion is not possible.
98      * This must be one of the type codes of <code>XPathResult</code>.
99     * @param result The <code>result</code> specifies a specific result
100      * object which may be reused and returned by this method. If this is
101      * specified as <code>null</code>or the implementation does not reuse
102      * the specified result, a new result object will be constructed and
103      * returned.For XPath 1.0 results, this object will be of type
104      * <code>XPathResult</code>.
105      * @return The result of the evaluation of the XPath expression.For XPath
106      * 1.0 results, this object will be of type <code>XPathResult</code>.
107      * @exception XPathException
108      * TYPE_ERR: Raised if the result cannot be converted to return the
109      * specified type.
110      * @exception DOMException
111      * WRONG_DOCUMENT_ERR: The Node is from a document that is not supported
112      * by the XPathEvaluator that created this
113      * <code>XPathExpression</code>.
114      * <br>NOT_SUPPORTED_ERR: The Node is not a type permitted as an XPath
115      * context node.
116      *
117      * @see org.w3c.dom.xpath.XPathExpression#evaluate(Node, short, XPathResult)
118      * @xsl.usage internal
119      */

120     public Object JavaDoc evaluate(
121         Node JavaDoc contextNode,
122         short type,
123         Object JavaDoc result)
124         throws XPathException, DOMException JavaDoc {
125             
126         // If the XPathEvaluator was determined by "casting" the document
127
if (m_doc != null) {
128         
129             // Check that the context node is owned by the same document
130
if ((contextNode != m_doc) && (!contextNode.getOwnerDocument().equals(m_doc))) {
131                 String JavaDoc fmsg = XPATHMessages.createXPATHMessage(XPATHErrorResources.ER_WRONG_DOCUMENT, null);
132                 throw new DOMException JavaDoc(DOMException.WRONG_DOCUMENT_ERR, fmsg);
133             }
134             
135             // Check that the context node is an acceptable node type
136
short nodeType = contextNode.getNodeType();
137             if ((nodeType != Document.DOCUMENT_NODE) &&
138                 (nodeType != Document.ELEMENT_NODE) &&
139                 (nodeType != Document.ATTRIBUTE_NODE) &&
140                 (nodeType != Document.TEXT_NODE) &&
141                 (nodeType != Document.CDATA_SECTION_NODE) &&
142                 (nodeType != Document.COMMENT_NODE) &&
143                 (nodeType != Document.PROCESSING_INSTRUCTION_NODE) &&
144                 (nodeType != XPathNamespace.XPATH_NAMESPACE_NODE)) {
145                     String JavaDoc fmsg = XPATHMessages.createXPATHMessage(XPATHErrorResources.ER_WRONG_NODETYPE, null);
146                     throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, fmsg);
147             }
148         }
149             
150         //
151
// If the type is not a supported type, throw an exception and be
152
// done with it!
153
if (!XPathResultImpl.isValidType(type)) {
154             String JavaDoc fmsg = XPATHMessages.createXPATHMessage(XPATHErrorResources.ER_INVALID_XPATH_TYPE, new Object JavaDoc[] {new Integer JavaDoc(type)});
155             throw new XPathException(XPathException.TYPE_ERR,fmsg); // Invalid XPath type argument: {0}
156
}
157         
158         // Cache xpath context?
159
XPathContext xpathSupport = new XPathContext();
160         
161         // if m_document is not null, build the DTM from the document
162
if (null != m_doc) {
163             xpathSupport.getDTMHandleFromNode(m_doc);
164         }
165
166         XObject xobj = null;
167         try {
168             xobj = m_xpath.execute(xpathSupport, contextNode, null);
169         } catch (TransformerException JavaDoc te) {
170             // What should we do here?
171
throw new XPathException(XPathException.INVALID_EXPRESSION_ERR,te.getMessageAndLocation());
172         }
173
174         // Create a new XPathResult object
175
// Reuse result object passed in?
176
// The constructor will check the compatibility of type and xobj and
177
// throw an exception if they are not compatible.
178
return new XPathResultImpl(type,xobj,contextNode, m_xpath);
179     }
180
181 }
182
Popular Tags