1. /*
  2. * Copyright 1999-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: CachedXPathAPI.java,v 1.5 2004/02/17 04:30:02 minchau Exp $
  18. */
  19. package com.sun.org.apache.xpath.internal;
  20. import javax.xml.transform.TransformerException;
  21. import com.sun.org.apache.xml.internal.utils.PrefixResolver;
  22. import com.sun.org.apache.xml.internal.utils.PrefixResolverDefault;
  23. import com.sun.org.apache.xpath.internal.objects.XObject;
  24. import org.w3c.dom.Document;
  25. import org.w3c.dom.Node;
  26. import org.w3c.dom.NodeList;
  27. import org.w3c.dom.traversal.NodeIterator;
  28. /**
  29. * The methods in this class are convenience methods into the
  30. * low-level XPath API.
  31. *
  32. * These functions tend to be a little slow, since a number of objects must be
  33. * created for each evaluation. A faster way is to precompile the
  34. * XPaths using the low-level API, and then just use the XPaths
  35. * over and over.
  36. *
  37. * This is an alternative for the old XPathAPI class, which provided
  38. * static methods for the purpose but had the drawback of
  39. * instantiating a new XPathContext (and thus building a new DTMManager,
  40. * and new DTMs) each time it was called. XPathAPIObject instead retains
  41. * its context as long as the object persists, reusing the DTMs. This
  42. * does have a downside: if you've changed your source document, you should
  43. * obtain a new XPathAPIObject to continue searching it, since trying to use
  44. * the old DTMs will probably yield bad results or malfunction outright... and
  45. * the cached DTMs may consume memory until this object and its context are
  46. * returned to the heap. Essentially, it's the caller's responsibility to
  47. * decide when to discard the cache.
  48. *
  49. * @see <a href="http://www.w3.org/TR/xpath">XPath Specification</a>
  50. * */
  51. public class CachedXPathAPI
  52. {
  53. /** XPathContext, and thus the document model system (DTMs), persists through multiple
  54. calls to this object. This is set in the constructor.
  55. */
  56. protected XPathContext xpathSupport;
  57. /** Default constructor. Establishes its own XPathContext, and hence
  58. * its own DTMManager. Good choice for simple uses.
  59. * */
  60. public CachedXPathAPI()
  61. {
  62. xpathSupport = new XPathContext();
  63. }
  64. /** This constructor shares its XPathContext with a pre-existing
  65. * CachedXPathAPI. That allows sharing document models (DTMs) and
  66. * previously established location state.
  67. *
  68. * Note that the original CachedXPathAPI and the new one should not
  69. * be operated concurrently; we do not support multithreaded access
  70. * to a single DTM at this time.
  71. *
  72. * %REVIEW% Should this instead do a clone-and-reset on the XPathSupport object?
  73. * */
  74. public CachedXPathAPI(CachedXPathAPI priorXPathAPI)
  75. {
  76. xpathSupport = priorXPathAPI.xpathSupport;
  77. }
  78. /** Returns the XPathSupport object used in this CachedXPathAPI
  79. *
  80. * %REVIEW% I'm somewhat concerned about the loss of encapsulation
  81. * this causes, but the xml-security folks say they need it.
  82. * */
  83. public XPathContext getXPathContext()
  84. {
  85. return this.xpathSupport;
  86. }
  87. /**
  88. * Use an XPath string to select a single node. XPath namespace
  89. * prefixes are resolved from the context node, which may not
  90. * be what you want (see the next method).
  91. *
  92. * @param contextNode The node to start searching from.
  93. * @param str A valid XPath string.
  94. * @return The first node found that matches the XPath, or null.
  95. *
  96. * @throws TransformerException
  97. */
  98. public Node selectSingleNode(Node contextNode, String str)
  99. throws TransformerException
  100. {
  101. return selectSingleNode(contextNode, str, contextNode);
  102. }
  103. /**
  104. * Use an XPath string to select a single node.
  105. * XPath namespace prefixes are resolved from the namespaceNode.
  106. *
  107. * @param contextNode The node to start searching from.
  108. * @param str A valid XPath string.
  109. * @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
  110. * @return The first node found that matches the XPath, or null.
  111. *
  112. * @throws TransformerException
  113. */
  114. public Node selectSingleNode(
  115. Node contextNode, String str, Node namespaceNode)
  116. throws TransformerException
  117. {
  118. // Have the XObject return its result as a NodeSetDTM.
  119. NodeIterator nl = selectNodeIterator(contextNode, str, namespaceNode);
  120. // Return the first node, or null
  121. return nl.nextNode();
  122. }
  123. /**
  124. * Use an XPath string to select a nodelist.
  125. * XPath namespace prefixes are resolved from the contextNode.
  126. *
  127. * @param contextNode The node to start searching from.
  128. * @param str A valid XPath string.
  129. * @return A NodeIterator, should never be null.
  130. *
  131. * @throws TransformerException
  132. */
  133. public NodeIterator selectNodeIterator(Node contextNode, String str)
  134. throws TransformerException
  135. {
  136. return selectNodeIterator(contextNode, str, contextNode);
  137. }
  138. /**
  139. * Use an XPath string to select a nodelist.
  140. * XPath namespace prefixes are resolved from the namespaceNode.
  141. *
  142. * @param contextNode The node to start searching from.
  143. * @param str A valid XPath string.
  144. * @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
  145. * @return A NodeIterator, should never be null.
  146. *
  147. * @throws TransformerException
  148. */
  149. public NodeIterator selectNodeIterator(
  150. Node contextNode, String str, Node namespaceNode)
  151. throws TransformerException
  152. {
  153. // Execute the XPath, and have it return the result
  154. XObject list = eval(contextNode, str, namespaceNode);
  155. // Have the XObject return its result as a NodeSetDTM.
  156. return list.nodeset();
  157. }
  158. /**
  159. * Use an XPath string to select a nodelist.
  160. * XPath namespace prefixes are resolved from the contextNode.
  161. *
  162. * @param contextNode The node to start searching from.
  163. * @param str A valid XPath string.
  164. * @return A NodeIterator, should never be null.
  165. *
  166. * @throws TransformerException
  167. */
  168. public NodeList selectNodeList(Node contextNode, String str)
  169. throws TransformerException
  170. {
  171. return selectNodeList(contextNode, str, contextNode);
  172. }
  173. /**
  174. * Use an XPath string to select a nodelist.
  175. * XPath namespace prefixes are resolved from the namespaceNode.
  176. *
  177. * @param contextNode The node to start searching from.
  178. * @param str A valid XPath string.
  179. * @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
  180. * @return A NodeIterator, should never be null.
  181. *
  182. * @throws TransformerException
  183. */
  184. public NodeList selectNodeList(
  185. Node contextNode, String str, Node namespaceNode)
  186. throws TransformerException
  187. {
  188. // Execute the XPath, and have it return the result
  189. XObject list = eval(contextNode, str, namespaceNode);
  190. // Return a NodeList.
  191. return list.nodelist();
  192. }
  193. /**
  194. * Evaluate XPath string to an XObject. Using this method,
  195. * XPath namespace prefixes will be resolved from the namespaceNode.
  196. * @param contextNode The node to start searching from.
  197. * @param str A valid XPath string.
  198. * @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
  199. * @return An XObject, which can be used to obtain a string, number, nodelist, etc, should never be null.
  200. * @see com.sun.org.apache.xpath.internal.objects.XObject
  201. * @see com.sun.org.apache.xpath.internal.objects.XNull
  202. * @see com.sun.org.apache.xpath.internal.objects.XBoolean
  203. * @see com.sun.org.apache.xpath.internal.objects.XNumber
  204. * @see com.sun.org.apache.xpath.internal.objects.XString
  205. * @see com.sun.org.apache.xpath.internal.objects.XRTreeFrag
  206. *
  207. * @throws TransformerException
  208. */
  209. public XObject eval(Node contextNode, String str)
  210. throws TransformerException
  211. {
  212. return eval(contextNode, str, contextNode);
  213. }
  214. /**
  215. * Evaluate XPath string to an XObject.
  216. * XPath namespace prefixes are resolved from the namespaceNode.
  217. * The implementation of this is a little slow, since it creates
  218. * a number of objects each time it is called. This could be optimized
  219. * to keep the same objects around, but then thread-safety issues would arise.
  220. *
  221. * @param contextNode The node to start searching from.
  222. * @param str A valid XPath string.
  223. * @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
  224. * @return An XObject, which can be used to obtain a string, number, nodelist, etc, should never be null.
  225. * @see com.sun.org.apache.xpath.internal.objects.XObject
  226. * @see com.sun.org.apache.xpath.internal.objects.XNull
  227. * @see com.sun.org.apache.xpath.internal.objects.XBoolean
  228. * @see com.sun.org.apache.xpath.internal.objects.XNumber
  229. * @see com.sun.org.apache.xpath.internal.objects.XString
  230. * @see com.sun.org.apache.xpath.internal.objects.XRTreeFrag
  231. *
  232. * @throws TransformerException
  233. */
  234. public XObject eval(Node contextNode, String str, Node namespaceNode)
  235. throws TransformerException
  236. {
  237. // Since we don't have a XML Parser involved here, install some default support
  238. // for things like namespaces, etc.
  239. // (Changed from: XPathContext xpathSupport = new XPathContext();
  240. // because XPathContext is weak in a number of areas... perhaps
  241. // XPathContext should be done away with.)
  242. // Create an object to resolve namespace prefixes.
  243. // XPath namespaces are resolved from the input context node's document element
  244. // if it is a root node, or else the current context node (for lack of a better
  245. // resolution space, given the simplicity of this sample code).
  246. PrefixResolverDefault prefixResolver = new PrefixResolverDefault(
  247. (namespaceNode.getNodeType() == Node.DOCUMENT_NODE)
  248. ? ((Document) namespaceNode).getDocumentElement() : namespaceNode);
  249. // Create the XPath object.
  250. XPath xpath = new XPath(str, null, prefixResolver, XPath.SELECT, null);
  251. // Execute the XPath, and have it return the result
  252. // return xpath.execute(xpathSupport, contextNode, prefixResolver);
  253. int ctxtNode = xpathSupport.getDTMHandleFromNode(contextNode);
  254. return xpath.execute(xpathSupport, ctxtNode, prefixResolver);
  255. }
  256. /**
  257. * Evaluate XPath string to an XObject.
  258. * XPath namespace prefixes are resolved from the namespaceNode.
  259. * The implementation of this is a little slow, since it creates
  260. * a number of objects each time it is called. This could be optimized
  261. * to keep the same objects around, but then thread-safety issues would arise.
  262. *
  263. * @param contextNode The node to start searching from.
  264. * @param str A valid XPath string.
  265. * @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
  266. * @param prefixResolver Will be called if the parser encounters namespace
  267. * prefixes, to resolve the prefixes to URLs.
  268. * @return An XObject, which can be used to obtain a string, number, nodelist, etc, should never be null.
  269. * @see com.sun.org.apache.xpath.internal.objects.XObject
  270. * @see com.sun.org.apache.xpath.internal.objects.XNull
  271. * @see com.sun.org.apache.xpath.internal.objects.XBoolean
  272. * @see com.sun.org.apache.xpath.internal.objects.XNumber
  273. * @see com.sun.org.apache.xpath.internal.objects.XString
  274. * @see com.sun.org.apache.xpath.internal.objects.XRTreeFrag
  275. *
  276. * @throws TransformerException
  277. */
  278. public XObject eval(
  279. Node contextNode, String str, PrefixResolver prefixResolver)
  280. throws TransformerException
  281. {
  282. // Since we don't have a XML Parser involved here, install some default support
  283. // for things like namespaces, etc.
  284. // (Changed from: XPathContext xpathSupport = new XPathContext();
  285. // because XPathContext is weak in a number of areas... perhaps
  286. // XPathContext should be done away with.)
  287. // Create the XPath object.
  288. XPath xpath = new XPath(str, null, prefixResolver, XPath.SELECT, null);
  289. // Execute the XPath, and have it return the result
  290. XPathContext xpathSupport = new XPathContext();
  291. int ctxtNode = xpathSupport.getDTMHandleFromNode(contextNode);
  292. return xpath.execute(xpathSupport, ctxtNode, prefixResolver);
  293. }
  294. }