1 package net.sf.saxon.expr; 2 import net.sf.saxon.om.Item; 3 import net.sf.saxon.om.LookaheadIterator; 4 import net.sf.saxon.om.NodeInfo; 5 import net.sf.saxon.om.SequenceIterator; 6 import net.sf.saxon.sort.NodeOrderComparer; 7 import net.sf.saxon.trans.XPathException; 8 9 12 13 public class UnionEnumeration implements SequenceIterator, LookaheadIterator { 14 15 private SequenceIterator e1; 16 private SequenceIterator e2; 17 private NodeInfo nextNode1 = null; 18 private NodeInfo nextNode2 = null; 19 private NodeOrderComparer comparer; 20 private NodeInfo current = null; 21 private int position = 0; 22 23 27 28 public UnionEnumeration(SequenceIterator p1, SequenceIterator p2, 29 NodeOrderComparer comparer) throws XPathException { 30 this.e1 = p1; 31 this.e2 = p2; 32 this.comparer = comparer; 33 34 nextNode1 = next(e1); 35 nextNode2 = next(e2); 36 } 37 38 42 43 private NodeInfo next(SequenceIterator iter) throws XPathException { 44 return (NodeInfo)iter.next(); 45 } 47 48 public boolean hasNext() { 49 return nextNode1!=null || nextNode2!=null; 50 } 51 52 public Item next() throws XPathException { 53 54 56 position++; 57 if (nextNode1 != null && nextNode2 != null) { 58 int c = comparer.compare(nextNode1, nextNode2); 59 if (c<0) { 60 current = nextNode1; 61 nextNode1 = next(e1); 62 return current; 63 64 } else if (c>0) { 65 current = nextNode2; 66 nextNode2 = next(e2); 67 return current; 68 69 } else { 70 current = nextNode2; 71 nextNode2 = next(e2); 72 nextNode1 = next(e1); 73 return current; 74 } 75 } 76 77 79 if (nextNode1!=null) { 80 current = nextNode1; 81 nextNode1 = next(e1); 82 return current; 83 } 84 if (nextNode2!=null) { 85 current = nextNode2; 86 nextNode2 = next(e2); 87 return current; 88 } 89 current = null; 90 position = -1; 91 return null; 92 } 93 94 public Item current() { 95 return current; 96 } 97 98 public int position() { 99 return position; 100 } 101 102 public SequenceIterator getAnother() throws XPathException { 103 return new UnionEnumeration(e1.getAnother(), e2.getAnother(), comparer); 104 } 105 106 115 116 public int getProperties() { 117 return LOOKAHEAD; 118 } 119 120 } 121 122 123 124 | Popular Tags |