KickJava   Java API By Example, From Geeks To Geeks.

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


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.pattern.NodeTest;
6 import net.sf.saxon.trans.XPathException;
7 import net.sf.saxon.type.AnyItemType;
8 import net.sf.saxon.type.AtomicType;
9 import net.sf.saxon.type.ItemType;
10 import net.sf.saxon.type.Type;
11 import net.sf.saxon.value.*;
12
13 /**
14 * An UntypedAtomicConverter is an expression that converts any untypedAtomic items in
15 * a sequence to a specified type
16 */

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

32
33     public UntypedAtomicConverter(Expression sequence, AtomicType requiredItemType, boolean allConverted) {
34         super(sequence);
35         this.requiredItemType = requiredItemType;
36         this.allConverted = allConverted;
37         ExpressionTool.copyLocationInfo(sequence, this);
38     }
39
40     /**
41     * Determine the data type of the items returned by the expression
42     */

43
44     public ItemType getItemType() {
45         if (allConverted) {
46             return requiredItemType;
47         } else {
48             return Type.getCommonSuperType(requiredItemType, operand.getItemType());
49         }
50     }
51
52     /**
53     * Type-check the expression
54     */

55
56     public Expression typeCheck(StaticContext env, ItemType contextItemType) throws XPathException {
57         operand = operand.typeCheck(env, contextItemType);
58         if (operand instanceof Value) {
59             return SequenceExtent.makeSequenceExtent(iterate(null)).simplify(env);
60         }
61         ItemType type = operand.getItemType();
62         if (type instanceof NodeTest) {
63             return this;
64         }
65         if (type==Type.ANY_ATOMIC_TYPE || type instanceof AnyItemType ||
66                 type==Type.UNTYPED_ATOMIC_TYPE) {
67             return this;
68         }
69         // the sequence can't contain any untyped atomic values, so there's no need for
70
// a converter
71
return operand;
72     }
73
74
75     /**
76      * Determine the special properties of this expression
77      * @return {@link StaticProperty#NON_CREATIVE}.
78      */

79
80     public int computeSpecialProperties() {
81         int p = super.computeSpecialProperties();
82         return p | StaticProperty.NON_CREATIVE;
83     }
84
85      /**
86     * Iterate over the sequence of values
87     */

88
89     public SequenceIterator iterate(XPathContext context) throws XPathException {
90         SequenceIterator base = operand.iterate(context);
91         return new MappingIterator(base, this, context);
92     }
93
94     /**
95     * Evaluate as an Item. This should only be called if the UntypedAtomicConverter has cardinality zero-or-one
96     */

97
98     public Item evaluateItem(XPathContext context) throws XPathException {
99         Item item = operand.evaluateItem(context);
100         if (item==null) return null;
101         if (item instanceof UntypedAtomicValue) {
102             try {
103                 AtomicValue val = ((UntypedAtomicValue)item).convert(requiredItemType, context, true);
104                 if (val instanceof ValidationErrorValue) {
105                     throw ((ValidationErrorValue)val).getException();
106                 }
107                 return val;
108             } catch (XPathException e) {
109                 if (e.getLocator() == null) {
110                     e.setLocator(this);
111                 }
112                 throw e;
113             }
114         } else {
115             //testConformance(item, context);
116
return item;
117         }
118     }
119
120     /**
121     * Implement the mapping function
122     */

123
124     public Object JavaDoc map(Item item, XPathContext context) throws XPathException {
125         if (item instanceof UntypedAtomicValue) {
126             Value val = ((UntypedAtomicValue)item).convert(requiredItemType, context, true);
127             if (val instanceof ValidationErrorValue) {
128                 throw ((ValidationErrorValue)val).getException();
129             }
130             return val;
131         } else {
132             return item;
133         }
134     }
135
136     /**
137      * Give a string representation of the operator for use in diagnostics
138      * @return the operator, as a string
139      */

140
141     protected String JavaDoc displayOperator(NamePool pool) {
142         return "convert untyped atomic items to " + requiredItemType.toString(pool);
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