KickJava   Java API By Example, From Geeks To Geeks.

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


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.trans.XPathException;
7 import net.sf.saxon.type.ExternalObjectType;
8 import net.sf.saxon.type.ItemType;
9 import net.sf.saxon.value.ObjectValue;
10 import net.sf.saxon.value.Value;
11
12 import java.io.PrintStream JavaDoc;
13 import java.util.Arrays JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 /**
17  * A tuple expression is an expression that returns a tuple. Specifically,
18  * it is a list of n expressions, which are evaluated to create a list of n items.
19  * Tuple expressions are used during the evaluation of a FLWR expression. A tuple
20  * is not a value within the XPath/XQuery type system, so it is represented as
21  * an external object, specifically as a Java array wrapped inside an ObjectValue.
22  *
23  */

24 public class TupleExpression extends ComputedExpression {
25
26     Expression[] components;
27
28     public TupleExpression(int width) {
29         components = new Expression[width];
30     }
31
32     public void setExpression(int i, Expression exp) {
33         components[i] = exp;
34         adoptChildExpression(components[i]);
35     }
36
37      public Expression simplify(StaticContext env) throws XPathException {
38         for (int i=0; i<components.length; i++) {
39             components[i] = components[i].simplify(env);
40             adoptChildExpression(components[i]);
41         }
42         return this;
43     }
44
45     public Expression promote(PromotionOffer offer) throws XPathException {
46         for (int i=0; i<components.length; i++) {
47             components[i] = doPromotion(components[i], offer);
48             adoptChildExpression(components[i]);
49         }
50         return this;
51     }
52
53     public ItemType getItemType() {
54         return new ExternalObjectType(Object JavaDoc.class);
55     }
56
57     public Expression typeCheck(StaticContext env, ItemType contextItemType) throws XPathException {
58         for (int i=0; i<components.length; i++) {
59             components[i] = components[i].typeCheck(env, contextItemType);
60             adoptChildExpression(components[i]);
61         }
62         return this;
63     }
64
65     /**
66      * Perform optimisation of an expression and its subexpressions.
67      * <p/>
68      * <p>This method is called after all references to functions and variables have been resolved
69      * to the declaration of the function or variable, and after all type checking has been done.</p>
70      *
71      * @param opt the optimizer in use. This provides access to supporting functions; it also allows
72      * different optimization strategies to be used in different circumstances.
73      * @param env the static context of the expression
74      * @param contextItemType the static type of "." at the point where this expression is invoked.
75      * The parameter is set to null if it is known statically that the context item will be undefined.
76      * If the type of the context item is not known statically, the argument is set to
77      * {@link net.sf.saxon.type.Type#ITEM_TYPE}
78      * @return the original expression, rewritten if appropriate to optimize execution
79      * @throws net.sf.saxon.trans.StaticError if an error is discovered during this phase
80      * (typically a type error)
81      */

82
83     public Expression optimize(Optimizer opt, StaticContext env, ItemType contextItemType) throws XPathException {
84         for (int i=0; i<components.length; i++) {
85             components[i] = components[i].optimize(opt, env, contextItemType);
86             adoptChildExpression(components[i]);
87         }
88         return this;
89     }
90
91     public void display(int level, NamePool pool, PrintStream JavaDoc out) {
92         out.println(ExpressionTool.indent(level) + "Tuple");
93         for (int i=0; i<components.length; i++) {
94             components[i].display(level+1, pool, out);
95         }
96     }
97
98
99     public Item evaluateItem(XPathContext context) throws XPathException {
100         Value[] tuple = new Value[components.length];
101         for (int i=0; i<components.length; i++) {
102             // Although TupleExpressions could be used for many purposes, in practice they are used
103
// for delivering the tuples used in a FLWOR "order by" clause; the first item in the tuple
104
// is the return value of the FLWOR, while the others are the sort keys. The return value and
105
// the first sort key will always be needed, so we evaluate them eagerly. The second and subsequent
106
// sort keys will often not be needed, so we evaluate them lazily.
107
if (i < 2) {
108                 tuple[i] = ExpressionTool.eagerEvaluate(components[i], context);
109             } else {
110                 tuple[i] = Value.asValue(ExpressionTool.lazyEvaluate(components[i], context, 10));
111             }
112         }
113         return new ObjectValue(tuple);
114     }
115
116     /**
117      * Get the cardinality of the expression. This is exactly one, in the sense
118      * that evaluating the TupleExpression returns a single tuple.
119      * @return the static cardinality - EXACTLY_ONE
120      */

121
122     public int computeCardinality() {
123         return StaticProperty.EXACTLY_ONE;
124     }
125
126     public int getIntrinsicDependencies() {
127         return 0;
128     }
129
130     public Iterator JavaDoc iterateSubExpressions() {
131         return Arrays.asList(components).iterator();
132     }
133
134 // public SequenceIterator iterate(XPathContext context) throws XPathException {
135
// return super.iterate(context);
136
// }
137

138 }
139
140
141 //
142
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
143
// you may not use this file except in compliance with the License. You may obtain a copy of the
144
// License at http://www.mozilla.org/MPL/
145
//
146
// Software distributed under the License is distributed on an "AS IS" basis,
147
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
148
// See the License for the specific language governing rights and limitations under the License.
149
//
150
// The Original Code is: all this file.
151
//
152
// The Initial Developer of the Original Code is Michael H. Kay
153
//
154
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
155
//
156
// Contributor(s): none
157
//
Popular Tags