KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xpath > domapi > XPathExpressionImpl


1 /*
2  * Copyright 2002-2004 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$
18  */

19
20
21 package org.apache.xpath.domapi;
22
23 import javax.xml.transform.TransformerException JavaDoc;
24
25 import org.apache.xalan.res.XSLMessages;
26 import org.apache.xml.utils.PrefixResolver;
27 import org.apache.xpath.XPath;
28 import org.apache.xpath.XPathContext;
29 import org.apache.xpath.objects.XObject;
30 import org.apache.xpath.res.XPATHErrorResources;
31
32 import org.w3c.dom.DOMException JavaDoc;
33 import org.w3c.dom.Document JavaDoc;
34 import org.w3c.dom.Node JavaDoc;
35 import org.w3c.dom.xpath.XPathException;
36 import org.w3c.dom.xpath.XPathExpression;
37 import org.w3c.dom.xpath.XPathNamespace;
38
39 /**
40  *
41  * The class provides an implementation of XPathExpression according
42  * to the DOM L3 XPath Specification, Working Draft 28, March 2002.
43  *
44  * <p>See also the <a HREF='http://www.w3.org/TR/2002/WD-DOM-Level-3-XPath-20020328'>Document Object Model (DOM) Level 3 XPath Specification</a>.</p>
45
46  * <p>The <code>XPathExpression</code> interface represents a parsed and resolved
47  * XPath expression.</p>
48  *
49  * @see org.w3c.dom.xpath.XPathExpression
50  */

51 public class XPathExpressionImpl implements XPathExpression {
52
53   private PrefixResolver m_resolver;
54   
55   /**
56    * The xpath object that this expression wraps
57    */

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

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

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

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