1 7 8 package javax.swing.text; 9 10 import java.util.Stack ; 11 import java.util.Enumeration ; 12 13 53 54 public class ElementIterator implements Cloneable { 55 56 57 private Element root; 58 private Stack elementStack = null; 59 60 71 private class StackItem implements Cloneable { 72 Element item; 73 int childIndex; 74 75 private StackItem(Element elem) { 76 81 this.item = elem; 82 this.childIndex = -1; 83 } 84 85 private void incrementIndex() { 86 childIndex++; 87 } 88 89 private Element getElement() { 90 return item; 91 } 92 93 private int getIndex() { 94 return childIndex; 95 } 96 97 protected Object clone() throws java.lang.CloneNotSupportedException { 98 return super.clone(); 99 } 100 } 101 102 109 public ElementIterator(Document document) { 110 root = document.getDefaultRootElement(); 111 } 112 113 114 119 public ElementIterator(Element root) { 120 this.root = root; 121 } 122 123 124 129 public synchronized Object clone() { 130 131 try { 132 ElementIterator it = new ElementIterator (root); 133 if (elementStack != null) { 134 it.elementStack = new Stack (); 135 for (int i = 0; i < elementStack.size(); i++) { 136 StackItem item = (StackItem)elementStack.elementAt(i); 137 StackItem clonee = (StackItem)item.clone(); 138 it.elementStack.push(clonee); 139 } 140 } 141 return it; 142 } catch (CloneNotSupportedException e) { 143 throw new InternalError (); 144 } 145 } 146 147 148 153 public Element first() { 154 if (root == null) { 156 return null; 157 } 158 159 elementStack = new Stack (); 160 if (root.getElementCount() != 0) { 161 elementStack.push(new StackItem(root)); 162 } 163 return root; 164 } 165 166 171 public int depth() { 172 if (elementStack == null) { 173 return 0; 174 } 175 return elementStack.size(); 176 } 177 178 179 185 public Element current() { 186 187 if (elementStack == null) { 188 return first(); 189 } 190 191 194 if (! elementStack.empty()) { 195 StackItem item = (StackItem)elementStack.peek(); 196 Element elem = item.getElement(); 197 int index = item.getIndex(); 198 if (index == -1) { 200 return elem; 201 } 202 return elem.getElement(index); 204 } 205 return null; 206 } 207 208 209 217 public Element next() { 218 219 222 if (elementStack == null) { 223 return first(); 224 } 225 226 if (elementStack.isEmpty()) { 228 return null; 229 } 230 231 233 StackItem item = (StackItem)elementStack.peek(); 234 Element elem = item.getElement(); 235 int index = item.getIndex(); 236 237 if (index+1 < elem.getElementCount()) { 238 Element child = elem.getElement(index+1); 239 if (child.isLeaf()) { 240 243 item.incrementIndex(); 244 } else { 245 248 elementStack.push(new StackItem(child)); 249 } 250 return child; 251 } else { 252 254 elementStack.pop(); 255 if (!elementStack.isEmpty()) { 256 258 StackItem top = (StackItem)elementStack.peek(); 259 top.incrementIndex(); 260 262 return next(); 263 } 264 } 265 return null; 266 } 267 268 269 277 public Element previous() { 278 279 int stackSize; 280 if (elementStack == null || (stackSize = elementStack.size()) == 0) { 281 return null; 282 } 283 284 StackItem item = (StackItem)elementStack.peek(); 287 Element elem = item.getElement(); 288 int index = item.getIndex(); 289 290 if (index > 0) { 291 292 return getDeepestLeaf(elem.getElement(--index)); 293 } else if (index == 0) { 294 297 return elem; 298 } else if (index == -1) { 299 if (stackSize == 1) { 300 return null; 302 } 303 306 Object top = elementStack.pop(); 307 item = (StackItem)elementStack.peek(); 308 309 elementStack.push(top); 311 elem = item.getElement(); 312 index = item.getIndex(); 313 return ((index == -1) ? elem : getDeepestLeaf(elem.getElement 314 (index))); 315 } 316 return null; 318 } 319 320 324 private Element getDeepestLeaf(Element parent) { 325 if (parent.isLeaf()) { 326 return parent; 327 } 328 int childCount = parent.getElementCount(); 329 if (childCount == 0) { 330 return parent; 331 } 332 return getDeepestLeaf(parent.getElement(childCount - 1)); 333 } 334 335 339 private void dumpTree() { 340 341 Element elem; 342 while (true) { 343 if ((elem = next()) != null) { 344 System.out.println("elem: " + elem.getName()); 345 AttributeSet attr = elem.getAttributes(); 346 String s = ""; 347 Enumeration names = attr.getAttributeNames(); 348 while (names.hasMoreElements()) { 349 Object key = names.nextElement(); 350 Object value = attr.getAttribute(key); 351 if (value instanceof AttributeSet ) { 352 s = s + key + "=**AttributeSet** "; 354 } else { 355 s = s + key + "=" + value + " "; 356 } 357 } 358 System.out.println("attributes: " + s); 359 } else { 360 break; 361 } 362 } 363 } 364 } 365 | Popular Tags |