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.xalan.lib;
  58. import org.w3c.dom.Node;
  59. import org.w3c.dom.NodeList;
  60. import org.apache.xpath.NodeSet;
  61. import org.apache.xpath.DOMHelper;
  62. import java.util.Hashtable;
  63. import javax.xml.parsers.*;
  64. /**
  65. * <meta name="usage" content="general"/>
  66. * This class contains EXSLT set extension functions.
  67. * It is accessed by specifying a namespace URI as follows:
  68. * <pre>
  69. * xmlns:set="http://exslt.org/sets"
  70. * </pre>
  71. *
  72. * The documentation for each function has been copied from the relevant
  73. * EXSLT Implementer page.
  74. *
  75. * @see <a href="http://www.exslt.org/">EXSLT</a>
  76. */
  77. public class ExsltSets extends ExsltBase
  78. {
  79. /**
  80. * The set:leading function returns the nodes in the node set passed as the first argument that
  81. * precede, in document order, the first node in the node set passed as the second argument. If
  82. * the first node in the second node set is not contained in the first node set, then an empty
  83. * node set is returned. If the second node set is empty, then the first node set is returned.
  84. *
  85. * @param nl1 NodeList for first node-set.
  86. * @param nl2 NodeList for second node-set.
  87. * @return a NodeList containing the nodes in nl1 that precede in document order the first
  88. * node in nl2; an empty node-set if the first node in nl2 is not in nl1; all of nl1 if nl2
  89. * is empty.
  90. *
  91. * @see <a href="http://www.exslt.org/">EXSLT</a>
  92. */
  93. public static NodeList leading (NodeList nl1, NodeList nl2)
  94. {
  95. if (nl2.getLength() == 0)
  96. return nl1;
  97. NodeSet ns1 = new NodeSet(nl1);
  98. NodeSet leadNodes = new NodeSet();
  99. Node endNode = nl2.item(0);
  100. if (!ns1.contains(endNode))
  101. return leadNodes; // empty NodeSet
  102. for (int i = 0; i < nl1.getLength(); i++)
  103. {
  104. Node testNode = nl1.item(i);
  105. if (DOMHelper.isNodeAfter(testNode, endNode)
  106. && !DOMHelper.isNodeTheSame(testNode, endNode))
  107. leadNodes.addElement(testNode);
  108. }
  109. return leadNodes;
  110. }
  111. /**
  112. * The set:trailing function returns the nodes in the node set passed as the first argument that
  113. * follow, in document order, the first node in the node set passed as the second argument. If
  114. * the first node in the second node set is not contained in the first node set, then an empty
  115. * node set is returned. If the second node set is empty, then the first node set is returned.
  116. *
  117. * @param nl1 NodeList for first node-set.
  118. * @param nl2 NodeList for second node-set.
  119. * @return a NodeList containing the nodes in nl1 that follow in document order the first
  120. * node in nl2; an empty node-set if the first node in nl2 is not in nl1; all of nl1 if nl2
  121. * is empty.
  122. *
  123. * @see <a href="http://www.exslt.org/">EXSLT</a>
  124. */
  125. public static NodeList trailing (NodeList nl1, NodeList nl2)
  126. {
  127. if (nl2.getLength() == 0)
  128. return nl1;
  129. NodeSet ns1 = new NodeSet(nl1);
  130. NodeSet trailNodes = new NodeSet();
  131. Node startNode = nl2.item(0);
  132. if (!ns1.contains(startNode))
  133. return trailNodes; // empty NodeSet
  134. for (int i = 0; i < nl1.getLength(); i++)
  135. {
  136. Node testNode = nl1.item(i);
  137. if (DOMHelper.isNodeAfter(startNode, testNode)
  138. && !DOMHelper.isNodeTheSame(startNode, testNode))
  139. trailNodes.addElement(testNode);
  140. }
  141. return trailNodes;
  142. }
  143. /**
  144. * The set:intersection function returns a node set comprising the nodes that are within
  145. * both the node sets passed as arguments to it.
  146. *
  147. * @param nl1 NodeList for first node-set.
  148. * @param nl2 NodeList for second node-set.
  149. * @return a NodeList containing the nodes in nl1 that are also
  150. * in nl2.
  151. *
  152. * @see <a href="http://www.exslt.org/">EXSLT</a>
  153. */
  154. public static NodeList intersection(NodeList nl1, NodeList nl2)
  155. {
  156. NodeSet ns1 = new NodeSet(nl1);
  157. NodeSet ns2 = new NodeSet(nl2);
  158. NodeSet inter = new NodeSet();
  159. inter.setShouldCacheNodes(true);
  160. for (int i = 0; i < ns1.getLength(); i++)
  161. {
  162. Node n = ns1.elementAt(i);
  163. if (ns2.contains(n))
  164. inter.addElement(n);
  165. }
  166. return inter;
  167. }
  168. /**
  169. * The set:difference function returns the difference between two node sets - those nodes that
  170. * are in the node set passed as the first argument that are not in the node set passed as the
  171. * second argument.
  172. *
  173. * @param nl1 NodeList for first node-set.
  174. * @param nl2 NodeList for second node-set.
  175. * @return a NodeList containing the nodes in nl1 that are not in nl2.
  176. *
  177. * @see <a href="http://www.exslt.org/">EXSLT</a>
  178. */
  179. public static NodeList difference(NodeList nl1, NodeList nl2)
  180. {
  181. NodeSet ns1 = new NodeSet(nl1);
  182. NodeSet ns2 = new NodeSet(nl2);
  183. NodeSet diff = new NodeSet();
  184. diff.setShouldCacheNodes(true);
  185. for (int i = 0; i < ns1.getLength(); i++)
  186. {
  187. Node n = ns1.elementAt(i);
  188. if (!ns2.contains(n))
  189. diff.addElement(n);
  190. }
  191. return diff;
  192. }
  193. /**
  194. * The set:distinct function returns a subset of the nodes contained in the node-set NS passed
  195. * as the first argument. Specifically, it selects a node N if there is no node in NS that has
  196. * the same string value as N, and that precedes N in document order.
  197. *
  198. * @param nl NodeList for the node-set.
  199. * @return a NodeList with nodes from nl containing distinct string values.
  200. * In other words, if more than one node in nl contains the same string value,
  201. * only include the first such node found.
  202. *
  203. * @see <a href="http://www.exslt.org/">EXSLT</a>
  204. */
  205. public static NodeList distinct(NodeList nl)
  206. {
  207. NodeSet dist = new NodeSet();
  208. dist.setShouldCacheNodes(true);
  209. Hashtable stringTable = new Hashtable();
  210. for (int i = 0; i < nl.getLength(); i++)
  211. {
  212. Node currNode = nl.item(i);
  213. String key = toString(currNode);
  214. if (key == null)
  215. dist.addElement(currNode);
  216. else if (!stringTable.containsKey(key))
  217. {
  218. stringTable.put(key, currNode);
  219. dist.addElement(currNode);
  220. }
  221. }
  222. return dist;
  223. }
  224. /**
  225. * The set:has-same-node function returns true if the node set passed as the first argument shares
  226. * any nodes with the node set passed as the second argument. If there are no nodes that are in both
  227. * node sets, then it returns false.
  228. *
  229. * The Xalan extensions MethodResolver converts 'has-same-node' to 'hasSameNode'.
  230. *
  231. * Note: Not to be confused with hasSameNodes in the Xalan namespace, which returns true if
  232. * the two node sets contain the exactly the same nodes (perhaps in a different order),
  233. * otherwise false.
  234. *
  235. * @see <a href="http://www.exslt.org/">EXSLT</a>
  236. */
  237. public static boolean hasSameNode(NodeList nl1, NodeList nl2)
  238. {
  239. NodeSet ns1 = new NodeSet(nl1);
  240. NodeSet ns2 = new NodeSet(nl2);
  241. for (int i = 0; i < ns1.getLength(); i++)
  242. {
  243. if (ns2.contains(ns1.elementAt(i)))
  244. return true;
  245. }
  246. return false;
  247. }
  248. }