Desktop tool for browsing account info from EVE-Online
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DescendantIterator.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*--
  2. $Id: DescendantIterator.java,v 1.6 2007/11/10 05:28:58 jhunter Exp $
  3. Copyright (C) 2000-2007 Jason Hunter & Brett McLaughlin.
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions
  7. are met:
  8. 1. Redistributions of source code must retain the above copyright
  9. notice, this list of conditions, and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions, and the disclaimer that follows
  12. these conditions in the documentation and/or other materials
  13. provided with the distribution.
  14. 3. The name "JDOM" must not be used to endorse or promote products
  15. derived from this software without prior written permission. For
  16. written permission, please contact <request_AT_jdom_DOT_org>.
  17. 4. Products derived from this software may not be called "JDOM", nor
  18. may "JDOM" appear in their name, without prior written permission
  19. from the JDOM Project Management <request_AT_jdom_DOT_org>.
  20. In addition, we request (but do not require) that you include in the
  21. end-user documentation provided with the redistribution and/or in the
  22. software itself an acknowledgement equivalent to the following:
  23. "This product includes software developed by the
  24. JDOM Project (http://www.jdom.org/)."
  25. Alternatively, the acknowledgment may be graphical using the logos
  26. available at http://www.jdom.org/images/logos.
  27. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  28. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  29. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  30. DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
  31. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  33. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  34. USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  35. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  36. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  38. SUCH DAMAGE.
  39. This software consists of voluntary contributions made by many
  40. individuals on behalf of the JDOM Project and was originally
  41. created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
  42. Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
  43. on the JDOM Project, please see <http://www.jdom.org/>.
  44. */
  45. package org.jdom;
  46. import java.util.*;
  47. import org.jdom.Content;
  48. import org.jdom.Element;
  49. import org.jdom.Parent;
  50. /**
  51. * Traverse all a parent's descendants (all children at any level below
  52. * the parent).
  53. *
  54. * @author Bradley S. Huffman
  55. * @author Jason Hunter
  56. * @version $Revision: 1.6 $, $Date: 2007/11/10 05:28:58 $
  57. */
  58. class DescendantIterator implements Iterator {
  59. private Iterator iterator;
  60. private Iterator nextIterator;
  61. private List stack = new ArrayList();
  62. private static final String CVS_ID =
  63. "@(#) $RCSfile: DescendantIterator.java,v $ $Revision: 1.6 $ $Date: 2007/11/10 05:28:58 $ $Name: jdom_1_1 $";
  64. /**
  65. * Iterator for the descendants of the supplied object.
  66. *
  67. * @param parent document or element whose descendants will be iterated
  68. */
  69. DescendantIterator(Parent parent) {
  70. if (parent == null) {
  71. throw new IllegalArgumentException("parent parameter was null");
  72. }
  73. this.iterator = parent.getContent().iterator();
  74. }
  75. /**
  76. * Returns true> if the iteration has more {@link Content} descendants.
  77. *
  78. * @return true is the iterator has more descendants
  79. */
  80. public boolean hasNext() {
  81. if (iterator != null && iterator.hasNext()) return true;
  82. if (nextIterator != null && nextIterator.hasNext()) return true;
  83. if (stackHasAnyNext()) return true;
  84. return false;
  85. }
  86. /**
  87. * Returns the next {@link Content} descendant.
  88. *
  89. * @return the next descendant
  90. */
  91. public Object next() {
  92. if (!hasNext()) {
  93. throw new NoSuchElementException();
  94. }
  95. // If we need to descend, go for it and record where we are.
  96. // We do the shuffle here on the next next() call so remove() is easy
  97. // to code up.
  98. if (nextIterator != null) {
  99. push(iterator);
  100. iterator = nextIterator;
  101. nextIterator = null;
  102. }
  103. // If this iterator is finished, try moving up the stack
  104. while (!iterator.hasNext()) {
  105. if (stack.size() > 0) {
  106. iterator = pop();
  107. }
  108. else {
  109. throw new NoSuchElementException("Somehow we lost our iterator");
  110. }
  111. }
  112. Content child = (Content) iterator.next();
  113. if (child instanceof Element) {
  114. nextIterator = ((Element)child).getContent().iterator();
  115. }
  116. return child;
  117. }
  118. /**
  119. * Detaches the last {@link org.jdom.Content} returned by the last call to
  120. * next from it's parent. <b>Note</b>: this <b>does not</b> affect
  121. * iteration and all children, siblings, and any node following the
  122. * removed node (in document order) will be visited.
  123. */
  124. public void remove() {
  125. iterator.remove();
  126. }
  127. private Iterator pop() {
  128. int stackSize = stack.size();
  129. if (stackSize == 0) {
  130. throw new NoSuchElementException("empty stack");
  131. }
  132. return (Iterator) stack.remove(stackSize - 1);
  133. }
  134. private void push(Iterator itr) {
  135. stack.add(itr);
  136. }
  137. private boolean stackHasAnyNext() {
  138. int size = stack.size();
  139. for (int i = 0; i < size; i++) {
  140. Iterator itr = (Iterator) stack.get(i);
  141. if (itr.hasNext()) {
  142. return true;
  143. }
  144. }
  145. return false;
  146. }
  147. }