1 package net.sf.saxon.expr; 2 import net.sf.saxon.om.ArrayIterator; 3 import net.sf.saxon.om.Item; 4 import net.sf.saxon.om.LookaheadIterator; 5 import net.sf.saxon.om.SequenceIterator; 6 import net.sf.saxon.trans.XPathException; 7 8 9 12 13 public class PositionIterator implements SequenceIterator, LookaheadIterator { 14 15 24 25 public static SequenceIterator make(SequenceIterator base, int min, int max) throws XPathException { 26 if (base instanceof ArrayIterator) { 28 return ((ArrayIterator)base).makeSliceIterator(min, max); 29 } else { 30 return new PositionIterator(base, min, max); 31 } 32 } 33 34 35 36 private SequenceIterator base; 37 private int position = 0; 38 private int min = 1; 39 private int max = Integer.MAX_VALUE; 40 private Item nextItem = null; 41 private Item current = null; 42 43 49 50 private PositionIterator(SequenceIterator base, int min, int max) throws XPathException { 51 this.base = base; 52 this.min = min; 53 if (min<1) min=1; 54 this.max = max; 55 if (max<min) { 56 nextItem = null; 57 return; 58 } 59 int i=1; 60 while ( i++ <= min ) { 61 nextItem = base.next(); 62 if (nextItem == null) { 63 break; 64 } 65 } 66 current = nextItem; 67 } 68 69 72 73 public boolean hasNext() { 74 return nextItem != null; 75 } 76 77 80 81 public Item next() throws XPathException { 82 if (nextItem == null) { 83 current = null; 84 position = -1; 85 return null; 86 } 87 current = nextItem; 88 position++; 89 if (base.position() < max) { 90 nextItem = base.next(); 91 } else { 92 nextItem = null; 93 } 94 return current; 95 } 96 97 98 public Item current() { 99 return current; 100 } 101 102 public int position() { 103 return position; 104 } 105 106 109 110 public SequenceIterator getAnother() throws XPathException { 111 return new PositionIterator(base.getAnother(), min, max); 112 } 113 114 123 124 public int getProperties() { 125 return LOOKAHEAD; 126 } 127 128 } 129 130 131 132 | Popular Tags |