1 18 package org.apache.batik.dom.traversal; 19 20 import org.apache.batik.dom.AbstractDocument; 21 import org.w3c.dom.DOMException ; 22 import org.w3c.dom.Node ; 23 import org.w3c.dom.traversal.NodeFilter; 24 import org.w3c.dom.traversal.NodeIterator; 25 26 33 public class DOMNodeIterator implements NodeIterator { 34 35 38 protected final static short INITIAL = 0; 39 40 43 protected final static short INVALID = 1; 44 45 48 protected final static short FORWARD = 2; 49 50 53 protected final static short BACKWARD = 3; 54 55 58 protected AbstractDocument document; 59 60 63 protected Node root; 64 65 68 protected int whatToShow; 69 70 73 protected NodeFilter filter; 74 75 79 protected boolean expandEntityReferences; 80 81 84 protected short state; 85 86 89 protected Node referenceNode; 90 91 100 public DOMNodeIterator(AbstractDocument doc, Node n, int what, 101 NodeFilter nf, boolean exp) { 102 document = doc; 103 root = n; 104 whatToShow = what; 105 filter = nf; 106 expandEntityReferences = exp; 107 108 referenceNode = root; 109 } 110 111 114 public Node getRoot() { 115 return root; 116 } 117 118 121 public int getWhatToShow() { 122 return whatToShow; 123 } 124 125 128 public NodeFilter getFilter() { 129 return filter; 130 } 131 132 135 public boolean getExpandEntityReferences() { 136 return expandEntityReferences; 137 } 138 139 142 public Node nextNode() { 143 switch (state) { 144 case INVALID: 145 throw document.createDOMException 146 (DOMException.INVALID_STATE_ERR, 147 "detached.iterator", null); 148 case BACKWARD: 149 case INITIAL: 150 state = FORWARD; 151 return referenceNode; 152 case FORWARD: 153 } 154 155 for (;;) { 156 unfilteredNextNode(); 157 if (referenceNode == null) { 158 return null; 159 } 160 if ((whatToShow & (1 << referenceNode.getNodeType() - 1)) != 0) { 161 if (filter == null || 162 filter.acceptNode(referenceNode) == NodeFilter.FILTER_ACCEPT) { 163 return referenceNode; 164 } 165 } 166 } 167 } 168 169 172 public Node previousNode() { 173 switch (state) { 174 case INVALID: 175 throw document.createDOMException 176 (DOMException.INVALID_STATE_ERR, 177 "detached.iterator", null); 178 case FORWARD: 179 case INITIAL: 180 state = BACKWARD; 181 return referenceNode; 182 case BACKWARD: 183 } 184 185 for (;;) { 186 unfilteredPreviousNode(); 187 if (referenceNode == null) { 188 return referenceNode; 189 } 190 if ((whatToShow & (1 << referenceNode.getNodeType() - 1)) != 0) { 191 if (filter == null || 192 filter.acceptNode(referenceNode) == NodeFilter.FILTER_ACCEPT) { 193 return referenceNode; 194 } 195 } 196 } 197 } 198 199 202 public void detach() { 203 state = INVALID; 204 document.detachNodeIterator(this); 205 } 206 207 210 public void nodeToBeRemoved(Node removedNode) { 211 if (state == INVALID) { 212 return; 213 } 214 215 Node node; 216 for (node = referenceNode; 217 node != null && node != root; 218 node = node.getParentNode()) { 219 if (node == removedNode) { 220 break; 221 } 222 } 223 if (node == null || node == root) { 224 return; 225 } 226 227 if (state == BACKWARD) { 228 if (node.getNodeType() != Node.ENTITY_REFERENCE_NODE || 230 expandEntityReferences) { 231 Node n = node.getFirstChild(); 232 if (n != null) { 233 referenceNode = n; 234 return; 235 } 236 } 237 238 Node n = node.getNextSibling(); 240 if (n != null) { 241 referenceNode = n; 242 return; 243 } 244 245 n = node; 247 while ((n = n.getParentNode()) != null && n != root) { 248 Node t = n.getNextSibling(); 249 if (t != null) { 250 referenceNode = t; 251 return; 252 } 253 } 254 255 referenceNode = null; 256 } else { 257 Node n = node.getPreviousSibling(); 258 259 if (n == null) { 261 referenceNode = node.getParentNode(); 262 return; 263 } 264 265 if (n.getNodeType() != Node.ENTITY_REFERENCE_NODE || 267 expandEntityReferences) { 268 Node t; 269 while ((t = n.getLastChild()) != null) { 270 n = t; 271 } 272 } 273 274 referenceNode = n; 275 } 276 } 277 278 281 protected void unfilteredNextNode() { 282 if (referenceNode == null) { 283 return; 284 } 285 286 if (referenceNode.getNodeType() != Node.ENTITY_REFERENCE_NODE || 288 expandEntityReferences) { 289 Node n = referenceNode.getFirstChild(); 290 if (n != null) { 291 referenceNode = n; 292 return; 293 } 294 } 295 296 Node n = referenceNode.getNextSibling(); 298 if (n != null) { 299 referenceNode = n; 300 return; 301 } 302 303 n = referenceNode; 305 while ((n = n.getParentNode()) != null && n != root) { 306 Node t = n.getNextSibling(); 307 if (t != null) { 308 referenceNode = t; 309 return; 310 } 311 } 312 referenceNode = null; 313 } 314 315 318 protected void unfilteredPreviousNode() { 319 if (referenceNode == null) { 320 return; 321 } 322 323 if (referenceNode == root) { 325 referenceNode = null; 326 return; 327 } 328 329 Node n = referenceNode.getPreviousSibling(); 330 331 if (n == null) { 333 referenceNode = referenceNode.getParentNode(); 334 return; 335 } 336 337 if (n.getNodeType() != Node.ENTITY_REFERENCE_NODE || 339 expandEntityReferences) { 340 Node t; 341 while ((t = n.getLastChild()) != null) { 342 n = t; 343 } 344 } 345 346 referenceNode = n; 347 } 348 } 349 | Popular Tags |