KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.sort;
2
3 import net.sf.saxon.expr.XPathContext;
4 import net.sf.saxon.om.SequenceIterator;
5 import net.sf.saxon.trans.XPathException;
6 import net.sf.saxon.value.EmptySequence;
7 import net.sf.saxon.value.ObjectValue;
8 import net.sf.saxon.value.Value;
9
10 /**
11  * A SortedTupleIterator is a modified SortedIterator. Whereas the sorted iterator
12  * used by XSLT computes the sort key of each item in a sequence, using that item
13  * as the context item, the SortedTupleIterator used by XQuery precomputes the sort
14  * keys from scratch; they do not need to be a function of the item being sorted.
15  */

16
17 class SortedTupleIterator extends SortedIterator {
18
19     // Note, the sort key expression within the FixedSortKeyDefinition is not used
20
// in this subclass.
21

22     public SortedTupleIterator(XPathContext context, SequenceIterator base,
23                                FixedSortKeyDefinition[] sortKeys) throws XPathException {
24         super(context, base, sortKeys);
25     }
26
27     /**
28      * Override the method that builds the array of values and sort keys.
29      * @throws XPathException
30      */

31
32     protected void buildArray() throws XPathException {
33         int allocated = 100;
34         nodeKeys = new Object JavaDoc[allocated * recordSize];
35         count = 0;
36
37         // initialise the array with data
38

39         while (true) {
40             ObjectValue tupleObject = (ObjectValue)base.next();
41             if (tupleObject == null) {
42                 break;
43             }
44             Value[] tuple = (Value[])tupleObject.getObject();
45             if (count==allocated) {
46                 allocated *= 2;
47                 Object JavaDoc[] nk2 = new Object JavaDoc[allocated * recordSize];
48                 System.arraycopy(nodeKeys, 0, nk2, 0, count * recordSize);
49                 nodeKeys = nk2;
50             }
51             int k = count*recordSize;
52             nodeKeys[k] = new ObjectValue(tuple[0]);
53                 // this is the "item" that will be returned by the TupleIterator.
54
// In general it is actually a sequence, so we wrap it in an ObjectValue
55
// It subsequently gets unwrapped by the MappingFunction applied to the
56
// output of the SortedTupleIterator.
57
for (int n=1; n<=sortkeys.length; n++) {
58                 Value v = tuple[n].reduce();
59                 if (v instanceof EmptySequence) {
60                     nodeKeys[k+n] = null;
61                 } else {
62                     nodeKeys[k+n] = v;
63                 }
64             }
65             nodeKeys[k+sortkeys.length+1] = new Integer JavaDoc(count);
66             count++;
67         }
68     }
69 }
70
71
72 //
73
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
74
// you may not use this file except in compliance with the License. You may obtain a copy of the
75
// License at http://www.mozilla.org/MPL/
76
//
77
// Software distributed under the License is distributed on an "AS IS" basis,
78
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
79
// See the License for the specific language governing rights and limitations under the License.
80
//
81
// The Original Code is: all this file.
82
//
83
// The Initial Developer of the Original Code is Michael H. Kay
84
//
85
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
86
//
87
// Contributor(s): none
88
//
Popular Tags