KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

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

48
49     public boolean evaluateAsBoolean(Context c) throws XPathException {
50         NodeEnumeration enum = nodeset.enumerate(c, false);
51         switch (operator) {
52             case Tokenizer.EQUALS:
53                 if (value.getDataType() == value.NUMBER) {
54                     double n1 = value.asNumber();
55                     while (enum.hasMoreElements()) {
56                         NodeInfo node = enum.nextElement();
57                         if (Value.stringToNumber(node.getStringValue()) == n1) {
58                             return true;
59                         }
60                     }
61                     return false;
62                 } else {
63                     String s1 = value.asString();
64                     while (enum.hasMoreElements()) {
65                         NodeInfo node = enum.nextElement();
66                         if (node.getStringValue().equals(s1)) {
67                             return true;
68                         }
69                     }
70                     return false;
71                 }
72                 
73             case Tokenizer.NE:
74                 if (value.getDataType() == value.NUMBER) {
75                     double n2 = value.asNumber();
76                     while (enum.hasMoreElements()) {
77                         NodeInfo node = enum.nextElement();
78                         if (Value.stringToNumber(node.getStringValue()) != n2) {
79                             return true;
80                         }
81                     }
82                     return false;
83                 } else {
84                     String s2 = value.asString();
85                     while (enum.hasMoreElements()) {
86                         NodeInfo node = enum.nextElement();
87                         if (!node.getStringValue().equals(s2)) {
88                             return true;
89                         }
90                     }
91                     return false;
92                 }
93                             
94             case Tokenizer.LE:
95                 double n1 = value.asNumber();
96                 while (enum.hasMoreElements()) {
97                     NodeInfo node = enum.nextElement();
98                     if (Value.stringToNumber(node.getStringValue()) <= n1) {
99                         return true;
100                     }
101                 }
102                 return false;
103
104             case Tokenizer.LT:
105                 double n2 = value.asNumber();
106                 while (enum.hasMoreElements()) {
107                     NodeInfo node = enum.nextElement();
108                     if (Value.stringToNumber(node.getStringValue()) < n2) {
109                         return true;
110                     }
111                 }
112                 return false;
113
114             case Tokenizer.GE:
115                 double n3 = value.asNumber();
116                 while (enum.hasMoreElements()) {
117                     NodeInfo node = enum.nextElement();
118                     if (Value.stringToNumber(node.getStringValue()) >= n3) {
119                         return true;
120                     }
121                 }
122                 return false;
123
124             case Tokenizer.GT:
125                 double n4 = value.asNumber();
126                 while (enum.hasMoreElements()) {
127                     NodeInfo node = enum.nextElement();
128                     if (Value.stringToNumber(node.getStringValue()) > n4) {
129                         return true;
130                     }
131                 }
132                 return false;
133             
134             default:
135                 return false;
136         }
137             
138
139     }
140
141     /**
142     * Determine the data type of the expression, if possible
143     * @return Value.BOOLEAN
144     */

145
146     public int getDataType() {
147         return Value.BOOLEAN;
148     }
149
150     /**
151     * Determine which aspects of the context the expression depends on. The result is
152     * a bitwise-or'ed value composed from constants such as Context.VARIABLES and
153     * Context.CURRENT_NODE
154     */

155
156     public int getDependencies() {
157         return nodeset.getDependencies();
158     }
159
160     /**
161     * Perform a partial evaluation of the expression, by eliminating specified dependencies
162     * on the context.
163     * @param dependencies The dependencies to be removed
164     * @param context The context to be used for the partial evaluation
165     * @return a new expression that does not have any of the specified
166     * dependencies
167     */

168
169     public Expression reduce(int dependencies, Context context) throws XPathException {
170         if ((nodeset.getDependencies() & dependencies) != 0 ) {
171             Expression e = nodeset.reduce(dependencies, context);
172             if (e instanceof SingletonExpression) {
173                 e = new SingletonComparison(
174                                 (SingletonExpression)e,
175                                 operator,
176                                 value);
177                 e.setStaticContext(getStaticContext());
178                 return e.simplify();
179             } else if (e instanceof NodeSetExpression) {
180                 e = new NodeSetComparison(
181                                 (NodeSetExpression)e,
182                                 operator,
183                                 value);
184                 e.setStaticContext(getStaticContext());
185                 return e.simplify();
186             } else if (e instanceof NodeSetValue) {
187                 return new BooleanValue(((NodeSetValue)e).compare(operator, value));
188             } else {
189                 throw new XPathException("Failed to reduce NodeSetComparison: returned " + e.getClass());
190             }
191         } else {
192             return this;
193         }
194     }
195
196     /**
197     * Diagnostic print of expression structure
198     */

199     
200     public void display(int level) {
201         System.err.println(indent(level) + Tokenizer.tokens[operator]);
202         nodeset.display(level+1);
203         value.display(level+1);
204     }
205
206 }
207
208 //
209
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
210
// you may not use this file except in compliance with the License. You may obtain a copy of the
211
// License at http://www.mozilla.org/MPL/
212
//
213
// Software distributed under the License is distributed on an "AS IS" basis,
214
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
215
// See the License for the specific language governing rights and limitations under the License.
216
//
217
// The Original Code is: all this file.
218
//
219
// The Initial Developer of the Original Code is
220
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
221
//
222
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
223
//
224
// Contributor(s): none.
225
//
226
Popular Tags