1 16 19 package org.apache.xalan.lib; 20 21 import java.util.Hashtable ; 22 23 import org.apache.xml.utils.DOMHelper; 24 import org.apache.xpath.NodeSet; 25 import org.w3c.dom.Node ; 26 import org.w3c.dom.NodeList ; 27 28 41 public class ExsltSets extends ExsltBase 42 { 43 57 public static NodeList leading (NodeList nl1, NodeList nl2) 58 { 59 if (nl2.getLength() == 0) 60 return nl1; 61 62 NodeSet ns1 = new NodeSet(nl1); 63 NodeSet leadNodes = new NodeSet(); 64 Node endNode = nl2.item(0); 65 if (!ns1.contains(endNode)) 66 return leadNodes; 68 for (int i = 0; i < nl1.getLength(); i++) 69 { 70 Node testNode = nl1.item(i); 71 if (DOMHelper.isNodeAfter(testNode, endNode) 72 && !DOMHelper.isNodeTheSame(testNode, endNode)) 73 leadNodes.addElement(testNode); 74 } 75 return leadNodes; 76 } 77 78 92 public static NodeList trailing (NodeList nl1, NodeList nl2) 93 { 94 if (nl2.getLength() == 0) 95 return nl1; 96 97 NodeSet ns1 = new NodeSet(nl1); 98 NodeSet trailNodes = new NodeSet(); 99 Node startNode = nl2.item(0); 100 if (!ns1.contains(startNode)) 101 return trailNodes; 103 for (int i = 0; i < nl1.getLength(); i++) 104 { 105 Node testNode = nl1.item(i); 106 if (DOMHelper.isNodeAfter(startNode, testNode) 107 && !DOMHelper.isNodeTheSame(startNode, testNode)) 108 trailNodes.addElement(testNode); 109 } 110 return trailNodes; 111 } 112 113 124 public static NodeList intersection(NodeList nl1, NodeList nl2) 125 { 126 NodeSet ns1 = new NodeSet(nl1); 127 NodeSet ns2 = new NodeSet(nl2); 128 NodeSet inter = new NodeSet(); 129 130 inter.setShouldCacheNodes(true); 131 132 for (int i = 0; i < ns1.getLength(); i++) 133 { 134 Node n = ns1.elementAt(i); 135 136 if (ns2.contains(n)) 137 inter.addElement(n); 138 } 139 140 return inter; 141 } 142 143 154 public static NodeList difference(NodeList nl1, NodeList nl2) 155 { 156 NodeSet ns1 = new NodeSet(nl1); 157 NodeSet ns2 = new NodeSet(nl2); 158 159 NodeSet diff = new NodeSet(); 160 161 diff.setShouldCacheNodes(true); 162 163 for (int i = 0; i < ns1.getLength(); i++) 164 { 165 Node n = ns1.elementAt(i); 166 167 if (!ns2.contains(n)) 168 diff.addElement(n); 169 } 170 171 return diff; 172 } 173 174 186 public static NodeList distinct(NodeList nl) 187 { 188 NodeSet dist = new NodeSet(); 189 dist.setShouldCacheNodes(true); 190 191 Hashtable stringTable = new Hashtable (); 192 193 for (int i = 0; i < nl.getLength(); i++) 194 { 195 Node currNode = nl.item(i); 196 String key = toString(currNode); 197 198 if (key == null) 199 dist.addElement(currNode); 200 else if (!stringTable.containsKey(key)) 201 { 202 stringTable.put(key, currNode); 203 dist.addElement(currNode); 204 } 205 } 206 207 return dist; 208 } 209 210 223 public static boolean hasSameNode(NodeList nl1, NodeList nl2) 224 { 225 226 NodeSet ns1 = new NodeSet(nl1); 227 NodeSet ns2 = new NodeSet(nl2); 228 229 for (int i = 0; i < ns1.getLength(); i++) 230 { 231 if (ns2.contains(ns1.elementAt(i))) 232 return true; 233 } 234 return false; 235 } 236 237 } 238 | Popular Tags |