1 package net.sf.saxon.functions; 2 import net.sf.saxon.expr.XPathContext; 3 import net.sf.saxon.om.Item; 4 import net.sf.saxon.om.SequenceIterator; 5 import net.sf.saxon.sort.AtomicComparer; 6 import net.sf.saxon.trans.XPathException; 7 import net.sf.saxon.value.AtomicValue; 8 import net.sf.saxon.value.IntegerValue; 9 10 11 14 15 16 public class IndexOf extends CollatingFunction { 17 18 21 22 public SequenceIterator iterate(XPathContext context) throws XPathException { 23 AtomicComparer comparer = getAtomicComparer(2, context); 24 SequenceIterator seq = argument[0].iterate(context); 25 AtomicValue val = (AtomicValue)argument[1].evaluateItem(context); 26 return new IndexIterator(seq, val, comparer); 27 } 28 29 private class IndexIterator implements SequenceIterator { 30 31 SequenceIterator base; 32 AtomicValue value; 33 AtomicComparer comparer; 34 int index = 0; 35 int position = 0; 36 Item current = null; 37 38 public IndexIterator(SequenceIterator base, AtomicValue value, AtomicComparer comparer) 39 { 40 this.base = base; 41 this.value = value; 42 this.comparer = comparer; 43 } 44 45 public Item next() throws XPathException { 46 while (true) { 47 AtomicValue i = (AtomicValue)base.next(); 48 if (i==null) break; 49 index++; 50 if (comparer.comparesEqual(i, value)) { 51 current = new IntegerValue(index); 52 position++; 53 return current; 54 } 55 } 56 current = null; 57 position = -1; 58 return null; 59 } 60 61 public Item current() { 62 return current; 63 } 64 65 public int position() { 66 return position; 67 } 68 69 public SequenceIterator getAnother() throws XPathException { 70 return new IndexIterator(base.getAnother(), value, comparer); 71 } 72 73 82 83 public int getProperties() { 84 return 0; 85 } 86 } 87 88 89 90 } 91 92 | Popular Tags |