1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 2002 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xalan" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 1999, Lotus
  53. * Development Corporation., http://www.lotus.com. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package org.apache.xpath.domapi;
  58. import javax.xml.transform.TransformerException;
  59. import org.apache.xalan.res.XSLMessages;
  60. import org.apache.xml.utils.PrefixResolver;
  61. import org.apache.xpath.XPath;
  62. import org.apache.xpath.XPathContext;
  63. import org.apache.xpath.objects.XObject;
  64. import org.apache.xpath.res.XPATHErrorResources;
  65. import org.w3c.dom.DOMException;
  66. import org.w3c.dom.Document;
  67. import org.w3c.dom.Node;
  68. import org.w3c.dom.xpath.*;
  69. /**
  70. *
  71. * The class provides an implementation of XPathExpression according
  72. * to the DOM L3 XPath Specification, Working Draft 28, March 2002.
  73. *
  74. * <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>
  75. * <p>The <code>XPathExpression</code> interface represents a parsed and resolved
  76. * XPath expression.</p>
  77. *
  78. * @see org.w3c.dom.xpath.XPathExpression
  79. */
  80. public class XPathExpressionImpl implements XPathExpression {
  81. private PrefixResolver m_resolver;
  82. /**
  83. * The xpath object that this expression wraps
  84. */
  85. private XPath m_xpath;
  86. /**
  87. * The document to be searched to parallel the case where the XPathEvaluator
  88. * is obtained by casting a Document.
  89. */
  90. private Document m_doc = null;
  91. /**
  92. * Constructor for XPathExpressionImpl.
  93. *
  94. * @param xpath The wrapped XPath object.
  95. * @param doc The document to be searched, to parallel the case where''
  96. * the XPathEvaluator is obtained by casting the document.
  97. */
  98. XPathExpressionImpl(XPath xpath, Document doc) {
  99. m_xpath = xpath;
  100. m_doc = doc;
  101. }
  102. /**
  103. * <meta name="usage" content="experimental"/>
  104. *
  105. * This method provides an implementation XPathResult.evaluate according
  106. * to the DOM L3 XPath Specification, Working Draft 28, March 2002.
  107. *
  108. * <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>
  109. *
  110. * <p>Evaluates this XPath expression and returns a result.</p>
  111. * @param contextNode The <code>context</code> is context node for the
  112. * evaluation of this XPath expression.If the XPathEvaluator was
  113. * obtained by casting the <code>Document</code> then this must be
  114. * owned by the same document and must be a <code>Document</code>,
  115. * <code>Element</code>, <code>Attribute</code>, <code>Text</code>,
  116. * <code>CDATASection</code>, <code>Comment</code>,
  117. * <code>ProcessingInstruction</code>, or <code>XPathNamespace</code>
  118. * node.If the context node is a <code>Text</code> or a
  119. * <code>CDATASection</code>, then the context is interpreted as the
  120. * whole logical text node as seen by XPath, unless the node is empty
  121. * in which case it may not serve as the XPath context.
  122. * @param type If a specific <code>type</code> is specified, then the
  123. * result will be coerced to return the specified type relying on
  124. * XPath conversions and fail if the desired coercion is not possible.
  125. * This must be one of the type codes of <code>XPathResult</code>.
  126. * @param result The <code>result</code> specifies a specific result
  127. * object which may be reused and returned by this method. If this is
  128. * specified as <code>null</code>or the implementation does not reuse
  129. * the specified result, a new result object will be constructed and
  130. * returned.For XPath 1.0 results, this object will be of type
  131. * <code>XPathResult</code>.
  132. * @return The result of the evaluation of the XPath expression.For XPath
  133. * 1.0 results, this object will be of type <code>XPathResult</code>.
  134. * @exception XPathException
  135. * TYPE_ERR: Raised if the result cannot be converted to return the
  136. * specified type.
  137. * @exception DOMException
  138. * WRONG_DOCUMENT_ERR: The Node is from a document that is not supported
  139. * by the XPathEvaluator that created this
  140. * <code>XPathExpression</code>.
  141. * <br>NOT_SUPPORTED_ERR: The Node is not a type permitted as an XPath
  142. * context node.
  143. *
  144. * @see org.w3c.dom.xpath.XPathExpression#evaluate(Node, short, XPathResult)
  145. */
  146. public Object evaluate(
  147. Node contextNode,
  148. short type,
  149. Object result)
  150. throws XPathException, DOMException {
  151. // If the XPathEvaluator was determined by "casting" the document
  152. if (m_doc != null) {
  153. // Check that the context node is owned by the same document
  154. if ((contextNode != m_doc) && (!contextNode.getOwnerDocument().equals(m_doc))) {
  155. String fmsg = XSLMessages.createXPATHMessage(XPATHErrorResources.ER_WRONG_DOCUMENT, null);
  156. throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, fmsg);
  157. }
  158. // Check that the context node is an acceptable node type
  159. short nodeType = contextNode.getNodeType();
  160. if ((nodeType != Document.DOCUMENT_NODE) &&
  161. (nodeType != Document.ELEMENT_NODE) &&
  162. (nodeType != Document.ATTRIBUTE_NODE) &&
  163. (nodeType != Document.TEXT_NODE) &&
  164. (nodeType != Document.CDATA_SECTION_NODE) &&
  165. (nodeType != Document.COMMENT_NODE) &&
  166. (nodeType != Document.PROCESSING_INSTRUCTION_NODE) &&
  167. (nodeType != XPathNamespace.XPATH_NAMESPACE_NODE)) {
  168. String fmsg = XSLMessages.createXPATHMessage(XPATHErrorResources.ER_WRONG_NODETYPE, null);
  169. throw new DOMException(DOMException.NOT_SUPPORTED_ERR, fmsg);
  170. }
  171. }
  172. //
  173. // If the type is not a supported type, throw an exception and be
  174. // done with it!
  175. if (!XPathResultImpl.isValidType(type)) {
  176. String fmsg = XSLMessages.createXPATHMessage(XPATHErrorResources.ER_INVALID_XPATH_TYPE, new Object[] {new Integer(type)});
  177. throw new XPathException(XPathException.TYPE_ERR,fmsg); // Invalid XPath type argument: {0}
  178. }
  179. // Cache xpath context?
  180. XPathContext xpathSupport = new XPathContext();
  181. // if m_document is not null, build the DTM from the document
  182. if (null != m_doc) {
  183. xpathSupport.getDTMHandleFromNode(m_doc);
  184. }
  185. XObject xobj = null;
  186. try {
  187. xobj = m_xpath.execute(xpathSupport, contextNode, m_resolver );
  188. } catch (TransformerException te) {
  189. // What should we do here?
  190. throw new XPathException(XPathException.INVALID_EXPRESSION_ERR,te.getMessageAndLocation());
  191. }
  192. // Create a new XPathResult object
  193. // Reuse result object passed in?
  194. // The constructor will check the compatibility of type and xobj and
  195. // throw an exception if they are not compatible.
  196. return new XPathResultImpl(type,xobj,contextNode);
  197. }
  198. }