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.res.XPATHErrorResources;
  63. import org.w3c.dom.DOMException;
  64. import org.w3c.dom.Document;
  65. import org.w3c.dom.Node;
  66. import org.w3c.dom.xpath.*;
  67. /**
  68. * <meta name="usage" content="experimental"/>
  69. *
  70. * The class provides an implementation of XPathEvaluator according
  71. * to the DOM L3 XPath Specification, Working Draft 28, March 2002.
  72. *
  73. * <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>
  74. *
  75. * </p>The evaluation of XPath expressions is provided by
  76. * <code>XPathEvaluator</code>, which will provide evaluation of XPath 1.0
  77. * expressions with no specialized extension functions or variables. It is
  78. * expected that the <code>XPathEvaluator</code> interface will be
  79. * implemented on the same object which implements the <code>Document</code>
  80. * interface in an implementation which supports the XPath DOM module.
  81. * <code>XPathEvaluator</code> implementations may be available from other
  82. * sources that may provide support for special extension functions or
  83. * variables which are not defined in this specification.</p>
  84. *
  85. * @see org.w3c.dom.xpath.XPathEvaluator
  86. *
  87. */
  88. public class XPathEvaluatorImpl implements XPathEvaluator {
  89. /**
  90. * This prefix resolver is created whenever null is passed to the
  91. * evaluate method. Its purpose is to satisfy the DOM L3 XPath API
  92. * requirement that if a null prefix resolver is used, an exception
  93. * should only be thrown when an attempt is made to resolve a prefix.
  94. */
  95. class DummyPrefixResolver implements PrefixResolver {
  96. /**
  97. * Constructor for DummyPrefixResolver.
  98. */
  99. public DummyPrefixResolver() {}
  100. /**
  101. * @exception DOMException
  102. * NAMESPACE_ERR: Always throws this exceptionn
  103. *
  104. * @see org.apache.xml.utils.PrefixResolver#getNamespaceForPrefix(String, Node)
  105. */
  106. public String getNamespaceForPrefix(String prefix, Node context) {
  107. String fmsg = XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NULL_RESOLVER, null);
  108. throw new DOMException(DOMException.NAMESPACE_ERR, fmsg); // Unable to resolve prefix with null prefix resolver.
  109. }
  110. /**
  111. * @exception DOMException
  112. * NAMESPACE_ERR: Always throws this exceptionn
  113. *
  114. * @see org.apache.xml.utils.PrefixResolver#getNamespaceForPrefix(String)
  115. */
  116. public String getNamespaceForPrefix(String prefix) {
  117. return getNamespaceForPrefix(prefix,null);
  118. }
  119. /**
  120. * @see org.apache.xml.utils.PrefixResolver#handlesNullPrefixes()
  121. */
  122. public boolean handlesNullPrefixes() {
  123. return false;
  124. }
  125. /**
  126. * @see org.apache.xml.utils.PrefixResolver#getBaseIdentifier()
  127. */
  128. public String getBaseIdentifier() {
  129. return null;
  130. }
  131. }
  132. /**
  133. * The document to be searched to parallel the case where the XPathEvaluator
  134. * is obtained by casting a Document.
  135. */
  136. private Document m_doc = null;
  137. /**
  138. * Constructor for XPathEvaluatorImpl.
  139. */
  140. public XPathEvaluatorImpl() {
  141. super();
  142. }
  143. /**
  144. * Constructor for XPathEvaluatorImpl.
  145. *
  146. * @param doc The document to be searched, to parallel the case where''
  147. * the XPathEvaluator is obtained by casting the document.
  148. */
  149. public XPathEvaluatorImpl(Document doc) {
  150. m_doc = doc;
  151. }
  152. /**
  153. * Creates a parsed XPath expression with resolved namespaces. This is
  154. * useful when an expression will be reused in an application since it
  155. * makes it possible to compile the expression string into a more
  156. * efficient internal form and preresolve all namespace prefixes which
  157. * occur within the expression.
  158. *
  159. * @param expression The XPath expression string to be parsed.
  160. * @param resolver The <code>resolver</code> permits translation of
  161. * prefixes within the XPath expression into appropriate namespace URIs
  162. * . If this is specified as <code>null</code>, any namespace prefix
  163. * within the expression will result in <code>DOMException</code>
  164. * being thrown with the code <code>NAMESPACE_ERR</code>.
  165. * @return The compiled form of the XPath expression.
  166. * @exception XPathException
  167. * INVALID_EXPRESSION_ERR: Raised if the expression is not legal
  168. * according to the rules of the <code>XPathEvaluator</code>i
  169. * @exception DOMException
  170. * NAMESPACE_ERR: Raised if the expression contains namespace prefixes
  171. * which cannot be resolved by the specified
  172. * <code>XPathNSResolver</code>.
  173. *
  174. * @see org.w3c.dom.xpath.XPathEvaluator#createExpression(String, XPathNSResolver)
  175. */
  176. public XPathExpression createExpression(
  177. String expression,
  178. XPathNSResolver resolver)
  179. throws XPathException, DOMException {
  180. try {
  181. // If the resolver is null, create a dummy prefix resolver
  182. XPath xpath = new XPath(expression,null,
  183. ((null == resolver) ? new DummyPrefixResolver() : ((PrefixResolver)resolver)),
  184. XPath.SELECT);
  185. return new XPathExpressionImpl(xpath, m_doc);
  186. } catch (TransformerException e) {
  187. throw new DOMException(XPathException.INVALID_EXPRESSION_ERR,e.getMessageAndLocation());
  188. }
  189. }
  190. /**
  191. * Adapts any DOM node to resolve namespaces so that an XPath expression
  192. * can be easily evaluated relative to the context of the node where it
  193. * appeared within the document. This adapter works like the DOM Level 3
  194. * method <code>lookupNamespaceURI</code> on nodes in resolving the
  195. * namespaceURI from a given prefix using the current information available
  196. * in the node's hierarchy at the time lookupNamespaceURI is called, also
  197. * correctly resolving the implicit xml prefix.
  198. *
  199. * @param nodeResolver The node to be used as a context for namespace
  200. * resolution.
  201. * @return <code>XPathNSResolver</code> which resolves namespaces with
  202. * respect to the definitions in scope for a specified node.
  203. *
  204. * @see org.w3c.dom.xpath.XPathEvaluator#createNSResolver(Node)
  205. */
  206. public XPathNSResolver createNSResolver(Node nodeResolver) {
  207. return new XPathNSResolverImpl((nodeResolver.getNodeType() == Node.DOCUMENT_NODE)
  208. ? ((Document) nodeResolver).getDocumentElement() : nodeResolver);
  209. }
  210. /**
  211. * Evaluates an XPath expression string and returns a result of the
  212. * specified type if possible.
  213. *
  214. * @param expression The XPath expression string to be parsed and
  215. * evaluated.
  216. * @param contextNode The <code>context</code> is context node for the
  217. * evaluation of this XPath expression. If the XPathEvaluator was
  218. * obtained by casting the <code>Document</code> then this must be
  219. * owned by the same document and must be a <code>Document</code>,
  220. * <code>Element</code>, <code>Attribute</code>, <code>Text</code>,
  221. * <code>CDATASection</code>, <code>Comment</code>,
  222. * <code>ProcessingInstruction</code>, or <code>XPathNamespace</code>
  223. * node. If the context node is a <code>Text</code> or a
  224. * <code>CDATASection</code>, then the context is interpreted as the
  225. * whole logical text node as seen by XPath, unless the node is empty
  226. * in which case it may not serve as the XPath context.
  227. * @param resolver The <code>resolver</code> permits translation of
  228. * prefixes within the XPath expression into appropriate namespace URIs
  229. * . If this is specified as <code>null</code>, any namespace prefix
  230. * within the expression will result in <code>DOMException</code>
  231. * being thrown with the code <code>NAMESPACE_ERR</code>.
  232. * @param type If a specific <code>type</code> is specified, then the
  233. * result will be coerced to return the specified type relying on
  234. * XPath type conversions and fail if the desired coercion is not
  235. * possible. This must be one of the type codes of
  236. * <code>XPathResult</code>.
  237. * @param result The <code>result</code> specifies a specific result
  238. * object which may be reused and returned by this method. If this is
  239. * specified as <code>null</code>or the implementation does not reuse
  240. * the specified result, a new result object will be constructed and
  241. * returned.For XPath 1.0 results, this object will be of type
  242. * <code>XPathResult</code>.
  243. * @return The result of the evaluation of the XPath expression.For XPath
  244. * 1.0 results, this object will be of type <code>XPathResult</code>.
  245. * @exception XPathException
  246. * INVALID_EXPRESSION_ERR: Raised if the expression is not legal
  247. * according to the rules of the <code>XPathEvaluator</code>i
  248. * <br>TYPE_ERR: Raised if the result cannot be converted to return the
  249. * specified type.
  250. * @exception DOMException
  251. * NAMESPACE_ERR: Raised if the expression contains namespace prefixes
  252. * which cannot be resolved by the specified
  253. * <code>XPathNSResolver</code>.
  254. * <br>WRONG_DOCUMENT_ERR: The Node is from a document that is not
  255. * supported by this XPathEvaluator.
  256. * <br>NOT_SUPPORTED_ERR: The Node is not a type permitted as an XPath
  257. * context node.
  258. *
  259. * @see org.w3c.dom.xpath.XPathEvaluator#evaluate(String, Node, XPathNSResolver, short, XPathResult)
  260. */
  261. public Object evaluate(
  262. String expression,
  263. Node contextNode,
  264. XPathNSResolver resolver,
  265. short type,
  266. Object result)
  267. throws XPathException, DOMException {
  268. XPathExpression xpathExpression = createExpression(expression, resolver);
  269. return xpathExpression.evaluate(contextNode, type, result);
  270. }
  271. }