1 61 62 63 64 package org.jaxen.util; 65 66 import java.util.HashSet ; 67 import java.util.Iterator ; 68 import java.util.LinkedList ; 69 import java.util.NoSuchElementException ; 70 import java.util.Set ; 71 72 import org.jaxen.Navigator; 73 74 79 public abstract class StackedIterator implements Iterator 80 { 81 82 private LinkedList iteratorStack; 83 private Navigator navigator; 84 85 private Set created; 86 87 public StackedIterator(Object contextNode, 88 Navigator navigator) 89 { 90 this.iteratorStack = new LinkedList (); 91 this.created = new HashSet (); 92 93 init( contextNode, 94 navigator ); 95 } 96 97 protected StackedIterator() 98 { 99 this.iteratorStack = new LinkedList (); 100 this.created = new HashSet (); 101 } 102 103 protected void init(Object contextNode, 104 Navigator navigator) 105 { 106 this.navigator = navigator; 107 108 } 110 111 protected Iterator internalCreateIterator(Object contextNode) 112 { 113 if ( this.created.contains( contextNode ) ) 114 { 115 return null; 116 } 117 118 this.created.add( contextNode ); 119 120 return createIterator( contextNode ); 121 } 122 123 public boolean hasNext() 124 { 125 Iterator curIter = currentIterator(); 126 127 if ( curIter == null ) 128 { 129 return false; 130 } 131 132 return curIter.hasNext(); 133 } 134 135 public Object next() throws NoSuchElementException 136 { 137 if ( ! hasNext() ) 138 { 139 throw new NoSuchElementException (); 140 } 141 142 Iterator curIter = currentIterator(); 143 Object object = curIter.next(); 144 145 pushIterator( internalCreateIterator( object ) ); 146 147 return object; 148 } 149 150 public void remove() throws UnsupportedOperationException 151 { 152 throw new UnsupportedOperationException (); 153 } 154 155 abstract protected Iterator createIterator(Object contextNode); 156 157 protected void pushIterator(Iterator iter) 158 { 159 if ( iter != null ) 160 { 161 this.iteratorStack.addFirst(iter); } 163 } 164 165 private Iterator currentIterator() 166 { 167 while ( iteratorStack.size() > 0 ) 168 { 169 Iterator curIter = (Iterator ) iteratorStack.getFirst(); 170 171 if ( curIter.hasNext() ) 172 { 173 return curIter; 174 } 175 176 iteratorStack.removeFirst(); 177 } 178 179 return null; 180 } 181 182 protected Navigator getNavigator() 183 { 184 return this.navigator; 185 } 186 } 187 | Popular Tags |