1 61 62 63 64 package org.jaxen.util; 65 66 import java.util.Iterator ; 67 import java.util.LinkedList ; 68 import java.util.NoSuchElementException ; 69 70 import org.jaxen.JaxenConstants; 71 import org.jaxen.Navigator; 72 import org.jaxen.UnsupportedAxisException; 73 74 public class PrecedingSiblingAxisIterator implements Iterator 75 { 76 private Object contextNode; 77 private Navigator navigator; 78 79 private Iterator siblingIter; 80 private Object nextObj; 81 82 public PrecedingSiblingAxisIterator(Object contextNode, 83 Navigator navigator) throws UnsupportedAxisException 84 { 85 this.contextNode = contextNode; 86 this.navigator = navigator; 87 88 init(); 89 if ( siblingIter.hasNext() ) 90 { 91 this.nextObj = siblingIter.next(); 92 } 93 } 94 95 private void init() throws UnsupportedAxisException 96 { 97 98 Object parent = this.navigator.getParentNode( this.contextNode ); 99 100 if ( parent != null ) 101 { 102 Iterator childIter = this.navigator.getChildAxisIterator( parent ); 103 LinkedList siblings = new LinkedList (); 104 105 while ( childIter.hasNext() ) 106 { 107 Object eachChild = childIter.next(); 108 if ( eachChild.equals(this.contextNode) ) 109 { 110 break; 111 } 112 siblings.addFirst( eachChild ); 113 } 114 115 this.siblingIter = siblings.iterator(); 116 117 } 118 else { 119 this.siblingIter = JaxenConstants.EMPTY_ITERATOR; 120 } 121 122 } 123 124 public boolean hasNext() 125 { 126 return ( this.nextObj != null ); 127 } 128 129 public Object next() throws NoSuchElementException 130 { 131 if ( ! hasNext() ) 132 { 133 throw new NoSuchElementException (); 134 } 135 136 Object obj = this.nextObj; 137 if ( siblingIter.hasNext() ) 138 { 139 this.nextObj = siblingIter.next(); 140 } 141 else { 142 this.nextObj = null; 143 } 144 return obj; 145 } 146 147 public void remove() throws UnsupportedOperationException 148 { 149 throw new UnsupportedOperationException (); 150 } 151 152 } 153 | Popular Tags |