1 package org.jaxen.util; 2 3 63 64 import java.util.Iterator ; 65 import java.util.NoSuchElementException ; 66 67 import org.jaxen.Navigator; 68 import org.jaxen.UnsupportedAxisException; 69 import org.jaxen.JaxenRuntimeException; 70 import org.jaxen.JaxenConstants; 71 72 public class FollowingAxisIterator implements Iterator 73 { 74 private Object contextNode; 75 76 private Navigator navigator; 77 78 private Iterator siblings; 79 80 private Iterator currentSibling; 81 82 public FollowingAxisIterator(Object contextNode, 83 Navigator navigator) throws UnsupportedAxisException 84 { 85 this.contextNode = contextNode; 86 this.navigator = navigator; 87 this.siblings = navigator.getFollowingSiblingAxisIterator(contextNode); 88 this.currentSibling = JaxenConstants.EMPTY_ITERATOR; 89 } 90 91 private boolean goForward() 92 { 93 while ( ! siblings.hasNext() ) 94 { 95 if ( !goUp() ) 96 { 97 return false; 98 } 99 } 100 101 Object nextSibling = siblings.next(); 102 103 this.currentSibling = new DescendantOrSelfAxisIterator(nextSibling, navigator); 104 105 return true; 106 } 107 108 private boolean goUp() 109 { 110 if ( contextNode == null 111 || 112 navigator.isDocument(contextNode) ) 113 { 114 return false; 115 } 116 117 try 118 { 119 contextNode = navigator.getParentNode( contextNode ); 120 121 if ( contextNode != null 122 && 123 !navigator.isDocument(contextNode) ) 124 { 125 siblings = navigator.getFollowingSiblingAxisIterator(contextNode); 126 return true; 127 } 128 else 129 { 130 return false; 131 } 132 } 133 catch (UnsupportedAxisException e) 134 { 135 throw new JaxenRuntimeException(e); 136 } 137 } 138 139 public boolean hasNext() 140 { 141 while ( ! currentSibling.hasNext() ) 142 { 143 if ( ! goForward() ) 144 { 145 return false; 146 } 147 } 148 149 return true; 150 } 151 152 public Object next() throws NoSuchElementException 153 { 154 if ( ! hasNext() ) 155 { 156 throw new NoSuchElementException (); 157 } 158 159 return currentSibling.next(); 160 } 161 162 public void remove() throws UnsupportedOperationException 163 { 164 throw new UnsupportedOperationException (); 165 } 166 } 167 | Popular Tags |