KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.expr;
2 import net.sf.saxon.om.NamePool;
3 import net.sf.saxon.trans.DynamicError;
4 import net.sf.saxon.trans.XPathException;
5 import net.sf.saxon.type.ItemType;
6 import net.sf.saxon.value.Value;
7
8 import java.io.PrintStream JavaDoc;
9 import java.util.Iterator JavaDoc;
10
11 /**
12 * Unary Expression: an expression taking a single operand expression
13 */

14
15 public abstract class UnaryExpression extends ComputedExpression {
16
17     protected Expression operand;
18
19     public UnaryExpression(Expression p0) {
20         operand = p0;
21         Container parent = (p0 instanceof ComputedExpression ? ((ComputedExpression)p0).getParentExpression() : null);
22         adoptChildExpression(p0);
23         setParentExpression(parent);
24     }
25
26     public Expression getBaseExpression() {
27         return operand;
28     }
29
30     /**
31     * Simplify an expression
32     * @return the simplified expression
33     */

34
35      public Expression simplify(StaticContext env) throws XPathException {
36         operand = operand.simplify(env);
37         return this;
38     }
39
40     /**
41     * Type-check the expression. Default implementation for unary operators that accept
42     * any kind of operand
43     */

44
45     public Expression typeCheck(StaticContext env, ItemType contextItemType) throws XPathException {
46         operand = operand.typeCheck(env, contextItemType);
47         // if the operand value is known, pre-evaluate the expression
48
try {
49             if (operand instanceof Value) {
50                 return ExpressionTool.eagerEvaluate(this, null);
51             }
52         } catch (DynamicError err) {
53             // if early evaluation fails, suppress the error: the value might
54
// not be needed at run-time
55
}
56         return this;
57     }
58
59     /**
60      * Perform optimisation of an expression and its subexpressions.
61      * <p/>
62      * <p>This method is called after all references to functions and variables have been resolved
63      * to the declaration of the function or variable, and after all type checking has been done.</p>
64      *
65      * @param opt the optimizer in use. This provides access to supporting functions; it also allows
66      * different optimization strategies to be used in different circumstances.
67      * @param env the static context of the expression
68      * @param contextItemType the static type of "." at the point where this expression is invoked.
69      * The parameter is set to null if it is known statically that the context item will be undefined.
70      * If the type of the context item is not known statically, the argument is set to
71      * {@link net.sf.saxon.type.Type#ITEM_TYPE}
72      * @return the original expression, rewritten if appropriate to optimize execution
73      * @throws net.sf.saxon.trans.StaticError if an error is discovered during this phase
74      * (typically a type error)
75      */

76
77     public Expression optimize(Optimizer opt, StaticContext env, ItemType contextItemType) throws XPathException {
78         operand = operand.optimize(opt, env, contextItemType);
79         // if the operand value is known, pre-evaluate the expression
80
try {
81             if (operand instanceof Value) {
82                 return ExpressionTool.eagerEvaluate(this, null);
83             }
84         } catch (DynamicError err) {
85             // if early evaluation fails, suppress the error: the value might
86
// not be needed at run-time
87
}
88         return this;
89     }
90
91
92     /**
93     * Promote this expression if possible
94     */

95
96     public Expression promote(PromotionOffer offer) throws XPathException {
97         Expression exp = offer.accept(this);
98         if (exp != null) {
99             return exp;
100         } else {
101             operand = doPromotion(operand, offer);
102             return this;
103         }
104     }
105
106     /**
107     * Get the immediate subexpressions of this expression
108     */

109
110     public Iterator JavaDoc iterateSubExpressions() {
111         return new MonoIterator(operand);
112     }
113
114     /**
115     * Get the static properties of this expression (other than its type). The result is
116     * bit-signficant. These properties are used for optimizations. In general, if
117     * property bit is set, it is true, but if it is unset, the value is unknown.
118     */

119
120     public int computeSpecialProperties() {
121         return operand.getSpecialProperties();
122     }
123
124     /**
125     * Determine the static cardinality. Default implementation returns the cardinality of the operand
126     */

127
128     public int computeCardinality() {
129         return operand.getCardinality();
130     }
131
132     /**
133      * Determine the data type of the expression, if possible. The default
134      * implementation for unary expressions returns the item type of the operand
135      * @return the item type of the items in the result sequence, insofar as this
136      * is known statically.
137      */

138
139     public ItemType getItemType() {
140         return operand.getItemType();
141     }
142
143     /**
144     * Is this expression the same as another expression?
145     */

146
147     public boolean equals(Object JavaDoc other) {
148         return this.getClass().equals(other.getClass()) &&
149                 this.operand.equals(((UnaryExpression)other).operand);
150     }
151
152     /**
153     * get HashCode for comparing two expressions. Note that this hashcode gives the same
154      * result for (A op B) and for (B op A), whether or not the operator is commutative.
155     */

156
157     public int hashCode() {
158         return ("UnaryExpression " + getClass()).hashCode() ^ operand.hashCode();
159     }
160
161     /**
162     * Diagnostic print of expression structure
163     */

164
165     public void display(int level, NamePool pool, PrintStream JavaDoc out) {
166         out.println(ExpressionTool.indent(level) + displayOperator(pool));
167         operand.display(level+1, pool, out);
168     }
169
170     /**
171      * Give a string representation of the operator for use in diagnostics
172      * @return the operator, as a string
173      */

174
175     protected abstract String JavaDoc displayOperator(NamePool pool);
176
177 }
178
179 //
180
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
181
// you may not use this file except in compliance with the License. You may obtain a copy of the
182
// License at http://www.mozilla.org/MPL/
183
//
184
// Software distributed under the License is distributed on an "AS IS" basis,
185
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
186
// See the License for the specific language governing rights and limitations under the License.
187
//
188
// The Original Code is: all this file.
189
//
190
// The Initial Developer of the Original Code is Michael H. Kay.
191
//
192
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
193
//
194
// Contributor(s): none.
195
//
196
Popular Tags