KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > expr > IntegerRangeTest


1 package net.sf.saxon.expr;
2 import net.sf.saxon.om.Item;
3 import net.sf.saxon.om.NamePool;
4 import net.sf.saxon.trans.XPathException;
5 import net.sf.saxon.type.ItemType;
6 import net.sf.saxon.type.Type;
7 import net.sf.saxon.value.AtomicValue;
8 import net.sf.saxon.value.BooleanValue;
9 import net.sf.saxon.value.NumericValue;
10
11 import java.io.PrintStream JavaDoc;
12 import java.util.Arrays JavaDoc;
13 import java.util.Iterator JavaDoc;
14
15 /**
16 * An IntegerRangeTest is an expression of the form
17  * E = N to M
18  * where E, N, and M are all expressions of type integer.
19 */

20
21 public class IntegerRangeTest extends ComputedExpression {
22
23     Expression value;
24     Expression min;
25     Expression max;
26
27     /**
28     * Construct a IntegerRangeTest
29     */

30
31     public IntegerRangeTest(Expression value, Expression min, Expression max) {
32         this.value = value;
33         this.min = min;
34         this.max = max;
35     }
36
37     public Expression typeCheck(StaticContext env, ItemType contextItemType) throws XPathException {
38         // Already done, we only get one of these expressions after the operands
39
// have been analyzed
40
return this;
41     }
42
43     /**
44      * Perform optimisation of an expression and its subexpressions.
45      * <p/>
46      * <p>This method is called after all references to functions and variables have been resolved
47      * to the declaration of the function or variable, and after all type checking has been done.</p>
48      *
49      * @param opt the optimizer in use. This provides access to supporting functions; it also allows
50      * different optimization strategies to be used in different circumstances.
51      * @param env the static context of the expression
52      * @param contextItemType the static type of "." at the point where this expression is invoked.
53      * The parameter is set to null if it is known statically that the context item will be undefined.
54      * If the type of the context item is not known statically, the argument is set to
55      * {@link net.sf.saxon.type.Type#ITEM_TYPE}
56      * @return the original expression, rewritten if appropriate to optimize execution
57      * @throws net.sf.saxon.trans.StaticError if an error is discovered during this phase
58      * (typically a type error)
59      */

60
61     public Expression optimize(Optimizer opt, StaticContext env, ItemType contextItemType) throws XPathException {
62         return this;
63     }
64
65     /**
66     * Get the data type of the items returned
67     */

68
69     public ItemType getItemType() {
70         return Type.BOOLEAN_TYPE;
71     }
72
73     /**
74     * Determine the static cardinality
75     */

76
77     public int computeCardinality() {
78         return StaticProperty.EXACTLY_ONE;
79     }
80
81     /**
82      * Get the immediate sub-expressions of this expression. Default implementation
83      * returns a zero-length array, appropriate for an expression that has no
84      * sub-expressions.
85      *
86      * @return an iterator containing the sub-expressions of this expression
87      */

88
89     public Iterator JavaDoc iterateSubExpressions() {
90         Expression[] e = {value, min, max};
91         return Arrays.asList(e).iterator();
92     }
93
94     /**
95      * Evaluate the expression
96      */

97
98     public Item evaluateItem(XPathContext c) throws XPathException {
99         AtomicValue av = (AtomicValue)value.evaluateItem(c);
100         if (av==null) {
101             return BooleanValue.FALSE;
102         }
103         NumericValue v = (NumericValue)av.getPrimitiveValue();
104
105         AtomicValue av2 = (AtomicValue)min.evaluateItem(c);
106         NumericValue v2 = (NumericValue)av2.getPrimitiveValue();
107
108         if (v.compareTo(v2) < 0) {
109             return BooleanValue.FALSE;
110         }
111         AtomicValue av3 = (AtomicValue)max.evaluateItem(c);
112         NumericValue v3 = (NumericValue)av3.getPrimitiveValue();
113
114         return BooleanValue.get(v.compareTo(v3) <= 0);
115     }
116
117     /**
118      * Display this instruction as an expression, for diagnostics
119      */

120
121     public void display(int level, NamePool pool, PrintStream JavaDoc out) {
122         out.println(ExpressionTool.indent(level) + "rangeTest min<value<max");
123         min.display(level+1, pool, out);
124         value.display(level+1, pool, out);
125         max.display(level+1, pool, out);
126     }
127 }
128
129 //
130
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
131
// you may not use this file except in compliance with the License. You may obtain a copy of the
132
// License at http://www.mozilla.org/MPL/
133
//
134
// Software distributed under the License is distributed on an "AS IS" basis,
135
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
136
// See the License for the specific language governing rights and limitations under the License.
137
//
138
// The Original Code is: all this file.
139
//
140
// The Initial Developer of the Original Code is Michael H. Kay
141
//
142
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
143
//
144
// Contributor(s): none.
145
//
146
Popular Tags