KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > value > BooleanValue


1 package net.sf.saxon.value;
2 import net.sf.saxon.Err;
3 import net.sf.saxon.ConversionContext;
4 import net.sf.saxon.expr.XPathContext;
5 import net.sf.saxon.trans.DynamicError;
6 import net.sf.saxon.trans.XPathException;
7 import net.sf.saxon.type.*;
8
9 /**
10  * A boolean XPath value
11  */

12
13 public final class BooleanValue extends AtomicValue implements Comparable JavaDoc {
14     private boolean value;
15
16     /**
17      * The boolean value TRUE
18      */

19     public static final BooleanValue TRUE = new BooleanValue(true);
20     /**
21      * The boolean value FALSE
22      */

23     public static final BooleanValue FALSE = new BooleanValue(false);
24
25     /**
26      * Private Constructor: create a boolean value. Only two instances of this class are
27      * ever created, one to represent true and one to represent false.
28      * @param value the initial value, true or false
29      */

30
31     private BooleanValue(boolean value) {
32         this.value = value;
33     }
34
35     /**
36      * Factory method: get a BooleanValue
37      *
38      * @param value true or false, to determine which boolean value is
39      * required
40      * @return the BooleanValue requested
41      */

42
43     public static BooleanValue get(boolean value) {
44         return (value ? TRUE : FALSE);
45     }
46
47     /**
48      * Convert a string to a boolean value, using the XML Schema rules (including
49      * whitespace trimming)
50      * @param s the input string
51      * @return the relevant BooleanValue if validation succeeds; or an ErrorValue if not.
52      */

53
54     public static AtomicValue fromString(CharSequence JavaDoc s) {
55         s = trimWhitespace(s).toString();
56         if (s.equals("true") || s.equals("1")) {
57             return TRUE;
58         } else if (s.equals("false") || s.equals("0")) {
59             return FALSE;
60         } else {
61             ValidationException err = new ValidationException(
62                                 "The string " + Err.wrap(s, Err.VALUE) + " cannot be cast to a boolean");
63             err.setErrorCode("FORG0001");
64             return new ValidationErrorValue(err);
65         }
66     }
67
68     /**
69      * Get the value
70      * @return true or false, the actual boolean value of this BooleanValue
71      */

72
73     public boolean getBooleanValue() {
74         return value;
75     }
76
77     /**
78      * Get the effective boolean value of this expression
79      *
80      * @param context dynamic evaluation context, not used in this
81      * implementation
82      * @return the boolean value
83      */

84     public boolean effectiveBooleanValue(XPathContext context) {
85         return value;
86     }
87
88     /**
89      * Convert to target data type
90      * @param requiredType an integer identifying the required atomic type
91      * @param conversion
92      * @return an AtomicValue, a value of the required type
93      */

94
95     public AtomicValue convertPrimitive(BuiltInAtomicType requiredType, boolean validate, ConversionContext conversion) {
96         switch(requiredType.getPrimitiveType()) {
97         case Type.BOOLEAN:
98         case Type.ATOMIC:
99         case Type.ITEM:
100             return this;
101         case Type.NUMBER:
102         case Type.INTEGER:
103             return new IntegerValue(value ? 1 : 0);
104         case Type.DECIMAL:
105         case Type.FLOAT:
106         case Type.DOUBLE:
107             return new IntegerValue(value ? 1 : 0).convertPrimitive(requiredType, validate, conversion);
108         case Type.STRING:
109             return new StringValue(getStringValueCS());
110         case Type.UNTYPED_ATOMIC:
111             return new UntypedAtomicValue(getStringValueCS());
112         default:
113             ValidationException err = new ValidationException("Cannot convert boolean to " +
114                                      requiredType.getDisplayName());
115             //err.setXPathContext(context);
116
err.setErrorCode("FORG0001");
117             return new ValidationErrorValue(err);
118         }
119     }
120
121     /**
122      * Convert to string
123      * @return "true" or "false"
124      */

125
126     public String JavaDoc getStringValue() {
127         return (value ? "true" : "false");
128     }
129
130     /**
131      * Determine the data type of the expression
132      * @return Type.BOOLEAN,
133      */

134
135     public ItemType getItemType() {
136         return Type.BOOLEAN_TYPE;
137     }
138
139     /**
140      * Convert to Java object (for passing to external functions)
141      *
142      * @param target the Java class to which conversion is required
143      * @exception XPathException if conversion is not possible or fails
144      * @return An object of the specified Java class
145      */

146
147     public Object JavaDoc convertToJava(Class JavaDoc target, XPathContext context) throws XPathException {
148         if (target==Object JavaDoc.class) {
149             return Boolean.valueOf(value);
150         } else if (target.isAssignableFrom(BooleanValue.class)) {
151             return this;
152         } else if (target==boolean.class) {
153             return Boolean.valueOf(value);
154         } else if (target==Boolean JavaDoc.class) {
155             return Boolean.valueOf(value);
156         } else if (target==String JavaDoc.class || target==CharSequence JavaDoc.class) {
157             return getStringValue();
158         } else if (target==double.class) {
159             return new Double JavaDoc((double)(value ? 1 : 0));
160         } else if (target==Double JavaDoc.class) {
161             return new Double JavaDoc((double)(value ? 1 : 0));
162         } else if (target==float.class) {
163             return new Float JavaDoc((float)(value ? 1 : 0));
164         } else if (target==Float JavaDoc.class) {
165             return new Float JavaDoc((float)(value ? 1 : 0));
166         } else if (target==long.class) {
167             return new Long JavaDoc((long)(value ? 1 : 0));
168         } else if (target==Long JavaDoc.class) {
169             return new Long JavaDoc((long)(value ? 1 : 0));
170         } else if (target==int.class) {
171             return new Integer JavaDoc(value ? 1 : 0);
172         } else if (target==Integer JavaDoc.class) {
173             return new Integer JavaDoc(value ? 1 : 0);
174         } else if (target==short.class) {
175             return new Short JavaDoc((short)(value ? 1 : 0));
176         } else if (target==Short JavaDoc.class) {
177             return new Short JavaDoc((short)(value ? 1 : 0));
178         } else if (target==byte.class) {
179             return new Byte JavaDoc((byte)(value ? 1 : 0));
180         } else if (target==Byte JavaDoc.class) {
181             return new Byte JavaDoc((byte)(value ? 1 : 0));
182         } else if (target==char.class) {
183             return new Character JavaDoc(value ? '1' : '0');
184         } else if (target==Character JavaDoc.class) {
185             return new Character JavaDoc(value ? '1' : '0');
186         } else {
187             Object JavaDoc o = super.convertToJava(target, context);
188             if (o == null) {
189                 DynamicError err = new DynamicError("Conversion of boolean to " + target.getName() +
190                         " is not supported");
191                 err.setXPathContext(context);
192                 err.setErrorCode("SAXON:0000");
193                 throw err;
194             }
195             return o;
196         }
197     }
198
199     /**
200      * Compare the value to another boolean value
201      *
202      * @throws ClassCastException if the other value is not a BooleanValue
203      * (the parameter is declared as Object to satisfy the Comparable
204      * interface)
205      * @param other The other boolean value
206      * @return -1 if this one is the lower, 0 if they are equal, +1 if this
207      * one is the higher. False is considered to be less than true.
208      */

209
210     public int compareTo(Object JavaDoc other) {
211         if (!(other instanceof BooleanValue)) {
212             throw new ClassCastException JavaDoc("Boolean values are not comparable to " + other.getClass());
213         }
214         if (this.value == ((BooleanValue)other).value) return 0;
215         if (this.value) return +1;
216         return -1;
217     }
218
219     /**
220      * Determine whether two boolean values are equal
221      *
222      * @param other the value to be compared to this value
223      * @return true if the other value is a boolean value and is equal to this
224      * value
225      */

226     public boolean equals(Object JavaDoc other) {
227         if (!(other instanceof BooleanValue)) {
228             throw new ClassCastException JavaDoc("Boolean values are not comparable to " + other.getClass());
229         }
230         return (this.value == ((BooleanValue)other).value);
231     }
232
233     /**
234      * Get a hash code for comparing two BooleanValues
235      *
236      * @return the hash code
237      */

238     public int hashCode() {
239         return (value ? 0 : 1);
240     }
241
242     /**
243      * Diagnostic display of this value as a string
244      * @return a string representation of this value: "true()" or "false()"
245      */

246     public String JavaDoc toString() {
247         return getStringValue() + "()";
248     }
249 }
250
251 //
252
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
253
// you may not use this file except in compliance with the License. You may obtain a copy of the
254
// License at http://www.mozilla.org/MPL/
255
//
256
// Software distributed under the License is distributed on an "AS IS" basis,
257
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
258
// See the License for the specific language governing rights and limitations under the License.
259
//
260
// The Original Code is: all this file.
261
//
262
// The Initial Developer of the Original Code is Michael H. Kay.
263
//
264
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
265
//
266
// Contributor(s): none.
267
//
268

269
Popular Tags