Debian Codegraph @

net/sf/saxon/s9api/XdmAtomicValue.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.s9api;
9
10 import net.sf.saxon.expr.sort.AtomicMatchKey;
11 import net.sf.saxon.trans.XPathException;
12 import net.sf.saxon.type.*;
13 import net.sf.saxon.value.*;
14
15 import java.math.BigDecimal;
16 import java.math.BigInteger;
17 import java.net.URI;
18
19 /**
20 * The class XdmAtomicValue represents an item in an XPath 2.0 sequence that is an atomic value.
21 * The value may belong to any of the 19 primitive types defined in XML Schema, or to a type
22 * derived from these primitive types, or the XPath 2.0 type xs:untypedAtomic. The type may
23 * be either a built-in type or a user-defined type.
24 * <p>An <code>XdmAtomicValue</code> is immutable.</p>
25 */
26 public class XdmAtomicValue extends XdmItem {
27
28 private XdmAtomicValue(){};
29
30 protected XdmAtomicValue(AtomicValue value, boolean flag) {
31 setValue(value);
32 }
33
34 /**
35 * Create an <tt>xs:boolean</tt> atomic value
36 *
37 * @param value the boolean value, true or false
38 */
39
40 public XdmAtomicValue(boolean value) {
41 this(BooleanValue.get(value), true);
42 }
43
44 /**
45 * Create an <tt>xs:long</tt> atomic value
46 *
47 * @param value the <tt>xs:integer</tt> value, as a long
48 */
49
50 public XdmAtomicValue(long value) {
51 this(Int64Value.makeDerived(value, BuiltInAtomicType.LONG), true);
52 }
53
54 /**
55 * Create an <tt>xs:int</tt> atomic value
56 *
57 * @param value the <tt>xs:integer</tt> value, as a long
58 */
59
60 public XdmAtomicValue(int value) {
61 this(Int64Value.makeDerived(value, BuiltInAtomicType.INT), true);
62 }
63
64 /**
65 * Create an <tt>xs:short</tt> atomic value
66 *
67 * @param value the <tt>xs:integer</tt> value, as a long
68 */
69
70 public XdmAtomicValue(short value) {
71 this(Int64Value.makeDerived(value, BuiltInAtomicType.SHORT), true);
72 }
73
74 /**
75 * Create an <tt>xs:short</tt> atomic value
76 *
77 * @param value the <tt>xs:integer</tt> value, as a long
78 */
79
80 public XdmAtomicValue(byte value) {
81 this(Int64Value.makeDerived(value, BuiltInAtomicType.BYTE), true);
82 }
83
84 /**
85 * Create an <tt>xs:decimal</tt> atomic value
86 *
87 * @param value the <tt>xs:decimal</tt> value, as a BigDecimal
88 */
89
90 public XdmAtomicValue(BigDecimal value) {
91 this(new BigDecimalValue(value), true);
92 }
93
94 /**
95 * Create an <tt>xs:double</tt> atomic value
96 *
97 * @param value the <tt>xs:double</tt> value, as a double
98 */
99
100 public XdmAtomicValue(double value) {
101 this(new DoubleValue(value), true);
102 }
103
104 /**
105 * Create an <tt>xs:float</tt> atomic value
106 *
107 * @param value the <tt>xs:float</tt> value, as a float
108 */
109
110 public XdmAtomicValue(float value) {
111 this(new FloatValue(value), true);
112 }
113
114 /**
115 * Create an <tt>xs:string</tt> atomic value
116 *
117 * @param value the <tt>xs:string</tt> value, as a string
118 */
119
120 public XdmAtomicValue(String value) {
121 this(new StringValue(value), true);
122 }
123
124 /**
125 * Create an <tt>xs:anyURI</tt> atomic value
126 *
127 * @param value the <tt>xs:anyURI</tt> value, as a URI
128 */
129
130 public XdmAtomicValue(URI value) {
131 this(new AnyURIValue(value.toString()), true);
132 }
133
134 /**
135 * Create an <tt>xs:QName</tt> atomic value
136 *
137 * @param value the <tt>xs:QName</tt> value, as a QName
138 */
139
140 public XdmAtomicValue(QName value) {
141 this(new QNameValue(value.getStructuredQName(), BuiltInAtomicType.QNAME), true);
142 }
143
144 /**
145 * Construct an atomic value given its lexical representation and the name of the required
146 * built-in atomic type.
147 * <p>This method cannot be used to construct values that are namespace-sensitive (QNames and Notations)</p>
148 *
149 * @param lexicalForm the value in the lexical space of the target data type. More strictly, the input
150 * value before the actions of the whitespace facet for the target data type are applied.
151 * @param type the required atomic type. This must either be one of the built-in
152 * atomic types defined in XML Schema, or a user-defined type whose definition appears
153 * in a schema that is known to the Processor. It must not be an abstract type.
154 * @throws SaxonApiException if the type is unknown, or is not atomic, or is namespace-sensitive;
155 * or if the value supplied in <tt>lexicalForm</tt> is not in the lexical space of the specified atomic
156 * type.
157 */
158
159 public XdmAtomicValue(String lexicalForm, ItemType type) throws SaxonApiException {
160 net.sf.saxon.type.ItemType it = type.getUnderlyingItemType();
161 if (!it.isPlainType()) {
162 throw new SaxonApiException("Requested type is not atomic");
163 }
164 if (((AtomicType) it).isAbstract()) {
165 throw new SaxonApiException("Requested type is an abstract type");
166 }
167 if (((AtomicType) it).isNamespaceSensitive()) {
168 throw new SaxonApiException("Requested type is namespace-sensitive");
169 }
170 try {
171 StringConverter converter = ((AtomicType)it).getStringConverter(type.getConversionRules());
172 setValue(converter.convertString(lexicalForm).asAtomic());
173 } catch (ValidationException e) {
174 throw new SaxonApiException(e);
175 }
176 }
177
178
179
180 /**
181 * Attempt conversion from an arbitrary Java object
182 * @param value the value to convert from. This must either be an instance of {@link AtomicValue}
183 * or {@link QName},
184 * or one of the basic Java types Boolean, Integer, Long, Short, Character, Byte,
185 * String, Double, Float, BigDecimal, BigInteger, or URI
186 * @return the result of the conversion if successful
187 * @throws IllegalArgumentException if conversion from this kind of Object is not supported
188 */
189
190 public static XdmAtomicValue makeAtomicValue(Object value) {
191 if (value instanceof AtomicValue) {
192 return new XdmAtomicValue((AtomicValue)value, true);
193 } else if (value instanceof Boolean) {
194 return new XdmAtomicValue((Boolean)value);
195 } else if (value instanceof Integer) {
196 return new XdmAtomicValue((Integer) value);
197 } else if (value instanceof Long) {
198 return new XdmAtomicValue((Long) value);
199 } else if (value instanceof Short) {
200 return new XdmAtomicValue((Short) value);
201 } else if (value instanceof Character) {
202 return new XdmAtomicValue((Character) value);
203 } else if (value instanceof Byte) {
204 return new XdmAtomicValue((Byte) value);
205 } else if (value instanceof String) {
206 return new XdmAtomicValue((String) value);
207 } else if (value instanceof Double) {
208 return new XdmAtomicValue((Double) value);
209 } else if (value instanceof Float) {
210 return new XdmAtomicValue((Float) value);
211 } else if (value instanceof BigDecimal) {
212 return new XdmAtomicValue((BigDecimal) value);
213 } else if (value instanceof BigInteger) {
214 return new XdmAtomicValue(IntegerValue.makeIntegerValue((BigInteger) value), true);
215 } else if (value instanceof URI) {
216 return new XdmAtomicValue((URI) value);
217 } else if (value instanceof QName) {
218 return new XdmAtomicValue((QName) value);
219 } if(value instanceof XdmAtomicValue){
220 return (XdmAtomicValue)value;
221 }else {
222 throw new IllegalArgumentException(value.toString());
223 }
224 }
225
226 /**
227 * Get the underlying implementation object representing the value. This method allows
228 * access to lower-level Saxon functionality, including classes and methods that offer
229 * no guarantee of stability across releases.
230 *
231 * @return the underlying implementation object representing the value
232 * @since 9.8 (previously inherited from XdmValue which returns a Sequence)
233 */
234 @Override
235 public AtomicValue getUnderlyingValue() {
236 return (AtomicValue)super.getUnderlyingValue();
237 }
238
239 /**
240 * Get the result of converting the atomic value to a string. This has the same
241 * effect as the XPath string() function.
242 */
243
244 public String toString() {
245 return getStringValue();
246 }
247
248 /**
249 * Get the primitive type of this atomic value, as a QName. The primitive types for this purpose are
250 * the 19 primitive types of XML Schema, plus xs:integer, xs:dayTimeDuration and xs:yearMonthDuration,
251 * and xs:untypedAtomic. For external objects, the result is xs:anyAtomicType.
252 *
253 * @return a QName naming the primitive type of this atomic value. This will always be an atomic type.
254 */
255
256 /*@NotNull*/
257 public QName getPrimitiveTypeName() {
258 AtomicValue value = getUnderlyingValue();
259 BuiltInAtomicType type = value.getPrimitiveType();
260 return new QName(type.getStructuredQName());
261 }
262
263
264 /**
265 * Get the type of this atomic value, as a QName.
266 *
267 * @return a QName naming the type of this atomic value. This will always be an atomic type.
268 */
269 public QName getTypeName() {
270 AtomicValue value = getUnderlyingValue();
271 AtomicType type = value.getItemType();
272 return new QName(type.getStructuredQName());
273 }
274
275 /**
276 * Get the value as a Java object of the nearest equivalent type.
277 * <p>The result type is as follows:</p>
278 * <table>
279 * <tr><th>XPath type</th> <th>Java class</th></tr>
280 * <tr><td>xs:string</td> <td>String</td></tr>
281 * <tr><td>xs:integer</td> <td>java.math.BigInteger</td></tr>
282 * <tr><td>xs:decimal</td> <td>java.math.BigDecimal</td></tr>
283 * <tr><td>xs:double</td> <td>Double</td></tr>
284 * <tr><td>xs:float</td> <td>Float</td></tr>
285 * <tr><td>xs:boolean</td> <td>Boolean</td></tr>
286 * <tr><td>xs:QName</td> <td>QName</td></tr>
287 * <tr><td>xs:anyURI</td> <td>String</td></tr>
288 * <tr><td>xs:untypedAtomic</td><td>String</td></tr>
289 * <tr><td>Other types</td> <td>currently String, but this may change in the future</td></tr>
290 * </table>
291 *
292 * @return the value, converted to a Java object of a suitable type
293 */
294
295 @SuppressWarnings({"AutoBoxing"})
296 public Object getValue() {
297 AtomicValue av = getUnderlyingValue();
298 if (av instanceof StringValue) {
299 return av.getStringValue();
300 } else if (av instanceof IntegerValue) {
301 return ((IntegerValue) av).asBigInteger();
302 } else if (av instanceof DoubleValue) {
303 return ((DoubleValue) av).getDoubleValue();
304 } else if (av instanceof FloatValue) {
305 return ((FloatValue) av).getFloatValue();
306 } else if (av instanceof BooleanValue) {
307 return ((BooleanValue) av).getBooleanValue();
308 } else if (av instanceof BigDecimalValue) {
309 return ((BigDecimalValue) av).getDecimalValue();
310 } else if (av instanceof QNameValue) {
311 QNameValue q = (QNameValue) av;
312 return new QName(q.getPrefix(), q.getNamespaceURI(), q.getLocalName());
313 } else {
314 return av.getStringValue();
315 }
316 }
317
318 /**
319 * Get the value converted to a boolean using the XPath casting rules
320 *
321 * @return the result of converting to a boolean (Note: this is not the same as the
322 * effective boolean value).
323 * @throws SaxonApiException if the value cannot be cast to a boolean
324 */
325
326 public boolean getBooleanValue() throws SaxonApiException {
327 AtomicValue av = getUnderlyingValue();
328 if (av instanceof BooleanValue) {
329 return ((BooleanValue) av).getBooleanValue();
330 } else if (av instanceof NumericValue) {
331 return !av.isNaN() && ((NumericValue) av).signum() != 0;
332 } else if (av instanceof StringValue) {
333 String s = av.getStringValue().trim();
334 return "1".equals(s) || "true".equals(s);
335 } else {
336 throw new SaxonApiException("Cannot cast item to a boolean");
337 }
338 }
339
340 /**
341 * Get the value converted to an integer using the XPath casting rules
342 *
343 * @return the result of converting to an integer
344 * @throws SaxonApiException if the value cannot be cast to an integer
345 */
346
347 public long getLongValue() throws SaxonApiException {
348 AtomicValue av = getUnderlyingValue();
349 if (av instanceof BooleanValue) {
350 return ((BooleanValue) av).getBooleanValue() ? 0L : 1L;
351 } else if (av instanceof NumericValue) {
352 try {
353 return ((NumericValue) av).longValue();
354 } catch (XPathException e) {
355 throw new SaxonApiException("Cannot cast item to an integer");
356 }
357 } else if (av instanceof StringValue) {
358 StringToDouble converter = StringToDouble.getInstance();
359 return (long) converter.stringToNumber(av.getStringValueCS());
360 } else {
361 throw new SaxonApiException("Cannot cast item to an integer");
362 }
363 }
364
365 /**
366 * Get the value converted to a double using the XPath casting rules.
367 * <p>If the value is a string, the XSD 1.1 rules are used, which means that the string
368 * "+INF" is recognised.</p>
369 *
370 * @return the result of converting to a double
371 * @throws SaxonApiException if the value cannot be cast to a double
372 */
373
374 public double getDoubleValue() throws SaxonApiException {
375 AtomicValue av = getUnderlyingValue();
376 if (av instanceof BooleanValue) {
377 return ((BooleanValue) av).getBooleanValue() ? 0.0 : 1.0;
378 } else if (av instanceof NumericValue) {
379 return ((NumericValue) av).getDoubleValue();
380 } else if (av instanceof StringValue) {
381 try {
382 StringToDouble converter = StringToDouble11.getInstance();
383 return converter.stringToNumber(av.getStringValueCS());
384 } catch (NumberFormatException e) {
385 throw new SaxonApiException(e.getMessage());
386 }
387 } else {
388 throw new SaxonApiException("Cannot cast item to a double");
389 }
390 }
391
392 /**
393 * Get the value converted to a decimal using the XPath casting rules
394 *
395 * @return the result of converting to a decimal
396 * @throws SaxonApiException if the value cannot be cast to a double
397 */
398
399 public BigDecimal getDecimalValue() throws SaxonApiException {
400 AtomicValue av = getUnderlyingValue();
401 if (av instanceof BooleanValue) {
402 return ((BooleanValue) av).getBooleanValue() ? BigDecimal.ZERO : BigDecimal.ONE;
403 } else if (av instanceof NumericValue) {
404 try {
405 return ((NumericValue) av).getDecimalValue();
406 } catch (XPathException e) {
407 throw new SaxonApiException("Cannot cast item to a decimal");
408 }
409 } else if (av instanceof StringValue) {
410 return new BigDecimal(av.getStringValueCS().toString());
411 } else {
412 throw new SaxonApiException("Cannot cast item to a decimal");
413 }
414 }
415
416 /**
417 * For an XdmAtomicValue representing an xs:QName or xs:NOTATION value,
418 * get the value as an instance of QName
419 * @return the corresponding QName if the value is an xs:QName or xs:NOTATION,
420 * or null otherwise
421 */
422
423 public QName getQNameValue() {
424 AtomicValue av = getUnderlyingValue();
425 if (av instanceof QualifiedNameValue) {
426 return new QName(((QualifiedNameValue)av).getStructuredQName());
427 } else {
428 return null;
429 }
430 }
431
432 /**
433 * Compare values for equality.
434 * Two atomic values are considered equal if they are equal according to the
435 * rules of the op:is-same-key() operation, used when comparing keys in maps
436 * @param other the value to be compared
437 * @return true if the values are deemed equal
438 */
439
440 public boolean equals(Object other) {
441 if (other instanceof XdmAtomicValue) {
442 AtomicMatchKey a = getUnderlyingValue().asMapKey();
443 AtomicMatchKey b = ((XdmAtomicValue)other).getUnderlyingValue().asMapKey();
444 return a.equals(b);
445 } else {
446 return false;
447 }
448 }
449
450 /**
451 * Get a hashcode that reflects the rules for equality matching
452 * @return a suitable hashcode
453 */
454
455 @Override
456 public int hashCode() {
457 return getUnderlyingValue().asMapKey().hashCode();
458 }
459 }
460