KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.icl.saxon.expr;
2 import com.icl.saxon.*;
3 import com.icl.saxon.functions.*;
4
5
6 /**
7 * An XPath value that encapsulates a Java object. Such a value can only be obtained by
8 * calling an extension function that returns it.
9 */

10
11 public class ObjectValue extends Value {
12     private Object JavaDoc value;
13
14     /**
15     * Constructor
16     * @value the object to be encapsulated
17     */

18
19     public ObjectValue(Object JavaDoc object) {
20         this.value = object;
21     }
22
23     /**
24     * Get the value as a String
25     * @return a String representation of the value
26     */

27
28     public String JavaDoc asString() {
29         return (value==null ? "" : value.toString());
30     }
31
32     /**
33     * Get the value as a number
34     * @return the numeric value
35     */

36
37     public double asNumber() {
38         return (value==null ? Double.NaN : Value.stringToNumber(value.toString()));
39     }
40
41     /**
42     * Convert the value to a boolean
43     * @return the boolean value
44     */

45
46     public boolean asBoolean() {
47         return (value==null ? false : value.toString().length() > 0);
48     }
49
50     /**
51     * Determine the data type of the expression
52     * @return Value.OBJECT
53     */

54
55     public int getDataType() {
56         return Value.OBJECT;
57     }
58
59     /**
60     * Get the encapsulated object
61     */

62
63     public Object JavaDoc getObject() {
64         return value;
65     }
66
67     /**
68     * Determine if two ObjectValues are equal
69     */

70
71     public boolean equals(ObjectValue other) {
72         return this.value.equals(other.value);
73     }
74
75     /**
76     * Get conversion preference for this value to a Java class. A low result
77     * indicates higher preference.
78     */

79        
80     public int conversionPreference(Class JavaDoc required) {
81         if (required==boolean.class) return Integer.MAX_VALUE; // don't know why
82
if (required==Boolean JavaDoc.class) return Integer.MAX_VALUE; // don't know why
83
if (required==byte.class) return 4;
84         if (required==Byte JavaDoc.class) return 5;
85         if (required==char.class) return 2;
86         if (required==Character JavaDoc.class) return 3;
87         if (required==double.class) return 4;
88         if (required==Double JavaDoc.class) return 5;
89         if (required==float.class) return 4;
90         if (required==Float JavaDoc.class) return 5;
91         if (required==int.class) return 4;
92         if (required==Integer JavaDoc.class) return 5;
93         if (required==long.class) return 4;
94         if (required==Long JavaDoc.class) return 5;
95         if (required==short.class) return 4;
96         if (required==Short JavaDoc.class) return 5;
97         if (required==String JavaDoc.class) return 1;
98         if (required==Object JavaDoc.class) return 8;
99         if (required==value.getClass()) return -1;
100             // this departs from the draft spec, but is useful to discriminate a method
101
// for the exact class from one for a superclass.
102
if (required.isAssignableFrom(value.getClass())) return 0;
103         return Integer.MAX_VALUE;
104     }
105
106     /**
107     * Convert to Java object (for passing to external functions)
108     */

109     
110     public Object JavaDoc convertToJava(Class JavaDoc target) throws XPathException {
111
112         if (value==null) return null;
113         
114         if (target.isAssignableFrom(value.getClass())) {
115             return value;
116         } else if (target==Value.class || target==ObjectValue.class) {
117             return this;
118         } else if (target==boolean.class) {
119             return new Boolean JavaDoc(asBoolean()); // technically not allowed
120
} else if (target==Boolean JavaDoc.class) {
121             return new Boolean JavaDoc(asBoolean()); // technically not allowed
122
} else if (target==String JavaDoc.class) {
123             return asString();
124         } else if (target==double.class) {
125             return new Double JavaDoc(asNumber());
126         } else if (target==Double JavaDoc.class) {
127             return new Double JavaDoc(asNumber());
128         } else if (target==float.class) {
129             return new Float JavaDoc(asNumber());
130         } else if (target==Float JavaDoc.class) {
131             return new Float JavaDoc(asNumber());
132         } else if (target==long.class) {
133             return new Long JavaDoc((long)asNumber());
134         } else if (target==Long JavaDoc.class) {
135             return new Long JavaDoc((long)asNumber());
136         } else if (target==int.class) {
137             return new Integer JavaDoc((int)asNumber());
138         } else if (target==Integer JavaDoc.class) {
139             return new Integer JavaDoc((int)asNumber());
140         } else if (target==short.class) {
141             return new Short JavaDoc((short)asNumber());
142         } else if (target==Short JavaDoc.class) {
143             return new Short JavaDoc((short)asNumber());
144         } else if (target==byte.class) {
145             return new Byte JavaDoc((byte)asNumber());
146         } else if (target==Byte JavaDoc.class) {
147             return new Byte JavaDoc((byte)asNumber());
148         } else if (target==char.class || target==Character JavaDoc.class) {
149             String JavaDoc s = asString();
150             if (s.length()==1) {
151                 return new Character JavaDoc(s.charAt(0));
152             } else {
153                 throw new XPathException("Cannot convert string to Java char unless length is 1");
154             }
155         } else {
156             throw new XPathException("Conversion of external object to " + target.getName() +
157                         " is not supported");
158         }
159     }
160
161     /**
162     * Diagnostic print of expression structure
163     */

164     
165     public void display(int level) {
166         System.err.println(indent(level) + "** external object **" );
167     }
168
169 }
170
171 //
172
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
173
// you may not use this file except in compliance with the License. You may obtain a copy of the
174
// License at http://www.mozilla.org/MPL/
175
//
176
// Software distributed under the License is distributed on an "AS IS" basis,
177
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
178
// See the License for the specific language governing rights and limitations under the License.
179
//
180
// The Original Code is: all this file.
181
//
182
// The Initial Developer of the Original Code is
183
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
184
//
185
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
186
//
187
// Contributor(s): none.
188
//
189

190
Popular Tags