1. /*
  2. * @(#)ElementIterator.java 1.7 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing.text;
  8. import java.util.Stack;
  9. import java.util.Enumeration;
  10. /**
  11. * <p>
  12. * ElementIterator, as the name suggests, iteratates over the Element
  13. * tree. The constructor can be invoked with either Document or an Element
  14. * as an argument. If the constructor is invoked with a Document as an
  15. * argument then the root of the iteration is the return value of
  16. * document.getDefaultRootElement().
  17. *
  18. * The iteration happens in a depth-first manner. In terms of how
  19. * boundary conditions are handled:
  20. * a) if next() is called before first() or current(), the
  21. * root will be returned.
  22. * b) next() returns null to indicate the end of the list.
  23. * c) previous() returns null when the current element is the root
  24. * or next() has returned null.
  25. *
  26. * The ElementIterator does no locking of the Element tree. This means
  27. * that it does not track any changes. It is the responsibility of the
  28. * user of this class, to ensure that no changes happen during element
  29. * iteration.
  30. *
  31. * Simple usage example:
  32. *
  33. * public void iterate() {
  34. * ElementIterator it = new ElementIterator(root);
  35. * Element elem;
  36. * while (true) {
  37. * if ((elem = next()) != null) {
  38. * // process element
  39. * System.out.println("elem: " + elem.getName());
  40. * } else {
  41. * break;
  42. * }
  43. * }
  44. * }
  45. *
  46. * @author Sunita Mani
  47. * @version 1.7 11/29/01
  48. *
  49. */
  50. public class ElementIterator implements Cloneable {
  51. private Element root;
  52. private Stack elementStack = null;
  53. /**
  54. * The StackItem class stores the element
  55. * as well as a child index. If the
  56. * index is -1, then the element represented
  57. * on the stack is the element itself.
  58. * Otherwise, the index functions as as index
  59. * into the vector of children of the element.
  60. * In this case, the item on the stack
  61. * represents the "index"th child of the element
  62. *
  63. */
  64. private class StackItem implements Cloneable {
  65. Element item;
  66. int childIndex;
  67. private StackItem(Element elem) {
  68. /**
  69. * -1 index implies a self reference,
  70. * as opposed to an index into its
  71. * list of children.
  72. */
  73. this.item = elem;
  74. this.childIndex = -1;
  75. }
  76. private void incrementIndex() {
  77. childIndex++;
  78. }
  79. private Element getElement() {
  80. return item;
  81. }
  82. private int getIndex() {
  83. return childIndex;
  84. }
  85. protected Object clone() throws java.lang.CloneNotSupportedException {
  86. return super.clone();
  87. }
  88. }
  89. /**
  90. * Creates a new ElementIterator. The
  91. * root element is taken to get the
  92. * default root element of the document.
  93. *
  94. * @param a Document.
  95. */
  96. public ElementIterator(Document document) {
  97. root = document.getDefaultRootElement();
  98. }
  99. /**
  100. * Creates a new ElementIterator.
  101. *
  102. * @param the root Element.
  103. */
  104. public ElementIterator(Element root) {
  105. this.root = root;
  106. }
  107. /**
  108. * Clones the ElementIterator.
  109. *
  110. * @return a cloned ElementIterator Object.
  111. */
  112. public synchronized Object clone() {
  113. try {
  114. ElementIterator it = new ElementIterator(root);
  115. it.elementStack = new Stack();
  116. for (int i = 0; i < elementStack.size(); i++) {
  117. StackItem item = (StackItem)elementStack.elementAt(i);
  118. StackItem clonee = (StackItem)item.clone();
  119. it.elementStack.push(clonee);
  120. }
  121. return it;
  122. } catch (CloneNotSupportedException e) {
  123. throw new InternalError();
  124. }
  125. }
  126. /**
  127. * Fetches the first element.
  128. *
  129. * @return an Element.
  130. */
  131. public Element first() {
  132. // just in case...
  133. if (root == null) {
  134. return null;
  135. }
  136. elementStack = new Stack();
  137. if (root.getElementCount() != 0) {
  138. elementStack.push(new StackItem(root));
  139. }
  140. return root;
  141. }
  142. /**
  143. * Fetches the current depth of element tree.
  144. *
  145. * @return the depth.
  146. */
  147. public int depth() {
  148. return elementStack.size();
  149. }
  150. /**
  151. * Fetches the current Element.
  152. *
  153. * @returns element on top of the stack or
  154. * null if the root element is null.
  155. */
  156. public Element current() {
  157. if (elementStack == null) {
  158. return first();
  159. }
  160. /*
  161. get a handle to the element on top of the stack.
  162. */
  163. if (! elementStack.empty()) {
  164. StackItem item = (StackItem)elementStack.peek();
  165. Element elem = item.getElement();
  166. int index = item.getIndex();
  167. // self reference
  168. if (index == -1) {
  169. return elem;
  170. }
  171. // return the child at location "index".
  172. return elem.getElement(index);
  173. }
  174. return null;
  175. }
  176. /**
  177. * Fetches the next Element. The strategy
  178. * used to locate the next element is
  179. * a depth-first search.
  180. *
  181. * @returns the next element or null
  182. * at the end of the list.
  183. */
  184. public Element next() {
  185. /* if current() has not been invoked
  186. and next is invoked, the very first
  187. element will be returned. */
  188. if (elementStack == null) {
  189. return first();
  190. }
  191. // no more elements
  192. if (elementStack.isEmpty()) {
  193. return null;
  194. }
  195. // get a handle to the element on top of the stack
  196. StackItem item = (StackItem)elementStack.peek();
  197. Element elem = item.getElement();
  198. int index = item.getIndex();
  199. if (index+1 < elem.getElementCount()) {
  200. Element child = elem.getElement(index+1);
  201. if (child.isLeaf()) {
  202. /* In this case we merely want to increment
  203. the child index of the item on top of the
  204. stack.*/
  205. item.incrementIndex();
  206. } else {
  207. /* In this case we need to push the child(branch)
  208. on the stack so that we can iterate over its
  209. children. */
  210. elementStack.push(new StackItem(child));
  211. }
  212. return child;
  213. } else {
  214. /* No more children for the item on top of the
  215. stack therefore pop the stack. */
  216. elementStack.pop();
  217. if (!elementStack.isEmpty()) {
  218. /* Increment the child index for the item that
  219. is now on top of the stack. */
  220. StackItem top = (StackItem)elementStack.peek();
  221. top.incrementIndex();
  222. /* We now want to return its next child, therefore
  223. call next() recursively. */
  224. return next();
  225. }
  226. }
  227. return null;
  228. }
  229. /**
  230. * Fetches the previous Element. If howver the current
  231. * element is the last element, or the current element
  232. * is null, then null is returned.
  233. *
  234. * @returns previous Element if available.
  235. *
  236. */
  237. public Element previous() {
  238. if (elementStack == null | elementStack.size() == 1) {
  239. return null;
  240. }
  241. // get a handle to the element on top of the stack
  242. //
  243. StackItem item = (StackItem)elementStack.peek();
  244. Element elem = item.getElement();
  245. int index = item.getIndex();
  246. if (index > 0) {
  247. /* return child at previous index. */
  248. return elem.getElement(--index);
  249. } else if (index == 0) {
  250. /* this implies that current is the element's
  251. first child, therefore previous is the
  252. element itself. */
  253. return elem;
  254. } else if (index == -1) {
  255. /* We need to return either the item
  256. below the top item or one of the
  257. former's children. */
  258. Object top = elementStack.pop();
  259. item = (StackItem)elementStack.peek();
  260. // restore the top item.
  261. elementStack.push(top);
  262. elem = item.getElement();
  263. index = item.getIndex();
  264. return ((index == -1) ? elem : elem.getElement(index));
  265. }
  266. // should never get here.
  267. return null;
  268. }
  269. /*
  270. Iterates through the element tree and prints
  271. out each element and its attributes.
  272. */
  273. private void dumpTree() {
  274. Element elem;
  275. while (true) {
  276. if ((elem = next()) != null) {
  277. System.out.println("elem: " + elem.getName());
  278. AttributeSet attr = elem.getAttributes();
  279. String s = "";
  280. Enumeration names = attr.getAttributeNames();
  281. while (names.hasMoreElements()) {
  282. Object key = names.nextElement();
  283. Object value = attr.getAttribute(key);
  284. if (value instanceof AttributeSet) {
  285. // don't go recursive
  286. s = s + key + "=**AttributeSet** ";
  287. } else {
  288. s = s + key + "=" + value + " ";
  289. }
  290. }
  291. System.out.println("attributes: " + s);
  292. } else {
  293. break;
  294. }
  295. }
  296. }
  297. }