KickJava   Java API By Example, From Geeks To Geeks.

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


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.om.SequenceIterator;
5 import net.sf.saxon.style.StandardNames;
6 import net.sf.saxon.trans.DynamicError;
7 import net.sf.saxon.trans.XPathException;
8 import net.sf.saxon.type.ItemType;
9 import net.sf.saxon.type.Type;
10 import net.sf.saxon.value.*;
11
12 /**
13 * A NumericPromoter performs numeric promotion on each item in a supplied sequence
14 */

15
16 public final class NumericPromoter extends UnaryExpression implements MappingFunction {
17
18     private int requiredType; // always xs:float or xs:double
19

20     /**
21     * Constructor
22     * @param sequence this must be a sequence of atomic values. This is not checked; a ClassCastException
23     * will occur if the precondition is not satisfied.
24     * @param requiredType the item type to which all items in the sequence should be converted,
25     * using the rules for "cast as".
26     */

27
28     public NumericPromoter(Expression sequence, int requiredType) {
29         super(sequence);
30         this.requiredType = requiredType;
31         ExpressionTool.copyLocationInfo(sequence, this);
32     }
33
34     /**
35     * Simplify an expression
36     */

37
38      public Expression simplify(StaticContext env) throws XPathException {
39         operand = operand.simplify(env);
40         if (operand instanceof AtomicValue) {
41             return promote(((AtomicValue)operand), null);
42         } else if (operand instanceof Value) {
43             return SequenceExtent.makeSequenceExtent(iterate(null)).reduce();
44         }
45         return this;
46     }
47
48     /**
49     * Type-check the expression
50     */

51
52     public Expression typeCheck(StaticContext env, ItemType contextItemType) throws XPathException {
53         operand = operand.typeCheck(env, contextItemType);
54         return this;
55     }
56
57     /**
58     * Optimize the expression
59     */

60
61     public Expression optimize(Optimizer opt, StaticContext env, ItemType contextItemType) throws XPathException {
62         operand = operand.optimize(opt, env, contextItemType);
63         return this;
64     }
65
66     /**
67     * Iterate over the sequence of values
68     */

69
70     public SequenceIterator iterate(XPathContext context) throws XPathException {
71         SequenceIterator base = operand.iterate(context);
72         return new MappingIterator(base, this, null);
73     }
74
75     /**
76     * Evaluate as an Item. This should only be called if the expression has cardinality zero-or-one
77     */

78
79     public Item evaluateItem(XPathContext context) throws XPathException {
80         Item item = operand.evaluateItem(context);
81         if (item==null) return null;
82         return promote(((AtomicValue)item), context);
83     }
84
85     /**
86     * Implement the mapping function
87     */

88
89     public Object JavaDoc map(Item item, XPathContext context) throws XPathException {
90         return promote(((AtomicValue)item), context);
91     }
92
93     /**
94      * Perform the promotion
95      */

96
97     private AtomicValue promote(AtomicValue value, XPathContext context) throws XPathException {
98         AtomicValue v = value.getPrimitiveValue();
99         if (!(v instanceof NumericValue || v instanceof UntypedAtomicValue)) {
100             DynamicError err = new DynamicError("Cannot promote non-numeric value to " + getItemType().toString());
101             err.setLocator(this);
102             err.setXPathContext(context);
103             throw err;
104         }
105         return v.convert(requiredType, context);
106     }
107
108     /**
109     * Determine the data type of the items returned by the expression, if possible
110     * @return a value such as Type.STRING, Type.BOOLEAN, Type.NUMBER, Type.NODE,
111     * or Type.ITEM (meaning not known in advance)
112     */

113
114     public ItemType getItemType() {
115         if (requiredType == StandardNames.XS_DOUBLE) {
116             return Type.DOUBLE_TYPE;
117         } else {
118             return Type.FLOAT_TYPE;
119         }
120     }
121
122     /**
123      * Is this expression the same as another expression?
124      */

125
126     public boolean equals(Object JavaDoc other) {
127         return super.equals(other) &&
128                 requiredType == ((NumericPromoter)other).requiredType;
129     }
130
131     /**
132      * Give a string representation of the operator for use in diagnostics
133      * @return the operator, as a string
134      */

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