KickJava   Java API By Example, From Geeks To Geeks.

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


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.AtomicType;
6 import net.sf.saxon.type.BuiltInAtomicType;
7 import net.sf.saxon.type.ItemType;
8 import net.sf.saxon.type.Type;
9 import net.sf.saxon.value.*;
10
11 /**
12 * Castable Expression: implements "Expr castable as atomic-type?".
13 * The implementation simply wraps a cast expression with a try/catch.
14 */

15
16 public final class CastableExpression extends UnaryExpression {
17
18     AtomicType targetType;
19     boolean allowEmpty;
20
21     public CastableExpression(Expression source, AtomicType target, boolean allowEmpty) {
22         super(source);
23         this.targetType = target;
24         this.allowEmpty = allowEmpty;
25     }
26
27     /**
28     * Simplify the expression
29     * @return the simplified expression
30     */

31
32      public Expression simplify(StaticContext env) throws XPathException {
33         operand = operand.simplify(env);
34         if (operand instanceof Value) {
35             return BooleanValue.get(effectiveBooleanValue(null));
36         }
37         return this;
38     }
39
40     /**
41     * Type-check the expression
42     */

43
44     public Expression typeCheck(StaticContext env, ItemType contextItemType) throws XPathException {
45         operand = operand.typeCheck(env, contextItemType);
46         SequenceType atomicType = SequenceType.makeSequenceType(
47                 Type.ANY_ATOMIC_TYPE,
48                                  (allowEmpty ? StaticProperty.ALLOWS_ZERO_OR_ONE
49                                              : StaticProperty.EXACTLY_ONE));
50
51         RoleLocator role = new RoleLocator(RoleLocator.TYPE_OP, "castable as", 0, null);
52         role.setSourceLocator(this);
53         operand = TypeChecker.staticTypeCheck(operand, atomicType, false, role, env);
54
55         if (operand instanceof AtomicValue) {
56             return BooleanValue.get(effectiveBooleanValue(null));
57         }
58         return this;
59     }
60
61     /**
62     * Type-check the expression
63     */

64
65     public Expression optimize(Optimizer opt, StaticContext env, ItemType contextItemType) throws XPathException {
66         operand = operand.optimize(opt, env, contextItemType);
67         if (operand instanceof AtomicValue) {
68             return BooleanValue.get(effectiveBooleanValue(null));
69         }
70         return this;
71     }
72
73
74
75     /**
76      * Is this expression the same as another expression?
77      */

78
79     public boolean equals(Object JavaDoc other) {
80         return super.equals(other) &&
81                 targetType == ((CastableExpression)other).targetType &&
82                 allowEmpty == ((CastableExpression)other).allowEmpty;
83     }
84
85     /**
86     * Determine the data type of the result of the Castable expression
87     */

88
89     public ItemType getItemType() {
90         return Type.BOOLEAN_TYPE;
91     }
92
93     public int computeCardinality() {
94         return StaticProperty.EXACTLY_ONE;
95     }
96
97     /**
98      * Determine the special properties of this expression
99      * @return {@link StaticProperty#NON_CREATIVE}.
100      */

101
102     public int computeSpecialProperties() {
103         int p = super.computeSpecialProperties();
104         return p | StaticProperty.NON_CREATIVE;
105     }
106
107     /**
108     * Evaluate the expression
109     */

110
111     public Item evaluateItem(XPathContext context) {
112         return BooleanValue.get(effectiveBooleanValue(context));
113     }
114
115     public boolean effectiveBooleanValue(XPathContext context) {
116         try {
117             AtomicValue value = (AtomicValue)operand.evaluateItem(context);
118             if (value == null) {
119                 return allowEmpty;
120             }
121             if (targetType instanceof BuiltInAtomicType) {
122                 return !(value.convert(targetType, context, true) instanceof ValidationErrorValue);
123             } else {
124                 AtomicValue prim =
125                     value.convert((AtomicType)targetType.getBuiltInBaseType(), context, true);
126                 if (prim instanceof ValidationErrorValue) {
127                     return false;
128                 }
129                 AtomicValue val =
130                     targetType.makeDerivedValue(prim, prim.getStringValueCS(), true);
131                 return !(val instanceof ValidationErrorValue);
132             }
133         } catch (XPathException err) {
134             return false;
135         }
136     }
137
138     /**
139      * Give a string representation of the operator for use in diagnostics
140      * @return the operator, as a string
141      */

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