1 package net.sf.saxon.sort; 2 3 import net.sf.saxon.expr.LastPositionFinder; 4 import net.sf.saxon.expr.XPathContext; 5 import net.sf.saxon.expr.XPathContextMajor; 6 import net.sf.saxon.om.Item; 7 import net.sf.saxon.om.SequenceIterator; 8 import net.sf.saxon.trace.InstructionInfoProvider; 9 import net.sf.saxon.trans.XPathException; 10 import net.sf.saxon.value.AtomicValue; 11 12 19 20 public class SortedGroupIterator extends SortedIterator implements GroupIterator { 21 22 private InstructionInfoProvider origin; 23 24 public SortedGroupIterator(XPathContext context, GroupIterator base, 25 FixedSortKeyDefinition[] sortKeys, 26 InstructionInfoProvider origin) throws XPathException { 27 super(context, base, sortKeys); 28 this.origin = origin; 29 recordSize += 2; 32 } 33 34 38 39 protected void buildArray() throws XPathException { 40 int allocated; 41 if ((base.getProperties() & SequenceIterator.LAST_POSITION_FINDER) != 0) { 42 allocated = ((LastPositionFinder)base).getLastPosition(); 43 } else { 44 allocated = 100; 45 } 46 47 nodeKeys = new Object [allocated * recordSize]; 48 count = 0; 49 50 XPathContextMajor c2 = context.newContext(); 51 c2.setCurrentIterator(base); 52 c2.setOrigin(origin); 53 c2.setCurrentGroupIterator((GroupIterator)base); 54 56 58 while (true) { 59 Item item = base.next(); 60 if (item == null) { 61 break; 62 } 63 if (count==allocated) { 64 allocated *= 2; 65 Object [] nk2 = new Object [allocated * recordSize]; 66 System.arraycopy(nodeKeys, 0, nk2, 0, count * recordSize); 67 nodeKeys = nk2; 68 } 69 int k = count*recordSize; 70 nodeKeys[k] = item; 71 for (int n=0; n<sortkeys.length; n++) { 72 nodeKeys[k+n+1] = sortkeys[n].getSortKey().evaluateItem(c2); 73 } 74 nodeKeys[k+sortkeys.length+1] = new Integer (count); 75 nodeKeys[k+sortkeys.length+2] = ((GroupIterator)base).getCurrentGroupingKey(); 77 nodeKeys[k+sortkeys.length+3] = ((GroupIterator)base).iterateCurrentGroup(); 78 count++; 79 } 80 } 81 82 public AtomicValue getCurrentGroupingKey() { 83 return (AtomicValue)nodeKeys[(index-1)*recordSize+sortkeys.length+2]; 84 } 85 86 public SequenceIterator iterateCurrentGroup() throws XPathException { 87 SequenceIterator iter = 88 (SequenceIterator)nodeKeys[(index-1)*recordSize+sortkeys.length+3]; 89 return iter.getAnother(); 90 } 91 } 92 93 94 | Popular Tags |