1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999 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;
  58. import javax.xml.transform.TransformerException;
  59. import org.w3c.dom.Node;
  60. import org.w3c.dom.Document;
  61. import org.w3c.dom.traversal.NodeIterator;
  62. import org.w3c.dom.NodeList;
  63. import org.apache.xpath.XPathContext;
  64. import org.apache.xpath.XPath;
  65. import org.apache.xpath.compiler.XPathParser;
  66. import org.apache.xpath.XPathContext;
  67. import org.apache.xml.utils.PrefixResolverDefault;
  68. import org.apache.xml.utils.PrefixResolver;
  69. import org.apache.xpath.objects.XObject;
  70. import org.apache.xml.dtm.DTM;
  71. import org.apache.xml.dtm.ref.DTMNodeIterator;
  72. import org.apache.xml.dtm.ref.DTMNodeList;
  73. import org.apache.xml.dtm.ref.DTMManagerDefault;
  74. /**
  75. * The methods in this class are convenience methods into the
  76. * low-level XPath API.
  77. * These functions tend to be a little slow, since a number of objects must be
  78. * created for each evaluation. A faster way is to precompile the
  79. * XPaths using the low-level API, and then just use the XPaths
  80. * over and over.
  81. *
  82. * NOTE: In particular, each call to this method will create a new
  83. * XPathContext, a new DTMManager... and thus a new DTM. That's very
  84. * safe, since it guarantees that you're always processing against a
  85. * fully up-to-date view of your document. But it's also portentially
  86. * very expensive, since you're rebuilding the DTM every time. You should
  87. * consider using an instance of CachedXPathAPI rather than these static
  88. * methods.
  89. *
  90. * @see <a href="http://www.w3.org/TR/xpath">XPath Specification</a>
  91. * */
  92. public class XPathAPI
  93. {
  94. /**
  95. * Use an XPath string to select a single node. XPath namespace
  96. * prefixes are resolved from the context node, which may not
  97. * be what you want (see the next method).
  98. *
  99. * @param contextNode The node to start searching from.
  100. * @param str A valid XPath string.
  101. * @return The first node found that matches the XPath, or null.
  102. *
  103. * @throws TransformerException
  104. */
  105. public static Node selectSingleNode(Node contextNode, String str)
  106. throws TransformerException
  107. {
  108. return selectSingleNode(contextNode, str, contextNode);
  109. }
  110. /**
  111. * Use an XPath string to select a single node.
  112. * XPath namespace prefixes are resolved from the namespaceNode.
  113. *
  114. * @param contextNode The node to start searching from.
  115. * @param str A valid XPath string.
  116. * @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
  117. * @return The first node found that matches the XPath, or null.
  118. *
  119. * @throws TransformerException
  120. */
  121. public static Node selectSingleNode(
  122. Node contextNode, String str, Node namespaceNode)
  123. throws TransformerException
  124. {
  125. // Have the XObject return its result as a NodeSetDTM.
  126. NodeIterator nl = selectNodeIterator(contextNode, str, namespaceNode);
  127. // Return the first node, or null
  128. return nl.nextNode();
  129. }
  130. /**
  131. * Use an XPath string to select a nodelist.
  132. * XPath namespace prefixes are resolved from the contextNode.
  133. *
  134. * @param contextNode The node to start searching from.
  135. * @param str A valid XPath string.
  136. * @return A NodeIterator, should never be null.
  137. *
  138. * @throws TransformerException
  139. */
  140. public static NodeIterator selectNodeIterator(Node contextNode, String str)
  141. throws TransformerException
  142. {
  143. return selectNodeIterator(contextNode, str, contextNode);
  144. }
  145. /**
  146. * Use an XPath string to select a nodelist.
  147. * XPath namespace prefixes are resolved from the namespaceNode.
  148. *
  149. * @param contextNode The node to start searching from.
  150. * @param str A valid XPath string.
  151. * @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
  152. * @return A NodeIterator, should never be null.
  153. *
  154. * @throws TransformerException
  155. */
  156. public static NodeIterator selectNodeIterator(
  157. Node contextNode, String str, Node namespaceNode)
  158. throws TransformerException
  159. {
  160. // Execute the XPath, and have it return the result
  161. XObject list = eval(contextNode, str, namespaceNode);
  162. // Have the XObject return its result as a NodeSetDTM.
  163. return list.nodeset();
  164. }
  165. /**
  166. * Use an XPath string to select a nodelist.
  167. * XPath namespace prefixes are resolved from the contextNode.
  168. *
  169. * @param contextNode The node to start searching from.
  170. * @param str A valid XPath string.
  171. * @return A NodeIterator, should never be null.
  172. *
  173. * @throws TransformerException
  174. */
  175. public static NodeList selectNodeList(Node contextNode, String str)
  176. throws TransformerException
  177. {
  178. return selectNodeList(contextNode, str, contextNode);
  179. }
  180. /**
  181. * Use an XPath string to select a nodelist.
  182. * XPath namespace prefixes are resolved from the namespaceNode.
  183. *
  184. * @param contextNode The node to start searching from.
  185. * @param str A valid XPath string.
  186. * @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
  187. * @return A NodeIterator, should never be null.
  188. *
  189. * @throws TransformerException
  190. */
  191. public static NodeList selectNodeList(
  192. Node contextNode, String str, Node namespaceNode)
  193. throws TransformerException
  194. {
  195. // Execute the XPath, and have it return the result
  196. XObject list = eval(contextNode, str, namespaceNode);
  197. // Return a NodeList.
  198. return list.nodelist();
  199. }
  200. /**
  201. * Evaluate XPath string to an XObject. Using this method,
  202. * XPath namespace prefixes will be resolved from the namespaceNode.
  203. * @param contextNode The node to start searching from.
  204. * @param str A valid XPath string.
  205. * @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
  206. * @return An XObject, which can be used to obtain a string, number, nodelist, etc, should never be null.
  207. * @see org.apache.xpath.objects.XObject
  208. * @see org.apache.xpath.objects.XNull
  209. * @see org.apache.xpath.objects.XBoolean
  210. * @see org.apache.xpath.objects.XNumber
  211. * @see org.apache.xpath.objects.XString
  212. * @see org.apache.xpath.objects.XRTreeFrag
  213. *
  214. * @throws TransformerException
  215. */
  216. public static XObject eval(Node contextNode, String str)
  217. throws TransformerException
  218. {
  219. return eval(contextNode, str, contextNode);
  220. }
  221. /**
  222. * Evaluate XPath string to an XObject.
  223. * XPath namespace prefixes are resolved from the namespaceNode.
  224. * The implementation of this is a little slow, since it creates
  225. * a number of objects each time it is called. This could be optimized
  226. * to keep the same objects around, but then thread-safety issues would arise.
  227. *
  228. * @param contextNode The node to start searching from.
  229. * @param str A valid XPath string.
  230. * @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
  231. * @return An XObject, which can be used to obtain a string, number, nodelist, etc, should never be null.
  232. * @see org.apache.xpath.objects.XObject
  233. * @see org.apache.xpath.objects.XNull
  234. * @see org.apache.xpath.objects.XBoolean
  235. * @see org.apache.xpath.objects.XNumber
  236. * @see org.apache.xpath.objects.XString
  237. * @see org.apache.xpath.objects.XRTreeFrag
  238. *
  239. * @throws TransformerException
  240. */
  241. public static XObject eval(Node contextNode, String str, Node namespaceNode)
  242. throws TransformerException
  243. {
  244. // Since we don't have a XML Parser involved here, install some default support
  245. // for things like namespaces, etc.
  246. // (Changed from: XPathContext xpathSupport = new XPathContext();
  247. // because XPathContext is weak in a number of areas... perhaps
  248. // XPathContext should be done away with.)
  249. XPathContext xpathSupport = new XPathContext();
  250. // Create an object to resolve namespace prefixes.
  251. // XPath namespaces are resolved from the input context node's document element
  252. // if it is a root node, or else the current context node (for lack of a better
  253. // resolution space, given the simplicity of this sample code).
  254. PrefixResolverDefault prefixResolver = new PrefixResolverDefault(
  255. (namespaceNode.getNodeType() == Node.DOCUMENT_NODE)
  256. ? ((Document) namespaceNode).getDocumentElement() : namespaceNode);
  257. // Create the XPath object.
  258. XPath xpath = new XPath(str, null, prefixResolver, XPath.SELECT, null);
  259. // Execute the XPath, and have it return the result
  260. // return xpath.execute(xpathSupport, contextNode, prefixResolver);
  261. int ctxtNode = xpathSupport.getDTMHandleFromNode(contextNode);
  262. return xpath.execute(xpathSupport, ctxtNode, prefixResolver);
  263. }
  264. /**
  265. * Evaluate XPath string to an XObject.
  266. * XPath namespace prefixes are resolved from the namespaceNode.
  267. * The implementation of this is a little slow, since it creates
  268. * a number of objects each time it is called. This could be optimized
  269. * to keep the same objects around, but then thread-safety issues would arise.
  270. *
  271. * @param contextNode The node to start searching from.
  272. * @param str A valid XPath string.
  273. * @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
  274. * @param prefixResolver Will be called if the parser encounters namespace
  275. * prefixes, to resolve the prefixes to URLs.
  276. * @return An XObject, which can be used to obtain a string, number, nodelist, etc, should never be null.
  277. * @see org.apache.xpath.objects.XObject
  278. * @see org.apache.xpath.objects.XNull
  279. * @see org.apache.xpath.objects.XBoolean
  280. * @see org.apache.xpath.objects.XNumber
  281. * @see org.apache.xpath.objects.XString
  282. * @see org.apache.xpath.objects.XRTreeFrag
  283. *
  284. * @throws TransformerException
  285. */
  286. public static XObject eval(
  287. Node contextNode, String str, PrefixResolver prefixResolver)
  288. throws TransformerException
  289. {
  290. // Since we don't have a XML Parser involved here, install some default support
  291. // for things like namespaces, etc.
  292. // (Changed from: XPathContext xpathSupport = new XPathContext();
  293. // because XPathContext is weak in a number of areas... perhaps
  294. // XPathContext should be done away with.)
  295. // Create the XPath object.
  296. XPath xpath = new XPath(str, null, prefixResolver, XPath.SELECT, null);
  297. // Execute the XPath, and have it return the result
  298. XPathContext xpathSupport = new XPathContext();
  299. int ctxtNode = xpathSupport.getDTMHandleFromNode(contextNode);
  300. return xpath.execute(xpathSupport, ctxtNode, prefixResolver);
  301. }
  302. }