KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.icl.saxon.expr;
2 import com.icl.saxon.*;
3
4
5 /**
6 * A boolean XPath value
7 */

8
9 public final class BooleanValue extends Value {
10     private boolean value;
11
12     /**
13     * Constructor: create a boolean value
14     * @param value the initial value, true or false
15     */

16
17     public BooleanValue(boolean value) {
18         this.value = value;
19     }
20
21     /**
22     * Convert to string
23     * @return "true" or "false"
24     */

25
26     public String JavaDoc asString() {
27         return (value ? "true" : "false");
28     }
29
30     /**
31     * Convert to number
32     * @return 1 for true, 0 for false
33     */

34
35     public double asNumber() {
36         return (value ? 1 : 0);
37     }
38
39     /**
40     * Convert to boolean (null operation)
41     * @return the value
42     */

43
44     public boolean asBoolean() {
45         return value;
46     }
47     
48
49     /**
50     * Determine the data type of the exprssion
51     * @return Value.BOOLEAN,
52     */

53
54     public int getDataType() {
55         return Value.BOOLEAN;
56     }
57
58     /**
59     * Get conversion preference for this value to a Java class. A low result
60     * indicates higher preference.
61     */

62        
63     public int conversionPreference(Class JavaDoc required) {
64         
65         if (required==Object JavaDoc.class) return 50;
66         if (required.isAssignableFrom(BooleanValue.class)) return 0;
67                 
68         if (required==boolean.class) return 0;
69         if (required==Boolean JavaDoc.class) return 0;
70         if (required==byte.class) return 3;
71         if (required==Byte JavaDoc.class) return 4;
72         if (required==char.class) return Integer.MAX_VALUE;
73         if (required==Character JavaDoc.class) return Integer.MAX_VALUE;
74         if (required==double.class) return 3;
75         if (required==Double JavaDoc.class) return 4;
76         if (required==float.class) return 3;
77         if (required==Float JavaDoc.class) return 4;
78         if (required==int.class) return 3;
79         if (required==Integer JavaDoc.class) return 4;
80         if (required==long.class) return 3;
81         if (required==Long JavaDoc.class) return 4;
82         if (required==short.class) return 3;
83         if (required==Short JavaDoc.class) return 4;
84         if (required==String JavaDoc.class) return 2;
85
86         return Integer.MAX_VALUE;
87     }
88
89     /**
90     * Convert to Java object (for passing to external functions)
91     */

92     
93     public Object JavaDoc convertToJava(Class JavaDoc target) throws XPathException {
94         if (target==Object JavaDoc.class) {
95             return new Boolean JavaDoc(value);
96         } else if (target.isAssignableFrom(BooleanValue.class)) {
97             return this;
98         } else if (target==boolean.class) {
99             return new Boolean JavaDoc(value);
100         } else if (target==Boolean JavaDoc.class) {
101             return new Boolean JavaDoc(value);
102         } else if (target==Object JavaDoc.class) {
103             return new Boolean JavaDoc(value);
104         } else if (target==String JavaDoc.class) {
105             return asString();
106         } else if (target==double.class) {
107             return new Double JavaDoc(asNumber());
108         } else if (target==Double JavaDoc.class) {
109             return new Double JavaDoc(asNumber());
110         } else if (target==float.class) {
111             return new Float JavaDoc(asNumber());
112         } else if (target==Float JavaDoc.class) {
113             return new Float JavaDoc(asNumber());
114         } else if (target==long.class) {
115             return new Long JavaDoc((value?1:0));
116         } else if (target==Long JavaDoc.class) {
117             return new Long JavaDoc((value?1:0));
118         } else if (target==int.class) {
119             return new Integer JavaDoc((value?1:0));
120         } else if (target==Integer JavaDoc.class) {
121             return new Integer JavaDoc((value?1:0));
122         } else if (target==short.class) {
123             return new Short JavaDoc((short)(value?1:0));
124         } else if (target==Short JavaDoc.class) {
125             return new Short JavaDoc((short)(value?1:0));
126         } else if (target==byte.class) {
127             return new Byte JavaDoc((byte)(value?1:0));
128         } else if (target==Byte JavaDoc.class) {
129             return new Byte JavaDoc((byte)(value?1:0));
130         } else {
131             throw new XPathException("Conversion of boolean to " + target.getName() +
132                         " is not supported");
133         }
134     }
135
136     /**
137     * Diagnostic print of expression structure
138     */

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

164
Popular Tags