1 38 package com.gargoylesoftware.htmlunit.html; 39 40 import java.util.Iterator ; 41 import java.util.NoSuchElementException ; 42 43 48 public final class Util { 49 50 51 private Util() { 52 } 54 55 61 public static Iterator getFollowingSiblingAxisIterator(final DomNode contextNode) { 62 return new NodeIterator (contextNode) { 63 protected DomNode getFirstNode (final DomNode node) { 64 return getNextNode(node); 65 } 66 protected DomNode getNextNode (final DomNode node) { 67 return node.getNextSibling(); 68 } 69 }; 70 } 71 72 78 public static Iterator getPrecedingSiblingAxisIterator (final DomNode contextNode) { 79 return new NodeIterator (contextNode) { 80 protected DomNode getFirstNode (final DomNode node) { 81 return getNextNode(node); 82 } 83 protected DomNode getNextNode (final DomNode node) { 84 return node.getPreviousSibling(); 85 } 86 }; 87 } 88 89 95 public static Iterator getFollowingAxisIterator (final DomNode contextNode) { 96 return new NodeIterator (contextNode) { 97 protected DomNode getFirstNode (final DomNode node) { 98 return getNextNode(node); 99 } 100 protected DomNode getNextNode (final DomNode node) { 101 if (node == null) { 102 return null; 103 } 104 else { 105 DomNode n = node.getFirstChild(); 106 if (n == null) { 107 n = node.getNextSibling(); 108 } 109 if (n == null) { 110 return getParentNext(node.getParentNode()); 111 } 112 else { 113 return n; 114 } 115 } 116 } 117 118 protected DomNode getParentNext(final DomNode node) { 119 if (node == null) { 120 return null; 121 } 122 123 DomNode n = node.getNextSibling(); 124 if (n == null) { 125 n = getParentNext(node.getParentNode()); 126 } 127 128 return n; 129 } 130 }; 131 } 132 133 139 public static Iterator getPrecedingAxisIterator (final DomNode contextNode) { 140 return new NodeIterator(contextNode) { 141 protected DomNode getFirstNode (final DomNode node) { 142 if (node == null) { 143 return null; 144 } 145 else { 146 final DomNode sibling = node.getPreviousSibling(); 147 if (sibling == null) { 148 return getFirstNode(node.getParentNode()); 149 } 150 else { 151 return sibling; 152 } 153 } 154 } 155 protected DomNode getNextNode (final DomNode node) { 156 if (node == null) { 157 return null; 158 } 159 else { 160 DomNode n = node.getLastChild(); 161 if (n == null) { 162 n = node.getPreviousSibling(); 163 } 164 if (n == null) { 165 return getFirstNode(node.getParentNode()); 166 } 167 else { 168 return n; 169 } 170 } 171 } 172 }; 173 } 174 } 175 176 183 abstract class NodeIterator implements Iterator { 184 185 private DomNode node_; 186 187 190 public NodeIterator (final DomNode contextNode) { 191 node_ = getFirstNode(contextNode); 192 } 193 194 public boolean hasNext () { 195 return (node_ != null); 196 } 197 198 public Object next () { 199 if (node_ == null) { 200 throw new NoSuchElementException (); 201 } 202 final DomNode ret = node_; 203 node_ = getNextNode(node_); 204 return ret; 205 } 206 207 public void remove () { 208 throw new UnsupportedOperationException (); 209 } 210 220 protected abstract DomNode getFirstNode (final DomNode contextNode); 221 232 protected abstract DomNode getNextNode (final DomNode contextNode); 233 } 234 235 236 | Popular Tags |