KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.value;
2 import net.sf.saxon.expr.XPathContext;
3 import net.sf.saxon.trans.DynamicError;
4 import net.sf.saxon.trans.XPathException;
5 import net.sf.saxon.type.BuiltInAtomicType;
6 import net.sf.saxon.type.ExternalObjectType;
7 import net.sf.saxon.type.ItemType;
8 import net.sf.saxon.type.Type;
9 import net.sf.saxon.ConversionContext;
10
11
12 /**
13 * An XPath value that encapsulates a Java object. Such a value can only be obtained by
14 * calling an extension function that returns it.
15 */

16
17 public class ObjectValue extends AtomicValue {
18     private Object JavaDoc value;
19
20     /**
21      * Default constructor for use in subclasses
22      */

23
24     public ObjectValue() {}
25
26     /**
27     * Constructor
28     * @param object the object to be encapsulated
29     */

30
31     public ObjectValue(Object JavaDoc object) {
32         this.value = object;
33     }
34
35     /**
36      * Set the value in this object value
37      */

38
39     public void setValue(Object JavaDoc value) {
40         this.value = value;
41     }
42
43     /**
44     * Convert to target data type
45     */

46
47     public AtomicValue convertPrimitive(BuiltInAtomicType requiredType, boolean validate, ConversionContext conversion) {
48         switch(requiredType.getPrimitiveType()) {
49         case Type.ATOMIC:
50         case Type.OBJECT:
51         case Type.ITEM:
52             return this;
53         case Type.BOOLEAN:
54             return BooleanValue.get(
55                     (value==null ? false : value.toString().length() > 0));
56         case Type.STRING:
57             return new StringValue(getStringValue());
58         case Type.UNTYPED_ATOMIC:
59             return new UntypedAtomicValue(getStringValue());
60         default:
61             return new StringValue(getStringValue()).convertPrimitive(requiredType, validate, conversion);
62         }
63     }
64
65     /**
66     * Get the value as a String
67     * @return a String representation of the value
68     */

69
70     public String JavaDoc getStringValue() {
71         return (value==null ? "" : value.toString());
72     }
73
74     /**
75     * Determine the data type of the expression
76     * @return Type.OBJECT
77     */

78
79     public ItemType getItemType() {
80         return new ExternalObjectType(value.getClass());
81     }
82
83     /**
84     * Get the encapsulated object
85     */

86
87     public Object JavaDoc getObject() {
88         return value;
89     }
90
91     /**
92     * Determine if two ObjectValues are equal
93     * @throws ClassCastException if they are not comparable
94     */

95
96     public boolean equals(Object JavaDoc other) {
97         return this.value.equals(((ObjectValue)other).value);
98     }
99
100     public int hashCode() {
101         return value.hashCode();
102     }
103
104     /**
105     * Convert to Java object (for passing to external functions)
106     */

107
108     public Object JavaDoc convertToJava(Class JavaDoc target, XPathContext context) throws XPathException {
109
110         if (value==null) return null;
111
112         if (target.isAssignableFrom(value.getClass())) {
113             return value;
114         } else if (target==Value.class || target==ObjectValue.class) {
115             return this;
116         } else if (target==boolean.class || target==Boolean JavaDoc.class) {
117             BooleanValue bval = (BooleanValue)convert(Type.BOOLEAN, context);
118             return Boolean.valueOf(bval.getBooleanValue());
119         } else if (target==String JavaDoc.class || target==CharSequence JavaDoc.class) {
120             return getStringValue();
121         } else if (target==double.class || target==Double JavaDoc.class) {
122             DoubleValue bval = (DoubleValue)convert(Type.DOUBLE, context);
123             return new Double JavaDoc(bval.getDoubleValue());
124         } else if (target==float.class || target==Float JavaDoc.class) {
125             DoubleValue bval = (DoubleValue)convert(Type.FLOAT, context);
126             return new Float JavaDoc(bval.getDoubleValue());
127         } else if (target==long.class || target==Long JavaDoc.class) {
128             IntegerValue bval = (IntegerValue)convert(Type.INTEGER, context);
129             return new Long JavaDoc(bval.longValue());
130         } else if (target==int.class || target==Integer JavaDoc.class) {
131             IntegerValue bval = (IntegerValue)convert(Type.INTEGER, context);
132             return new Integer JavaDoc((int)bval.longValue());
133         } else if (target==short.class || target==Short JavaDoc.class) {
134             IntegerValue bval = (IntegerValue)convert(Type.INTEGER, context);
135             return new Short JavaDoc((short)bval.longValue());
136         } else if (target==byte.class || target==Byte JavaDoc.class) {
137             IntegerValue bval = (IntegerValue)convert(Type.INTEGER, context);
138             return new Byte JavaDoc((byte)bval.longValue());
139         } else if (target==char.class || target==Character JavaDoc.class) {
140             String JavaDoc s = getStringValue();
141             if (s.length()==1) {
142                 return new Character JavaDoc(s.charAt(0));
143             } else {
144                 throw new DynamicError("Cannot convert string to Java char unless length is 1");
145             }
146         } else {
147             throw new DynamicError("Conversion of external object to " + target.getName() +
148                         " is not supported");
149         }
150     }
151
152 }
153
154 //
155
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
156
// you may not use this file except in compliance with the License. You may obtain a copy of the
157
// License at http://www.mozilla.org/MPL/
158
//
159
// Software distributed under the License is distributed on an "AS IS" basis,
160
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
161
// See the License for the specific language governing rights and limitations under the License.
162
//
163
// The Original Code is: all this file.
164
//
165
// The Initial Developer of the Original Code is Michael H. Kay.
166
//
167
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
168
//
169
// Contributor(s): none.
170
//
171

172
Popular Tags