1 57 58 package org.xquark.xpath.datamodel.xerces.dom; 59 60 import java.util.Vector ; 61 62 import org.w3c.dom.Node ; 63 import org.w3c.dom.NodeList ; 64 65 112 public class DeepNodeListImpl 113 implements NodeList { 114 115 119 protected NodeImpl rootNode; protected String tagName; protected int changes=0; 122 protected Vector nodes; 123 124 protected String nsName; 125 protected boolean enableNS = false; 126 127 131 132 public DeepNodeListImpl(NodeImpl rootNode, String tagName) { 133 this.rootNode = rootNode; 134 this.tagName = tagName; 135 nodes = new Vector (); 136 } 137 138 139 public DeepNodeListImpl(NodeImpl rootNode, 140 String nsName, String tagName) { 141 this(rootNode, tagName); 142 this.nsName = (nsName != null && !nsName.equals("")) ? nsName : null; 143 enableNS = true; 144 } 145 146 150 151 public int getLength() { 152 item(java.lang.Integer.MAX_VALUE); 154 return nodes.size(); 155 } 156 157 158 public Node item(int index) { 159 Node thisNode; 160 161 if(rootNode.changes() != changes) { 163 nodes = new Vector (); 164 changes = rootNode.changes(); 165 } 166 167 if (index < nodes.size()) 169 return (Node )nodes.elementAt(index); 170 171 else { 173 174 if (nodes.size() == 0) 176 thisNode = rootNode; 177 else 178 thisNode=(NodeImpl)(nodes.lastElement()); 179 180 while(thisNode != null && index >= nodes.size()) { 182 thisNode=nextMatchingElementAfter(thisNode); 183 if (thisNode != null) 184 nodes.addElement(thisNode); 185 } 186 187 return thisNode; 189 } 190 191 } 193 197 202 protected Node nextMatchingElementAfter(Node current) { 203 204 Node next; 205 while (current != null) { 206 if (current.hasChildNodes()) { 208 current = (current.getFirstChild()); 209 } 210 211 else if (current != rootNode && null != (next = current.getNextSibling())) { 213 current = next; 214 } 215 216 else { 218 next = null; 219 for (; current != rootNode; current = current.getParentNode()) { 221 222 next = current.getNextSibling(); 223 if (next != null) 224 break; 225 } 226 current = next; 227 } 228 229 if (current != rootNode 232 && current != null 233 && current.getNodeType() == Node.ELEMENT_NODE) { 234 if (!enableNS) { 235 if (tagName.equals("*") || 236 ((ElementImpl) current).getTagName().equals(tagName)) 237 { 238 return current; 239 } 240 } else { 241 if (tagName.equals("*")) { 243 if (nsName != null && nsName.equals("*")) { 244 return current; 245 } else { 246 ElementImpl el = (ElementImpl) current; 247 if ((nsName == null 248 && el.getNamespaceURI() == null) 249 || (nsName != null 250 && nsName.equals(el.getNamespaceURI()))) 251 { 252 return current; 253 } 254 } 255 } else { 256 ElementImpl el = (ElementImpl) current; 257 if (el.getLocalName() != null 258 && el.getLocalName().equals(tagName)) { 259 if (nsName != null && nsName.equals("*")) { 260 return current; 261 } else { 262 if ((nsName == null 263 && el.getNamespaceURI() == null) 264 || (nsName != null && 265 nsName.equals(el.getNamespaceURI()))) 266 { 267 return current; 268 } 269 } 270 } 271 } 272 } 273 } 274 275 } 277 278 return null; 280 281 } 283 } | Popular Tags |