dleslie 00/10/17 12:07:09
Modified: java/src/org/apache/xalan/lib Extensions.java
Removed: java/src/org/apache/xalan/lib NodeSetOperations.java
Log:
Moved extensions from NodeSetOperations into Extensions.
Revision Changes Path
1.2 +100 -0 xml-xalan/java/src/org/apache/xalan/lib/Extensions.java
Index: Extensions.java
===================================================================
RCS file: /home/cvs/xml-xalan/java/src/org/apache/xalan/lib/Extensions.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Extensions.java 2000/10/17 17:06:42 1.1
+++ Extensions.java 2000/10/17 19:07:09 1.2
@@ -57,10 +57,16 @@
package org.apache.xalan.lib;
+import org.w3c.dom.Node;
+import org.w3c.dom.Document;
+import org.w3c.dom.DocumentFragment;
+import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.w3c.dom.traversal.NodeIterator;
import org.apache.xpath.NodeSet;
+import java.util.Hashtable;
+
import org.apache.xalan.extensions.ExpressionContext;
/**
@@ -97,5 +103,99 @@
return new NodeSet(textNode);
}
}
+
+ /**
+ * Returns the intersection of two node-sets.
+ * @param n1 NodeIterator for first node-set
+ * @param ni2 NodeIterator for second node-set
+ * @return a NodeSet containing the nodes in ni1 that are also
+ * in ni2
+ */
+ public static NodeSet intersection(NodeIterator ni1, NodeIterator ni2)
+ throws org.xml.sax.SAXException
+ {
+ NodeSet ns1 = new NodeSet(ni1);
+ NodeSet ns2 = new NodeSet(ni2);
+ NodeSet inter= new NodeSet();
+ inter.setShouldCacheNodes(true);
+ for (int i=0; i < ns1.getLength(); i++)
+ {
+ Node n = ns1.elementAt(i);
+ if(ns2.contains(n))
+ inter.addElement(n);
+ }
+ return inter;
+ }
+
+ /**
+ * Returns the difference between two node-sets.
+ * @param n1 NodeIterator for first node-set
+ * @param ni2 NodeIterator for second node-set
+ * @return a NodeSet containing the nodes in ni1 that are not
+ * in ni2
+ */
+ public static NodeSet difference(NodeIterator ni1, NodeIterator ni2)
+ throws org.xml.sax.SAXException
+ {
+ NodeSet ns1 = new NodeSet(ni1);
+ NodeSet ns2 = new NodeSet(ni2);
+ NodeSet inter= new NodeSet();
+ NodeSet diff = new NodeSet();
+ diff.setShouldCacheNodes(true);
+ for (int i = 0; i < ns1.getLength(); i++)
+ {
+ Node n = ns1.elementAt(i);
+ if(!ns2.contains(n))
+ diff.addElement(n);
+ }
+ return diff;
+ }
+
+ /**
+ * Returns node-set containing distinct string values.
+ * @param ni NodeIterator for node-set
+ * @return a NodeSet with nodes from ni containing distinct string
values.
+ * In other words, if more than one node in ni contains the same string
value,
+ * only include the first such node found.
+ */
+ public static NodeSet distinct(NodeIterator ni)
+ throws org.xml.sax.SAXException
+ {
+ NodeSet ns = new NodeSet(ni);
+ NodeSet dist = new NodeSet();
+ dist.setShouldCacheNodes(true);
+ Hashtable stringTable = new Hashtable();
+ for (int i = 0; i < ns.getLength(); i++)
+ {
+ Node n = ns.elementAt(i);
+ String key = n.getNodeValue();
+ if (!stringTable.containsKey(key))
{
+ stringTable.put(key, n);
+ dist.addElement(n);
+ }
+ }
+ return dist;
+ }
+
+ /**
+ * Returns true of both node-sets contain the same set of nodes.
+ * @param n1 NodeIterator for first node-set
+ * @param ni2 NodeIterator for second node-set
+ * @return true if ni1 and ni2 contain exactly the same set of nodes.
+ */
+ public static boolean hasSameNodes(NodeIterator ni1, NodeIterator ni2)
+ {
+ NodeSet ns1 = new NodeSet(ni1);
+ NodeSet ns2 = new NodeSet(ni2);
+ if (ns1.getLength() != ns2.getLength())
+ return false;
+ for (int i = 0; i < ns1.getLength(); i++)
+ {
+ Node n = ns1.elementAt(i);
+ if(!ns2.contains(n))
+ return false;
+ }
+ return true;
+ }
}