1 16 17 package org.apache.xerces.dom; 18 19 import org.w3c.dom.Node ; 20 import org.w3c.dom.NodeList ; 21 22 import java.util.Vector ; 23 24 73 public class DeepNodeListImpl 74 implements NodeList { 75 76 80 protected NodeImpl rootNode; protected String tagName; protected int changes=0; 83 protected Vector nodes; 84 85 protected String nsName; 86 protected boolean enableNS = false; 87 88 92 93 public DeepNodeListImpl(NodeImpl rootNode, String tagName) { 94 this.rootNode = rootNode; 95 this.tagName = tagName; 96 nodes = new Vector (); 97 } 98 99 100 public DeepNodeListImpl(NodeImpl rootNode, 101 String nsName, String tagName) { 102 this(rootNode, tagName); 103 this.nsName = (nsName != null && !nsName.equals("")) ? nsName : null; 104 enableNS = true; 105 } 106 107 111 112 public int getLength() { 113 item(java.lang.Integer.MAX_VALUE); 115 return nodes.size(); 116 } 117 118 119 public Node item(int index) { 120 Node thisNode; 121 122 if(rootNode.changes() != changes) { 124 nodes = new Vector (); 125 changes = rootNode.changes(); 126 } 127 128 if (index < nodes.size()) 130 return (Node )nodes.elementAt(index); 131 132 else { 134 135 if (nodes.size() == 0) 137 thisNode = rootNode; 138 else 139 thisNode=(NodeImpl)(nodes.lastElement()); 140 141 while(thisNode != null && index >= nodes.size()) { 143 thisNode=nextMatchingElementAfter(thisNode); 144 if (thisNode != null) 145 nodes.addElement(thisNode); 146 } 147 148 return thisNode; 150 } 151 152 } 154 158 163 protected Node nextMatchingElementAfter(Node current) { 164 165 Node next; 166 while (current != null) { 167 if (current.hasChildNodes()) { 169 current = (current.getFirstChild()); 170 } 171 172 else if (current != rootNode && null != (next = current.getNextSibling())) { 174 current = next; 175 } 176 177 else { 179 next = null; 180 for (; current != rootNode; current = current.getParentNode()) { 182 183 next = current.getNextSibling(); 184 if (next != null) 185 break; 186 } 187 current = next; 188 } 189 190 if (current != rootNode 193 && current != null 194 && current.getNodeType() == Node.ELEMENT_NODE) { 195 if (!enableNS) { 196 if (tagName.equals("*") || 197 ((ElementImpl) current).getTagName().equals(tagName)) 198 { 199 return current; 200 } 201 } else { 202 if (tagName.equals("*")) { 204 if (nsName != null && nsName.equals("*")) { 205 return current; 206 } else { 207 ElementImpl el = (ElementImpl) current; 208 if ((nsName == null 209 && el.getNamespaceURI() == null) 210 || (nsName != null 211 && nsName.equals(el.getNamespaceURI()))) 212 { 213 return current; 214 } 215 } 216 } else { 217 ElementImpl el = (ElementImpl) current; 218 if (el.getLocalName() != null 219 && el.getLocalName().equals(tagName)) { 220 if (nsName != null && nsName.equals("*")) { 221 return current; 222 } else { 223 if ((nsName == null 224 && el.getNamespaceURI() == null) 225 || (nsName != null && 226 nsName.equals(el.getNamespaceURI()))) 227 { 228 return current; 229 } 230 } 231 } 232 } 233 } 234 } 235 236 } 238 239 return null; 241 242 } 244 } | Popular Tags |