KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.icl.saxon.expr;
2 import com.icl.saxon.*;
3
4 import java.util.*;
5
6 /**
7 * A value is the result of an expression but it is also an expression in its own right
8 */

9
10 public abstract class Value extends Expression {
11
12     /**
13     * Static method to convert strings to numbers. Might as well go here as anywhere else.
14     * @param s the String to be converted
15     * @return a double representing the value of the String; if it cannot be converted,
16     * return NaN (as required by the XSL specification)
17     */

18
19     public static double stringToNumber(String JavaDoc s) {
20         // check for things allowed by Java but not by XPath 1.0
21
if (s.indexOf('+')>=0 ||
22                 s.indexOf('e')>=0 ||
23                 s.indexOf('E')>=0 ) {
24             return Double.NaN;
25         }
26         try {
27             return new Double JavaDoc(s.trim()).doubleValue();
28         } catch (NumberFormatException JavaDoc err) {
29             return Double.NaN;
30         }
31     }
32
33     /**
34     * Constants denoting the data types of an expression or value
35     */

36
37     public static final int BOOLEAN = 1;
38     public static final int NUMBER = 2;
39     public static final int STRING = 3;
40     public static final int NODESET = 4;
41     //public static final int FRAGMENT = 5;
42
public static final int OBJECT = 6;
43     public static final int ANY = -1;
44     
45     /**
46     * Evaluate the Value. Null operation, because it has already been evaluated
47     * @param context The context (not used)
48     * @return the value, unchanged
49     */

50
51     public Value evaluate(Context context) throws XPathException {
52         return this;
53     }
54
55     /**
56     * Simplify an expression
57     * @return the simplified expression
58     */

59
60     public Expression simplify() {
61         return this;
62     }
63
64     /**
65     * Determine which aspects of the context the expression depends on. The result is
66     * a bitwise-or'ed value composed from constants such as Context.VARIABLES and
67     * Context.CURRENT_NODE
68     */

69
70     public int getDependencies() {
71         return 0;
72     }
73
74     /**
75     * Convert the value to a String value
76     * @return the value converted to a String
77     */

78
79     public abstract String JavaDoc asString() throws XPathException;
80
81     /**
82     * Convert the value to a Number
83     * @return the value converted to a String
84     */

85
86     public abstract double asNumber() throws XPathException;
87
88     /**
89     * Convert the value to a Boolean
90     * @return the value converted to a Boolean
91     */

92
93     public abstract boolean asBoolean() throws XPathException;
94
95     /**
96     * Test whether two values are equal. See the XSL specification: if either operand is a
97     * nodeset, they are compared as nodesets; else if either is a boolean, they
98     * are compared as booleans; else if either operand is a number, they are compared as numbers;
99     * else they are compared as strings.
100     * @return a boolean giving the value of the expression, evaluated in the current context
101     */

102
103     public boolean equals(Value other) throws XPathException {
104
105         // if this is a NodeSet value, the method will be handled by the NodeSetValue class
106

107         if (other instanceof NodeSetValue)
108             return other.equals(this);
109             
110         if (this instanceof BooleanValue || other instanceof BooleanValue)
111             return this.asBoolean() == other.asBoolean();
112             
113         if (this instanceof NumericValue || other instanceof NumericValue)
114             return this.asNumber() == other.asNumber();
115                     
116         return this.asString().equals(other.asString());
117
118     }
119
120     /**
121     * Test whether two values are not-equal. Note that a!=b means the same as !(a=b) except
122     * where either a or b is a nodeset.
123     * @return a boolean giving the value of the expression, evaluated in the current context
124     */

125
126     public boolean notEquals(Value other) throws XPathException {
127
128         // if this is a NodeSet value, the method will be handled by the NodeSetValue class
129

130         if (other instanceof NodeSetValue)
131             return other.notEquals(this);
132
133         return !equals(other);
134     }
135
136     /**
137     * Test how a Value compares to another Value under a relational comparison.
138     * Note that the method is overridden for NodeSetValue
139     * @param operator The comparison operator, one of Tokenizer.LE, Tokenizer.LT,
140     * Tokenizer.GE, Tokenizer.GT, Tokenizer.EQUALS, Tokenizer.NE.
141     */

142
143     public boolean compare(int operator, Value other) throws XPathException {
144
145         if (operator==Tokenizer.EQUALS) return equals(other);
146         if (operator==Tokenizer.NE) return notEquals(other);
147
148         if (other instanceof NodeSetValue) {
149             return other.compare(inverse(operator), this);
150         }
151
152         return numericCompare(operator, this.asNumber(), other.asNumber());
153     }
154
155     /**
156     * Return the inverse of a relational operator, so that "a op b" can be
157     * rewritten as "b inverse(op) a"
158     */

159
160     protected final static int inverse(int operator) {
161         switch(operator) {
162             case Tokenizer.LT:
163                 return Tokenizer.GT;
164             case Tokenizer.LE:
165                 return Tokenizer.GE;
166             case Tokenizer.GT:
167                 return Tokenizer.LT;
168             case Tokenizer.GE:
169                 return Tokenizer.LE;
170             default:
171                 return operator;
172         }
173     }
174     
175
176     protected final boolean numericCompare(int operator, double x, double y) {
177         switch(operator) {
178             case Tokenizer.LT:
179                 return x < y;
180             case Tokenizer.LE:
181                 return x <= y;
182             case Tokenizer.GT:
183                 return x > y;
184             case Tokenizer.GE:
185                 return x >= y;
186             default:
187                 return false;
188         }
189     }
190
191     /**
192     * Perform a partial evaluation of the expression, by eliminating specified dependencies
193     * on the context.
194     * @param dependencies The dependencies to be removed
195     * @param context The context to be used for the partial evaluation
196     * @return a new expression that does not have any of the specified
197     * dependencies
198     */

199
200     public Expression reduce(int dependencies, Context context) {
201         return this;
202     }
203
204     /**
205     * Convert to Java object (for passing to external functions)
206     * @param target The class required by the external function
207     * @return an object of the target class
208     */

209     
210     public abstract Object JavaDoc convertToJava(Class JavaDoc target) throws XPathException;
211
212     /**
213     * Get conversion preference for this value to a Java class. A low result
214     * indicates higher preference.
215     */

216        
217     public abstract int conversionPreference(Class JavaDoc required);
218
219 }
220
221 //
222
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
223
// you may not use this file except in compliance with the License. You may obtain a copy of the
224
// License at http://www.mozilla.org/MPL/
225
//
226
// Software distributed under the License is distributed on an "AS IS" basis,
227
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
228
// See the License for the specific language governing rights and limitations under the License.
229
//
230
// The Original Code is: all this file.
231
//
232
// The Initial Developer of the Original Code is
233
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
234
//
235
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
236
//
237
// Contributor(s): none.
238
//
239
Popular Tags