KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > expr > SingletonComparison


1 package com.icl.saxon.expr;
2 import com.icl.saxon.*;
3
4
5 /**
6 * Singleton Comparison: A Relational Expression that compares a singleton node-set with a string
7 * or numeric value for equals, not-equals, greater-than or less-than.
8 */

9
10 public class SingletonComparison extends Expression {
11
12     SingletonExpression node;
13     int operator;
14     Value value;
15
16     public SingletonComparison(SingletonExpression p1, int op, Value p2) {
17         node = p1;
18         operator = op;
19         value = p2;
20     }
21
22     /**
23     * Simplify an expression
24     * @return the simplified expression
25     */

26
27     public Expression simplify() throws XPathException {
28         return this;
29     }
30
31     /**
32     * Evaluate the expression in a given context
33     * @param c the given context for evaluation
34     * @return a BooleanValue representing the result of the comparison of the two operands
35     */

36
37     public Value evaluate(Context c) throws XPathException {
38         return new BooleanValue(evaluateAsBoolean(c));
39     }
40
41     /**
42     * Evaluate the expression in a given context
43     * @param c the given context for evaluation
44     * @return a boolean representing the result of the numeric comparison of the two operands
45     */

46
47     public boolean evaluateAsBoolean(Context c) throws XPathException {
48         boolean exists = node.evaluateAsBoolean(c);
49         if (exists) {
50             if (value instanceof StringValue ||
51                             value instanceof FragmentValue ||
52                             value instanceof TextFragmentValue ) {
53                 switch (operator) {
54                     case Tokenizer.EQUALS:
55                         return node.evaluateAsString(c).equals(value.asString());
56                     case Tokenizer.NE:
57                         return !node.evaluateAsString(c).equals(value.asString());
58                     case Tokenizer.LT:
59                         return node.evaluateAsNumber(c) < value.asNumber();
60                     case Tokenizer.LE:
61                         return node.evaluateAsNumber(c) <= value.asNumber();
62                     case Tokenizer.GT:
63                         return node.evaluateAsNumber(c) > value.asNumber();
64                     case Tokenizer.GE:
65                         return node.evaluateAsNumber(c) >= value.asNumber();
66                     default:
67                         throw new XPathException("Bad operator in singleton comparison");
68                 }
69             } else if (value instanceof NumericValue) {
70                 switch(operator) {
71                     case Tokenizer.EQUALS:
72                         return node.evaluateAsNumber(c) == value.asNumber();
73                     case Tokenizer.NE:
74                         return node.evaluateAsNumber(c) != value.asNumber();
75                     case Tokenizer.LT:
76                         return node.evaluateAsNumber(c) < value.asNumber();
77                     case Tokenizer.LE:
78                         return node.evaluateAsNumber(c) <= value.asNumber();
79                     case Tokenizer.GT:
80                         return node.evaluateAsNumber(c) > value.asNumber();
81                     case Tokenizer.GE:
82                         return node.evaluateAsNumber(c) >= value.asNumber();
83                     default:
84                         throw new XPathException("Bad operator in singleton comparison");
85                 }
86             } else {
87                 throw new XPathException("Unrecognized type in singleton comparison");
88             }
89
90         } else {
91             return false;
92         }
93     }
94
95     /**
96     * Determine the data type of the expression, if possible
97     * @return Value.BOOLEAN
98     */

99
100     public int getDataType() {
101         return Value.BOOLEAN;
102     }
103
104     /**
105     * Determine which aspects of the context the expression depends on. The result is
106     * a bitwise-or'ed value composed from constants such as Context.VARIABLES and
107     * Context.CURRENT_NODE
108     */

109
110     public int getDependencies() {
111         return node.getDependencies();
112     }
113
114     /**
115     * Perform a partial evaluation of the expression, by eliminating specified dependencies
116     * on the context.
117     * @param dependencies The dependencies to be removed
118     * @param context The context to be used for the partial evaluation
119     * @return a new expression that does not have any of the specified
120     * dependencies
121     */

122
123     public Expression reduce(int dependencies, Context context) throws XPathException {
124         if ((node.getDependencies() & dependencies) != 0 ) {
125             Expression e = node.reduce(dependencies, context);
126             if (e instanceof SingletonExpression) {
127                 e = new SingletonComparison(
128                                 (SingletonExpression)e,
129                                 operator,
130                                 value);
131                 e.setStaticContext(getStaticContext());
132                 return e.simplify();
133             } else if (e instanceof NodeSetValue) {
134                 return new BooleanValue(((NodeSetValue)e).compare(operator, value));
135             } else {
136                 throw new XPathException("Failed to reduce SingletonComparison: returned " + e.getClass());
137             }
138         } else {
139             return this;
140         }
141     }
142     /**
143     * Diagnostic print of expression structure
144     */

145     
146     public void display(int level) {
147         System.err.println(indent(level) + "SingletonComparison " + Tokenizer.tokens[operator]);
148         node.display(level+1);
149         value.display(level+1);
150     }
151 }
152
153 //
154
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
155
// you may not use this file except in compliance with the License. You may obtain a copy of the
156
// License at http://www.mozilla.org/MPL/
157
//
158
// Software distributed under the License is distributed on an "AS IS" basis,
159
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
160
// See the License for the specific language governing rights and limitations under the License.
161
//
162
// The Original Code is: all this file.
163
//
164
// The Initial Developer of the Original Code is
165
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
166
//
167
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
168
//
169
// Contributor(s): none.
170
//
171
Popular Tags