KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.sort;
2
3 import net.sf.saxon.expr.*;
4 import net.sf.saxon.om.Item;
5 import net.sf.saxon.om.NamePool;
6 import net.sf.saxon.om.SequenceIterator;
7 import net.sf.saxon.trans.XPathException;
8 import net.sf.saxon.type.AnyItemType;
9 import net.sf.saxon.type.ItemType;
10 import net.sf.saxon.type.Type;
11 import net.sf.saxon.value.EmptySequence;
12 import net.sf.saxon.value.ObjectValue;
13 import net.sf.saxon.value.Value;
14
15 import java.io.PrintStream JavaDoc;
16 import java.util.Iterator JavaDoc;
17
18 /**
19  * A TupleSorter is an expression that sorts a stream of tuples. It is used
20  * to implement XQuery FLWR expressions.
21  */

22 public class TupleSorter extends ComputedExpression implements MappingFunction {
23
24     private Expression base;
25     private FixedSortKeyDefinition[] sortKeys;
26
27         // Although this class uses the FixedSortKeyDefinition class to define the sort keys,
28
// the actual sort key expression in the FixedSortKeyDefinition is not used. This is because
29
// the sort key is instead defined as one of the members of the tuple delivered by the
30
// TupleSorter. Therefore, the sort key expression is not managed as a child of this expression.
31

32     public TupleSorter(Expression base, FixedSortKeyDefinition[] keys) {
33         this.base = base;
34         this.sortKeys = keys;
35         adoptChildExpression(base);
36     }
37
38     public Expression simplify(StaticContext env) throws XPathException {
39         base = base.simplify(env);
40         return this;
41     }
42
43     public Expression typeCheck(StaticContext env, ItemType contextItemType) throws XPathException {
44         base = base.typeCheck(env, contextItemType);
45         return this;
46     }
47
48     public Expression optimize(Optimizer opt, StaticContext env, ItemType contextItemType) throws XPathException {
49         base = base.optimize(opt, env, contextItemType);
50         if (base instanceof EmptySequence) {
51             return base;
52         }
53         return this;
54     }
55
56     public ItemType getItemType() {
57         return AnyItemType.getInstance();
58             // TODO: we can do better than this, but we need more information
59
}
60
61     public int computeCardinality() {
62         return StaticProperty.ALLOWS_ZERO_OR_MORE;
63     }
64
65     public Iterator iterateSubExpressions() {
66         return new MonoIterator(base);
67     }
68
69     /**
70     * Promote this expression if possible
71     */

72
73     public Expression promote(PromotionOffer offer) throws XPathException {
74         Expression exp = offer.accept(this);
75         if (exp != null) {
76             return exp;
77         } else {
78             base = base.promote(offer);
79             return this;
80         }
81     }
82
83     public SequenceIterator iterate(XPathContext context) throws XPathException {
84         SequenceIterator iter = new SortedTupleIterator(context, base.iterate(context), sortKeys);
85         MappingIterator mapper = new MappingIterator(iter, this, context);
86         return mapper;
87     }
88
89     public boolean effectiveBooleanValue(XPathContext context) throws XPathException {
90         // so long as the sequence is homogeneous (all atomic values or all nodes), the EBV
91
// of the sorted sequence is the same as the EBV of the base sequence. Only if it is
92
// heterogeneous do we need to do the sort in order to calculate the EBV.
93
ItemType type = base.getItemType();
94         if (type == Type.ITEM_TYPE) {
95             return super.effectiveBooleanValue(context);
96         } else {
97             return base.effectiveBooleanValue(context);
98         }
99     }
100
101     public void display(int level, NamePool pool, PrintStream JavaDoc out) {
102         out.println(ExpressionTool.indent(level) + "TupleSorter");
103         base.display(level+1, pool, out);
104     }
105
106     /**
107      * Mapping function to map the wrapped objects returned by the SortedTupleIterator
108      * into real items. This is done because each tuple may actually represent a sequence
109      * of underlying values that share the same sort key.
110      */

111
112     public Object JavaDoc map(Item item, XPathContext context) throws XPathException {
113         ObjectValue tuple = (ObjectValue)item;
114         Object JavaDoc o = tuple.getObject();
115         if (o == null) {
116             return o;
117         }
118         if (o instanceof Item) {
119             return o;
120         }
121         Value value = (Value)o;
122         return value.iterate(context);
123     }
124
125 }
126
127
128 //
129
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
130
// you may not use this file except in compliance with the License. You may obtain a copy of the
131
// License at http://www.mozilla.org/MPL/
132
//
133
// Software distributed under the License is distributed on an "AS IS" basis,
134
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
135
// See the License for the specific language governing rights and limitations under the License.
136
//
137
// The Original Code is: all this file.
138
//
139
// The Initial Developer of the Original Code is Michael H. Kay
140
//
141
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
142
//
143
// Contributor(s): none
144
//
Popular Tags