| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.value; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.expr.XPathContext; |
| 12 |
import net.sf.saxon.expr.sort.CodepointCollator; |
| 13 |
import net.sf.saxon.expr.sort.ComparisonException; |
| 14 |
import net.sf.saxon.lib.StringCollator; |
| 15 |
import net.sf.saxon.om.StandardNames; |
| 16 |
import net.sf.saxon.trans.XPathException; |
| 17 |
import net.sf.saxon.type.*; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public class UntypedAtomicValue extends StringValue { |
| 25 |
|
| 26 |
public static final UntypedAtomicValue ZERO_LENGTH_UNTYPED = |
| 27 |
new UntypedAtomicValue(""); |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
int cachedConversionType = -1; |
| 34 |
ConversionResult cachedConversionResult = null; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public UntypedAtomicValue( CharSequence value) { |
| 43 |
this.value = value == null ? "" : value; |
| 44 |
typeLabel = BuiltInAtomicType.UNTYPED_ATOMIC; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public AtomicValue copyAsSubType(AtomicType typeLabel) { |
| 56 |
UntypedAtomicValue v = new UntypedAtomicValue(value); |
| 57 |
v.cachedConversionResult = cachedConversionResult; |
| 58 |
v.cachedConversionType = cachedConversionType; |
| 59 |
v.typeLabel = typeLabel; |
| 60 |
return v; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public BuiltInAtomicType getPrimitiveType() { |
| 72 |
return BuiltInAtomicType.UNTYPED_ATOMIC; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public int compareTo( AtomicValue other, StringCollator collator, XPathContext context) { |
| 91 |
if (other instanceof NumericValue) { |
| 92 |
DoubleValue doubleValue = null; |
| 93 |
if (cachedConversionType == StandardNames.XS_DOUBLE) { |
| 94 |
try { |
| 95 |
doubleValue = (DoubleValue) cachedConversionResult.asAtomic(); |
| 96 |
} catch (ValidationException e) { |
| 97 |
throw new NumberFormatException(e.getMessage()); |
| 98 |
} |
| 99 |
} |
| 100 |
if (doubleValue == null) { |
| 101 |
try { |
| 102 |
doubleValue = new DoubleValue(context.getConfiguration().getConversionRules() |
| 103 |
.getStringToDoubleConverter().stringToNumber(value)); |
| 104 |
} catch (NumberFormatException e) { |
| 105 |
XPathException xe = new XPathException("Cannot convert untyped atomic value '" + getStringValue() |
| 106 |
+ "' to a double ", "FORG0001"); |
| 107 |
throw new ComparisonException(xe); |
| 108 |
} |
| 109 |
cachedConversionType = StandardNames.XS_DOUBLE; |
| 110 |
cachedConversionResult = doubleValue; |
| 111 |
} |
| 112 |
return doubleValue.compareTo((NumericValue)other); |
| 113 |
} else if (other instanceof StringValue) { |
| 114 |
if (collator instanceof CodepointCollator) { |
| 115 |
|
| 116 |
return CodepointCollator.compareCS(getStringValueCS(), other.getStringValueCS()); |
| 117 |
} else { |
| 118 |
return collator.compareStrings(getStringValue(), other.getStringValue()); |
| 119 |
} |
| 120 |
} else { |
| 121 |
final Configuration config = context.getConfiguration(); |
| 122 |
Converter converter = config.getConversionRules().getConverter(BuiltInAtomicType.UNTYPED_ATOMIC, other.getItemType()); |
| 123 |
ConversionResult result = converter.convert(this); |
| 124 |
if (result instanceof ValidationFailure) { |
| 125 |
XPathException xe = new XPathException("Cannot convert untyped atomic value '" + getStringValue() |
| 126 |
+ "' to type " + other.getItemType(), "FORG0001"); |
| 127 |
throw new ComparisonException(xe); |
| 128 |
} |
| 129 |
return ((Comparable) result).compareTo(other); |
| 130 |
|
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public synchronized ConversionResult getConversionResultIfKnown(int atomicType) { |
| 140 |
return cachedConversionType == atomicType ? cachedConversionResult : null; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
public synchronized ConversionResult obtainConversionResult(int atomicType, StringConverter converter) { |
| 153 |
|
| 154 |
|
| 155 |
ConversionResult knownResult = getConversionResultIfKnown(atomicType); |
| 156 |
if (knownResult == null) { |
| 157 |
cachedConversionType = atomicType; |
| 158 |
return cachedConversionResult = converter.convertString(value); |
| 159 |
} else { |
| 160 |
return knownResult; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
public synchronized void setConversionResult(int atomicType, AtomicValue value) { |
| 169 |
cachedConversionType = atomicType; |
| 170 |
cachedConversionResult = value; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
public String toShortString() { |
| 180 |
return "u" + super.toShortString(); |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
} |
| 185 |
|