KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > sort > SortedGroupIterator


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 /**
13  * A SortedGroupIterator is a modified SortedIterator. It sorts a sequence of groups,
14  * and is itself a GroupIterator. The modifications retain extra information about
15  * the items being sorted. The items are each the leading item of a group, and as well
16  * as the item itself, the iterator preserves information about the group: specifically,
17  * an iterator over the items in the group, and the value of the grouping key (if any).
18  */

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         // add two items to each tuple, for the iterator over the items in the group,
30
// and the grouping key, respectively.
31
recordSize += 2;
32     }
33
34     /**
35      * Override the method that builds the array of values and sort keys.
36      * @throws XPathException
37      */

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 JavaDoc[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                 // this provides the context for evaluating the sort key
55

56         // initialise the array with data
57

58         while (true) {
59             Item item = base.next();
60             if (item == null) {
61                 break;
62             }
63             if (count==allocated) {
64                 allocated *= 2;
65                 Object JavaDoc[] nk2 = new Object JavaDoc[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 JavaDoc(count);
75             // extra code added to superclass
76
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 //
95
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
96
// you may not use this file except in compliance with the License. You may obtain a copy of the
97
// License at http://www.mozilla.org/MPL/
98
//
99
// Software distributed under the License is distributed on an "AS IS" basis,
100
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
101
// See the License for the specific language governing rights and limitations under the License.
102
//
103
// The Original Code is: all this file.
104
//
105
// The Initial Developer of the Original Code is Michael H. Kay
106
//
107
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
108
//
109
// Contributor(s): none
110
//
Popular Tags