| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 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 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public XdmAtomicValue(boolean value) { |
| 41 |
this(BooleanValue.get(value), true); |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public XdmAtomicValue(long value) { |
| 51 |
this(Int64Value.makeDerived(value, BuiltInAtomicType.LONG), true); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public XdmAtomicValue(int value) { |
| 61 |
this(Int64Value.makeDerived(value, BuiltInAtomicType.INT), true); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public XdmAtomicValue(short value) { |
| 71 |
this(Int64Value.makeDerived(value, BuiltInAtomicType.SHORT), true); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public XdmAtomicValue(byte value) { |
| 81 |
this(Int64Value.makeDerived(value, BuiltInAtomicType.BYTE), true); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public XdmAtomicValue(BigDecimal value) { |
| 91 |
this(new BigDecimalValue(value), true); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public XdmAtomicValue(double value) { |
| 101 |
this(new DoubleValue(value), true); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public XdmAtomicValue(float value) { |
| 111 |
this(new FloatValue(value), true); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public XdmAtomicValue(String value) { |
| 121 |
this(new StringValue(value), true); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public XdmAtomicValue(URI value) { |
| 131 |
this(new AnyURIValue(value.toString()), true); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
public XdmAtomicValue(QName value) { |
| 141 |
this(new QNameValue(value.getStructuredQName(), BuiltInAtomicType.QNAME), true); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 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 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 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 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
@Override |
| 235 |
public AtomicValue getUnderlyingValue() { |
| 236 |
return (AtomicValue)super.getUnderlyingValue(); |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
public String toString() { |
| 245 |
return getStringValue(); |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
public QName getPrimitiveTypeName() { |
| 258 |
AtomicValue value = getUnderlyingValue(); |
| 259 |
BuiltInAtomicType type = value.getPrimitiveType(); |
| 260 |
return new QName(type.getStructuredQName()); |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
public QName getTypeName() { |
| 270 |
AtomicValue value = getUnderlyingValue(); |
| 271 |
AtomicType type = value.getItemType(); |
| 272 |
return new QName(type.getStructuredQName()); |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 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 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 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 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 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 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 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 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 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 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 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 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 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 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
@Override |
| 456 |
public int hashCode() { |
| 457 |
return getUnderlyingValue().asMapKey().hashCode(); |
| 458 |
} |
| 459 |
} |
| 460 |
|