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 16 17 public class ObjectValue extends AtomicValue { 18 private Object value; 19 20 23 24 public ObjectValue() {} 25 26 30 31 public ObjectValue(Object object) { 32 this.value = object; 33 } 34 35 38 39 public void setValue(Object value) { 40 this.value = value; 41 } 42 43 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 69 70 public String getStringValue() { 71 return (value==null ? "" : value.toString()); 72 } 73 74 78 79 public ItemType getItemType() { 80 return new ExternalObjectType(value.getClass()); 81 } 82 83 86 87 public Object getObject() { 88 return value; 89 } 90 91 95 96 public boolean equals(Object other) { 97 return this.value.equals(((ObjectValue)other).value); 98 } 99 100 public int hashCode() { 101 return value.hashCode(); 102 } 103 104 107 108 public Object convertToJava(Class 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 .class) { 117 BooleanValue bval = (BooleanValue)convert(Type.BOOLEAN, context); 118 return Boolean.valueOf(bval.getBooleanValue()); 119 } else if (target==String .class || target==CharSequence .class) { 120 return getStringValue(); 121 } else if (target==double.class || target==Double .class) { 122 DoubleValue bval = (DoubleValue)convert(Type.DOUBLE, context); 123 return new Double (bval.getDoubleValue()); 124 } else if (target==float.class || target==Float .class) { 125 DoubleValue bval = (DoubleValue)convert(Type.FLOAT, context); 126 return new Float (bval.getDoubleValue()); 127 } else if (target==long.class || target==Long .class) { 128 IntegerValue bval = (IntegerValue)convert(Type.INTEGER, context); 129 return new Long (bval.longValue()); 130 } else if (target==int.class || target==Integer .class) { 131 IntegerValue bval = (IntegerValue)convert(Type.INTEGER, context); 132 return new Integer ((int)bval.longValue()); 133 } else if (target==short.class || target==Short .class) { 134 IntegerValue bval = (IntegerValue)convert(Type.INTEGER, context); 135 return new Short ((short)bval.longValue()); 136 } else if (target==byte.class || target==Byte .class) { 137 IntegerValue bval = (IntegerValue)convert(Type.INTEGER, context); 138 return new Byte ((byte)bval.longValue()); 139 } else if (target==char.class || target==Character .class) { 140 String s = getStringValue(); 141 if (s.length()==1) { 142 return new Character (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 172 | Popular Tags |