Debian Codegraph @

net/sf/saxon/value/UntypedAtomicValue.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
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 * An Untyped Atomic value. This inherits from StringValue for implementation convenience, even
21 * though an untypedAtomic value is not a String in the data model type hierarchy.
22 */
23
24 public class UntypedAtomicValue extends StringValue {
25
26 public static final UntypedAtomicValue ZERO_LENGTH_UNTYPED =
27 new UntypedAtomicValue("");
28
29 // If the value is used once as a number, it's likely that it will be used
30 // repeatedly as a number, so we cache the result of conversion; similarly
31 // for other types
32
33 int cachedConversionType = -1;
34 ConversionResult cachedConversionResult = null;
35
36 /**
37 * Constructor
38 *
39 * @param value the String value. Null is taken as equivalent to "".
40 */
41
42 public UntypedAtomicValue(/*@Nullable*/ CharSequence value) {
43 this.value = value == null ? "" : value;
44 typeLabel = BuiltInAtomicType.UNTYPED_ATOMIC;
45 }
46
47 /**
48 * Create a copy of this atomic value, with a different type label
49 *
50 * @param typeLabel the type label of the new copy. The caller is responsible for checking that
51 * the value actually conforms to this type.
52 */
53
54 /*@NotNull*/
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 * Determine the primitive type of the value. This delivers the same answer as
65 * getItemType().getPrimitiveItemType(). The primitive types are
66 * the 19 primitive types of XML Schema, plus xs:integer, xs:dayTimeDuration and xs:yearMonthDuration,
67 * and xs:untypedAtomic. For external objects, the result is AnyAtomicType.
68 */
69
70 /*@NotNull*/
71 public BuiltInAtomicType getPrimitiveType() {
72 return BuiltInAtomicType.UNTYPED_ATOMIC;
73 }
74
75 /**
76 * Compare an untypedAtomic value with another value, using a given collator to perform
77 * any string comparisons. This works by converting the untypedAtomic value to the type
78 * of the other operand, which is the correct behavior for operators like "=" and "!=",
79 * but not for "eq" and "ne": in the latter case, the untypedAtomic value is converted
80 * to a string and this method is therefore not used.
81 *
82 * @param other the value to compare this value with
83 * @param collator the collation to be used for comparing strings
84 * @param context the XPath dynamic context
85 * @return -1 if the this value is less than the other, 0 if they are equal, +1 if this
86 * value is greater.
87 * @throws ClassCastException if the value cannot be cast to the type of the other operand
88 */
89
90 public int compareTo(/*@NotNull*/ AtomicValue other, /*@NotNull*/ StringCollator collator, /*@NotNull*/ 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 // This optimization avoids creating String objects for the purpose of the comparison
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 * Ask whether there is a known match key for comparing this value as
136 * a particular type. Note this is not used for string comparisons
137 */
138
139 public synchronized ConversionResult getConversionResultIfKnown(int atomicType) {
140 return cachedConversionType == atomicType ? cachedConversionResult : null;
141 }
142
143 /**
144 * Compute and cache a conversion result converting this value to a particular type
145 * <p>This method should not be used if the converter is namespace-sensitive, since the cached
146 * value may then be incorrect.</p>
147 *
148 * @param atomicType the fingerprint of the type to which the value is being convertered
149 * @param converter the converter used to convert to the target type
150 */
151
152 public synchronized ConversionResult obtainConversionResult(int atomicType, StringConverter converter) {
153 // The logic here doesn't take into account the possibility that the cached result was obtained
154 // using a different converter. The risk is small, but
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 * Cache the result of a conversion
166 */
167
168 public synchronized void setConversionResult(int atomicType, AtomicValue value) {
169 cachedConversionType = atomicType;
170 cachedConversionResult = value;
171 }
172
173 /**
174 * For displaying the value in error messages, prefix with "u" to highlight that it's
175 * untyped atomic
176 * @return a short depiction of the string for use in error messages
177 */
178
179 public String toShortString() {
180 return "u" + super.toShortString();
181 }
182
183
184 }
185