1 package com.icl.saxon.expr; 2 import com.icl.saxon.*; 3 import com.icl.saxon.functions.*; 4 5 6 10 11 public class ObjectValue extends Value { 12 private Object value; 13 14 18 19 public ObjectValue(Object object) { 20 this.value = object; 21 } 22 23 27 28 public String asString() { 29 return (value==null ? "" : value.toString()); 30 } 31 32 36 37 public double asNumber() { 38 return (value==null ? Double.NaN : Value.stringToNumber(value.toString())); 39 } 40 41 45 46 public boolean asBoolean() { 47 return (value==null ? false : value.toString().length() > 0); 48 } 49 50 54 55 public int getDataType() { 56 return Value.OBJECT; 57 } 58 59 62 63 public Object getObject() { 64 return value; 65 } 66 67 70 71 public boolean equals(ObjectValue other) { 72 return this.value.equals(other.value); 73 } 74 75 79 80 public int conversionPreference(Class required) { 81 if (required==boolean.class) return Integer.MAX_VALUE; if (required==Boolean .class) return Integer.MAX_VALUE; if (required==byte.class) return 4; 84 if (required==Byte .class) return 5; 85 if (required==char.class) return 2; 86 if (required==Character .class) return 3; 87 if (required==double.class) return 4; 88 if (required==Double .class) return 5; 89 if (required==float.class) return 4; 90 if (required==Float .class) return 5; 91 if (required==int.class) return 4; 92 if (required==Integer .class) return 5; 93 if (required==long.class) return 4; 94 if (required==Long .class) return 5; 95 if (required==short.class) return 4; 96 if (required==Short .class) return 5; 97 if (required==String .class) return 1; 98 if (required==Object .class) return 8; 99 if (required==value.getClass()) return -1; 100 if (required.isAssignableFrom(value.getClass())) return 0; 103 return Integer.MAX_VALUE; 104 } 105 106 109 110 public Object convertToJava(Class 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 (asBoolean()); } else if (target==Boolean .class) { 121 return new Boolean (asBoolean()); } else if (target==String .class) { 123 return asString(); 124 } else if (target==double.class) { 125 return new Double (asNumber()); 126 } else if (target==Double .class) { 127 return new Double (asNumber()); 128 } else if (target==float.class) { 129 return new Float (asNumber()); 130 } else if (target==Float .class) { 131 return new Float (asNumber()); 132 } else if (target==long.class) { 133 return new Long ((long)asNumber()); 134 } else if (target==Long .class) { 135 return new Long ((long)asNumber()); 136 } else if (target==int.class) { 137 return new Integer ((int)asNumber()); 138 } else if (target==Integer .class) { 139 return new Integer ((int)asNumber()); 140 } else if (target==short.class) { 141 return new Short ((short)asNumber()); 142 } else if (target==Short .class) { 143 return new Short ((short)asNumber()); 144 } else if (target==byte.class) { 145 return new Byte ((byte)asNumber()); 146 } else if (target==Byte .class) { 147 return new Byte ((byte)asNumber()); 148 } else if (target==char.class || target==Character .class) { 149 String s = asString(); 150 if (s.length()==1) { 151 return new Character (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 164 165 public void display(int level) { 166 System.err.println(indent(level) + "** external object **" ); 167 } 168 169 } 170 171 190 | Popular Tags |