1 package net.sf.saxon.value; 2 import net.sf.saxon.trans.XPathException; 3 import net.sf.saxon.type.AtomicType; 4 import net.sf.saxon.type.BuiltInAtomicType; 5 import net.sf.saxon.type.ItemType; 6 import net.sf.saxon.type.Type; 7 import net.sf.saxon.ConversionContext; 8 import net.sf.saxon.sort.CodepointCollator; 9 10 import java.util.Comparator ; 11 12 16 17 public class UntypedAtomicValue extends StringValue { 18 19 public static final UntypedAtomicValue ZERO_LENGTH_UNTYPED = 20 new UntypedAtomicValue(""); 21 22 25 DoubleValue doubleValue = null; 26 27 31 32 public UntypedAtomicValue(CharSequence value) { 33 this.value = (value==null ? "" : value); 34 } 35 36 40 41 public ItemType getItemType() { 42 return Type.UNTYPED_ATOMIC_TYPE; 43 } 44 45 48 49 public AtomicValue convertPrimitive(BuiltInAtomicType requiredType, boolean validate, ConversionContext conversion) { 50 int req = requiredType.getFingerprint(); 51 if (req==Type.STRING) { 52 if (value.length() == 0) { 53 return StringValue.EMPTY_STRING; 55 } else { 56 return new StringValue(value); 57 } 58 } else if (req==Type.DOUBLE || req==Type.NUMBER) { 59 if (doubleValue==null) { 61 AtomicValue v = super.convertPrimitive(requiredType, validate, conversion); 62 if (v instanceof DoubleValue) { 63 doubleValue = (DoubleValue)v; 65 } 66 return v; 67 } 68 return doubleValue; 69 } else { 70 return super.convertPrimitive(requiredType, validate, conversion); 71 } 72 } 73 74 83 84 public int compareTo(Object other, Comparator collator, ConversionContext conversion) { 85 if (other instanceof NumericValue) { 86 if (doubleValue == null) { 87 try { 88 doubleValue = (DoubleValue)convert(Type.DOUBLE, null); 89 } catch (XPathException err) { 90 throw new ClassCastException ("Cannot convert untyped value " + 91 '\"' + getStringValueCS() + "\" to a double"); 92 } 93 } 94 return doubleValue.compareTo(other); 95 } else if (other instanceof StringValue) { 96 if (collator instanceof CodepointCollator) { 97 return ((CodepointCollator)collator).compareCS(getStringValueCS(), 99 ((StringValue)other).getStringValueCS()); 100 } else { 101 return collator.compare(getStringValue(), ((StringValue)other).getStringValue()); 102 } 103 } else if (other instanceof AtomicValue) { 104 AtomicValue conv = 105 convert((AtomicType)((Value)other).getItemType(), conversion, true); 106 if (conv instanceof ValidationErrorValue) { 107 throw new ClassCastException ("Cannot convert untyped atomic value '" + getStringValue() 108 + "' to type " + ((Value)other).getItemType()); 109 } 110 if (!(conv instanceof Comparable )) { 111 throw new ClassCastException ("Type " + ((Value)other).getItemType() + " is not ordered"); 112 } 113 return ((Comparable )conv).compareTo(other); 114 115 } else { 116 return collator.compare(getStringValue(), other.toString()); 118 } 119 } 120 121 128 129 public boolean schemaEquals(Value obj) { 130 throw new UnsupportedOperationException ("schemaEquals() cannot be applied to untyped atomic values"); 131 } 132 133 } 134 135 153 | Popular Tags |