1 16 17 package org.apache.xerces.dom; 18 19 import org.w3c.dom.DOMException ; 20 import org.w3c.dom.Node ; 21 import org.w3c.dom.traversal.NodeFilter; 22 import org.w3c.dom.traversal.NodeIterator; 23 24 25 39 public class NodeIteratorImpl implements NodeIterator { 40 41 45 46 private DocumentImpl fDocument; 47 48 private Node fRoot; 49 50 private int fWhatToShow = NodeFilter.SHOW_ALL; 51 52 private NodeFilter fNodeFilter; 53 54 private boolean fDetach = false; 55 56 69 70 private Node fCurrentNode; 71 72 78 private boolean fForward = true; 79 80 81 private boolean fEntityReferenceExpansion; 82 83 87 88 public NodeIteratorImpl( DocumentImpl document, 89 Node root, 90 int whatToShow, 91 NodeFilter nodeFilter, 92 boolean entityReferenceExpansion) { 93 fDocument = document; 94 fRoot = root; 95 fCurrentNode = null; 96 fWhatToShow = whatToShow; 97 fNodeFilter = nodeFilter; 98 fEntityReferenceExpansion = entityReferenceExpansion; 99 } 100 101 public Node getRoot() { 102 return fRoot; 103 } 104 105 109 110 public int getWhatToShow() { 111 return fWhatToShow; 112 } 113 114 115 public NodeFilter getFilter() { 116 return fNodeFilter; 117 } 118 119 120 public boolean getExpandEntityReferences() { 121 return fEntityReferenceExpansion; 122 } 123 124 128 public Node nextNode() { 129 130 if( fDetach) { 131 throw new DOMException ( 132 DOMException.INVALID_STATE_ERR, 133 DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_STATE_ERR", null)); 134 } 135 136 if (fRoot == null) return null; 138 139 Node nextNode = fCurrentNode; 140 boolean accepted = false; 142 accepted_loop: 143 while (!accepted) { 144 145 if (!fForward && nextNode!=null) { 147 nextNode = fCurrentNode; 149 } else { 150 if (!fEntityReferenceExpansion 152 && nextNode != null 153 && nextNode.getNodeType() == Node.ENTITY_REFERENCE_NODE) { 154 nextNode = nextNode(nextNode, false); 155 } else { 156 nextNode = nextNode(nextNode, true); 157 } 158 } 159 160 fForward = true; 162 if (nextNode == null) return null; 164 165 accepted = acceptNode(nextNode); 167 if (accepted) { 168 fCurrentNode = nextNode; 170 return fCurrentNode; 171 } else 172 continue accepted_loop; 173 174 } 176 return null; 178 179 } 180 181 184 public Node previousNode() { 185 186 if( fDetach) { 187 throw new DOMException ( 188 DOMException.INVALID_STATE_ERR, 189 DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_STATE_ERR", null)); 190 } 191 192 if (fRoot == null || fCurrentNode == null) return null; 194 195 Node previousNode = fCurrentNode; 196 boolean accepted = false; 197 198 accepted_loop: 199 while (!accepted) { 200 201 if (fForward && previousNode != null) { 202 previousNode = fCurrentNode; 204 } else { 205 previousNode = previousNode(previousNode); 207 } 208 209 fForward = false; 211 212 if (previousNode == null) return null; 215 216 accepted = acceptNode(previousNode); 218 if (accepted) { 219 fCurrentNode = previousNode; 221 return fCurrentNode; 222 } else 223 continue accepted_loop; 224 } 225 return null; 227 } 228 229 230 boolean acceptNode(Node node) { 231 232 if (fNodeFilter == null) { 233 return ( fWhatToShow & (1 << node.getNodeType()-1)) != 0 ; 234 } else { 235 return ((fWhatToShow & (1 << node.getNodeType()-1)) != 0 ) 236 && fNodeFilter.acceptNode(node) == NodeFilter.FILTER_ACCEPT; 237 } 238 } 239 240 241 Node matchNodeOrParent(Node node) { 242 if (fCurrentNode == null) return null; 245 246 for (Node n = fCurrentNode; n != fRoot; n = n.getParentNode()) { 249 if (node == n) return n; 250 } 251 return null; 252 } 253 254 260 Node nextNode(Node node, boolean visitChildren) { 261 262 if (node == null) return fRoot; 263 264 Node result; 265 if (visitChildren) { 267 if (node.hasChildNodes()) { 269 result = node.getFirstChild(); 270 return result; 271 } 272 } 273 274 if (node == fRoot) { return null; 276 } 277 278 result = node.getNextSibling(); 280 if (result != null) return result; 281 282 283 Node parent = node.getParentNode(); 285 while (parent != null && parent != fRoot) { 286 result = parent.getNextSibling(); 287 if (result != null) { 288 return result; 289 } else { 290 parent = parent.getParentNode(); 291 } 292 293 } 295 return null; 297 } 298 299 302 Node previousNode(Node node) { 303 304 Node result; 305 306 if (node == fRoot) return null; 308 309 result = node.getPreviousSibling(); 311 if (result == null) { 312 result = node.getParentNode(); 314 return result; 315 } 316 317 if (result.hasChildNodes() 319 && !(!fEntityReferenceExpansion 320 && result != null 321 && result.getNodeType() == Node.ENTITY_REFERENCE_NODE)) 322 323 { 324 while (result.hasChildNodes()) { 325 result = result.getLastChild(); 326 } 327 } 328 329 return result; 330 } 331 332 335 public void removeNode(Node node) { 336 337 340 if (node == null) return; 341 342 Node deleted = matchNodeOrParent(node); 343 344 if (deleted == null) return; 345 346 if (fForward) { 347 fCurrentNode = previousNode(deleted); 348 } else 349 { 351 Node next = nextNode(deleted, false); 352 if (next!=null) { 353 fCurrentNode = next; 355 } else { 356 fCurrentNode = previousNode(deleted); 359 fForward = true; 360 } 361 362 } 363 364 } 365 366 public void detach() { 367 fDetach = true; 368 fDocument.removeNodeIterator(this); 369 } 370 371 } 372 | Popular Tags |