KickJava   Java API By Example, From Geeks To Geeks.

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


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.trans.XPathException;
6 import net.sf.saxon.type.AtomicType;
7 import net.sf.saxon.type.ItemType;
8 import net.sf.saxon.type.Type;
9 import net.sf.saxon.value.AtomicValue;
10 import net.sf.saxon.value.Cardinality;
11 import net.sf.saxon.value.SequenceExtent;
12 import net.sf.saxon.value.Value;
13
14 /**
15 * An AtomicSequenceConverter is an expression that performs a cast on each member of
16 * a supplied sequence
17 */

18
19 public final class AtomicSequenceConverter extends UnaryExpression implements MappingFunction {
20
21     private AtomicType reqItemType;
22     private int requiredPrimitiveType;
23
24     /**
25     * Constructor
26     * @param sequence this must be a sequence of atomic values. This is not checked; a ClassCastException
27     * will occur if the precondition is not satisfied.
28     * @param requiredItemType the item type to which all items in the sequence should be converted,
29     * using the rules for "cast as".
30     */

31
32     public AtomicSequenceConverter(Expression sequence, AtomicType requiredItemType) {
33         super(sequence);
34         this.reqItemType = requiredItemType;
35         this.requiredPrimitiveType = requiredItemType.getPrimitiveType();
36         ExpressionTool.copyLocationInfo(sequence, this);
37     }
38
39     /**
40     * Simplify an expression
41     */

42
43      public Expression simplify(StaticContext env) throws XPathException {
44         operand = operand.simplify(env);
45         if (operand instanceof Value) {
46             return new SequenceExtent(iterate(null));
47         }
48         return this;
49     }
50
51     /**
52     * Type-check the expression
53     */

54
55     public Expression typeCheck(StaticContext env, ItemType contextItemType) throws XPathException {
56         operand = operand.typeCheck(env, contextItemType);
57         if (Type.isSubType(operand.getItemType(), reqItemType)) {
58             return operand;
59         } else if (!Cardinality.allowsMany(operand.getCardinality())) {
60             CastExpression cast = new CastExpression(operand, reqItemType,
61                                         (operand.getCardinality() & StaticProperty.ALLOWS_ZERO) != 0);
62             ExpressionTool.copyLocationInfo(this, cast);
63             cast.setParentExpression(getParentExpression());
64             return cast;
65         } else {
66             return this;
67         }
68     }
69
70     /**
71      * Determine the special properties of this expression
72      * @return {@link StaticProperty#NON_CREATIVE}.
73      */

74
75     public int computeSpecialProperties() {
76         int p = super.computeSpecialProperties();
77         return p | StaticProperty.NON_CREATIVE;
78     }
79
80     /**
81     * Iterate over the sequence of values
82     */

83
84     public SequenceIterator iterate(XPathContext context) throws XPathException {
85         SequenceIterator base = operand.iterate(context);
86         return new MappingIterator(base, this, null);
87     }
88
89     /**
90     * Evaluate as an Item. This should only be called if the AtomicSequenceConverter has cardinality zero-or-one
91     */

92
93     public Item evaluateItem(XPathContext context) throws XPathException {
94         Item item = operand.evaluateItem(context);
95         if (item==null) return null;
96         return ((AtomicValue)item).convert(requiredPrimitiveType, context);
97     }
98
99     /**
100     * Implement the mapping function
101     */

102
103     public Object JavaDoc map(Item item, XPathContext context) throws XPathException {
104         return ((AtomicValue)item).convert(requiredPrimitiveType, context);
105     }
106
107     /**
108     * Determine the data type of the items returned by the expression, if possible
109     * @return a value such as Type.STRING, Type.BOOLEAN, Type.NUMBER, Type.NODE,
110     * or Type.ITEM (meaning not known in advance)
111     */

112
113     public ItemType getItemType() {
114         return reqItemType;
115     }
116
117     /**
118     * Determine the static cardinality of the expression
119     */

120
121     public int computeCardinality() {
122         return operand.getCardinality();
123     }
124
125     /**
126      * Is this expression the same as another expression?
127      */

128
129     public boolean equals(Object JavaDoc other) {
130         return super.equals(other) &&
131                 requiredPrimitiveType == ((AtomicSequenceConverter)other).requiredPrimitiveType;
132     }
133
134     /**
135      * Give a string representation of the operator for use in diagnostics
136      * @return the operator, as a string
137      */

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