| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.type; |
| 9 |
|
| 10 |
import net.sf.saxon.lib.ConversionRules; |
| 11 |
import net.sf.saxon.om.NameChecker; |
| 12 |
import net.sf.saxon.om.NamespaceResolver; |
| 13 |
import net.sf.saxon.om.QNameException; |
| 14 |
import net.sf.saxon.trans.Err; |
| 15 |
import net.sf.saxon.trans.XPathException; |
| 16 |
import net.sf.saxon.tree.util.FastStringBuffer; |
| 17 |
import net.sf.saxon.value.*; |
| 18 |
import net.sf.saxon.value.StringValue; |
| 19 |
|
| 20 |
import java.util.regex.Pattern; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public abstract class StringConverter extends Converter { |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
protected StringConverter() { |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
protected StringConverter(ConversionRules rules) { |
| 51 |
super(rules); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public abstract ConversionResult convertString( CharSequence input); |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public ValidationFailure validate( CharSequence input) { |
| 76 |
ConversionResult result = convertString(input); |
| 77 |
return result instanceof ValidationFailure ? (ValidationFailure) result : null; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
@Override |
| 82 |
public ConversionResult convert(AtomicValue input) { |
| 83 |
return convertString(input.getStringValueCS()); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public static class StringToNonStringDerivedType extends StringConverter { |
| 93 |
private StringConverter phaseOne; |
| 94 |
private DownCastingConverter phaseTwo; |
| 95 |
|
| 96 |
public StringToNonStringDerivedType(StringConverter phaseOne, DownCastingConverter phaseTwo) { |
| 97 |
this.phaseOne = phaseOne; |
| 98 |
this.phaseTwo = phaseTwo; |
| 99 |
} |
| 100 |
|
| 101 |
@Override |
| 102 |
public StringToNonStringDerivedType setNamespaceResolver(NamespaceResolver resolver) { |
| 103 |
return new StringToNonStringDerivedType( |
| 104 |
(StringConverter) phaseOne.setNamespaceResolver(resolver), |
| 105 |
(DownCastingConverter) phaseTwo.setNamespaceResolver(resolver)); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
public ConversionResult convert(StringValue input) { |
| 110 |
CharSequence in = input.getStringValueCS(); |
| 111 |
try { |
| 112 |
in = phaseTwo.getTargetType().preprocess(in); |
| 113 |
} catch (ValidationException err) { |
| 114 |
return err.getValidationFailure(); |
| 115 |
} |
| 116 |
ConversionResult temp = phaseOne.convertString(in); |
| 117 |
if (temp instanceof ValidationFailure) { |
| 118 |
return temp; |
| 119 |
} |
| 120 |
return phaseTwo.convert((AtomicValue) temp, in); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
public ConversionResult convertString( CharSequence input) { |
| 125 |
try { |
| 126 |
input = phaseTwo.getTargetType().preprocess(input); |
| 127 |
} catch (ValidationException err) { |
| 128 |
return err.getValidationFailure(); |
| 129 |
} |
| 130 |
ConversionResult temp = phaseOne.convertString(input); |
| 131 |
if (temp instanceof ValidationFailure) { |
| 132 |
return temp; |
| 133 |
} |
| 134 |
return phaseTwo.convert((AtomicValue) temp, input); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
@Override |
| 146 |
public ValidationFailure validate(CharSequence input) { |
| 147 |
try { |
| 148 |
input = phaseTwo.getTargetType().preprocess(input); |
| 149 |
} catch (ValidationException err) { |
| 150 |
return err.getValidationFailure(); |
| 151 |
} |
| 152 |
ConversionResult temp = phaseOne.convertString(input); |
| 153 |
if (temp instanceof ValidationFailure) { |
| 154 |
return (ValidationFailure) temp; |
| 155 |
} |
| 156 |
return phaseTwo.validate((AtomicValue) temp, input); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
public static class StringToString extends StringConverter { |
| 165 |
public static final StringToString INSTANCE = new StringToString(); |
| 166 |
|
| 167 |
@Override |
| 168 |
public ConversionResult convert(AtomicValue input) { |
| 169 |
return new StringValue(input.getStringValueCS()); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
public ConversionResult convertString( CharSequence input) { |
| 174 |
return new StringValue(input); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
public ValidationFailure validate( CharSequence input) { |
| 179 |
return null; |
| 180 |
} |
| 181 |
|
| 182 |
public boolean isAlwaysSuccessful() { |
| 183 |
return true; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
public static class StringToUntypedAtomic extends StringConverter { |
| 192 |
public static final StringToUntypedAtomic INSTANCE = new StringToUntypedAtomic(); |
| 193 |
|
| 194 |
@Override |
| 195 |
public UntypedAtomicValue convert(AtomicValue input) { |
| 196 |
return new UntypedAtomicValue(input.getStringValueCS()); |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
public ConversionResult convertString( CharSequence input) { |
| 201 |
return new UntypedAtomicValue(input); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
public ValidationFailure validate( CharSequence input) { |
| 206 |
return null; |
| 207 |
} |
| 208 |
|
| 209 |
public boolean isAlwaysSuccessful() { |
| 210 |
return true; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
public static class StringToNormalizedString extends StringConverter { |
| 220 |
public static final StringToNormalizedString INSTANCE = new StringToNormalizedString(); |
| 221 |
|
| 222 |
public ConversionResult convertString( CharSequence input) { |
| 223 |
return new StringValue(Whitespace.normalizeWhitespace(input), BuiltInAtomicType.NORMALIZED_STRING); |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
public ValidationFailure validate( CharSequence input) { |
| 228 |
return null; |
| 229 |
} |
| 230 |
|
| 231 |
public boolean isAlwaysSuccessful() { |
| 232 |
return true; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
public static class StringToToken extends StringConverter { |
| 241 |
public static final StringToToken INSTANCE = new StringToToken(); |
| 242 |
|
| 243 |
public ConversionResult convertString( CharSequence input) { |
| 244 |
return new StringValue(Whitespace.collapseWhitespace(input), BuiltInAtomicType.TOKEN); |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
public ValidationFailure validate( CharSequence input) { |
| 249 |
return null; |
| 250 |
} |
| 251 |
|
| 252 |
public boolean isAlwaysSuccessful() { |
| 253 |
return true; |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
public static class StringToLanguage extends StringConverter { |
| 262 |
private final static Pattern regex = Pattern.compile("[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*"); |
| 263 |
|
| 264 |
public static final StringToLanguage INSTANCE = new StringToLanguage(); |
| 265 |
|
| 266 |
|
| 267 |
public ConversionResult convertString( CharSequence input) { |
| 268 |
CharSequence trimmed = Whitespace.trimWhitespace(input); |
| 269 |
if (!regex.matcher(trimmed).matches()) { |
| 270 |
return new ValidationFailure("The value '" + input + "' is not a valid xs:language"); |
| 271 |
} |
| 272 |
return new StringValue(trimmed, BuiltInAtomicType.LANGUAGE); |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
public ValidationFailure validate( CharSequence input) { |
| 277 |
if (regex.matcher(Whitespace.trimWhitespace(input)).matches()) { |
| 278 |
return null; |
| 279 |
} else { |
| 280 |
return new ValidationFailure("The value '" + input + "' is not a valid xs:language"); |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
public static class StringToNCName extends StringConverter { |
| 290 |
|
| 291 |
public static final StringToNCName TO_ID = new StringToNCName(BuiltInAtomicType.ID); |
| 292 |
public static final StringToNCName TO_ENTITY = new StringToNCName(BuiltInAtomicType.ENTITY); |
| 293 |
public static final StringToNCName TO_NCNAME = new StringToNCName(BuiltInAtomicType.NCNAME); |
| 294 |
public static final StringToNCName TO_IDREF = new StringToNCName(BuiltInAtomicType.IDREF); |
| 295 |
|
| 296 |
AtomicType targetType; |
| 297 |
|
| 298 |
public StringToNCName(AtomicType targetType) { |
| 299 |
this.targetType = targetType; |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
public ConversionResult convertString( CharSequence input) { |
| 304 |
CharSequence trimmed = Whitespace.trimWhitespace(input); |
| 305 |
if (NameChecker.isValidNCName(trimmed)) { |
| 306 |
return new StringValue(trimmed, targetType); |
| 307 |
} else { |
| 308 |
return new ValidationFailure("The value '" + input + "' is not a valid " + targetType.getDisplayName()); |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
public ValidationFailure validate( CharSequence input) { |
| 314 |
if (NameChecker.isValidNCName(Whitespace.trimWhitespace(input))) { |
| 315 |
return null; |
| 316 |
} else { |
| 317 |
return new ValidationFailure("The value '" + input + "' is not a valid " + targetType.getDisplayName()); |
| 318 |
} |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
public static class StringToNMTOKEN extends StringConverter { |
| 327 |
|
| 328 |
public final static StringToNMTOKEN INSTANCE = new StringToNMTOKEN(); |
| 329 |
|
| 330 |
|
| 331 |
public ConversionResult convertString( CharSequence input) { |
| 332 |
CharSequence trimmed = Whitespace.trimWhitespace(input); |
| 333 |
if (NameChecker.isValidNmtoken(trimmed)) { |
| 334 |
return new StringValue(trimmed, BuiltInAtomicType.NMTOKEN); |
| 335 |
} else { |
| 336 |
return new ValidationFailure("The value '" + input + "' is not a valid xs:NMTOKEN"); |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
public ValidationFailure validate( CharSequence input) { |
| 342 |
if (NameChecker.isValidNmtoken(Whitespace.trimWhitespace(input))) { |
| 343 |
return null; |
| 344 |
} else { |
| 345 |
return new ValidationFailure("The value '" + input + "' is not a valid xs:NMTOKEN"); |
| 346 |
} |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
public static class StringToName extends StringToNCName { |
| 356 |
public static final StringToName INSTANCE = new StringToName(); |
| 357 |
|
| 358 |
public StringToName() { |
| 359 |
super(BuiltInAtomicType.NAME); |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
public ConversionResult convertString( CharSequence input) { |
| 364 |
ValidationFailure vf = validate(input); |
| 365 |
if (vf == null) { |
| 366 |
return new StringValue(Whitespace.trimWhitespace(input), BuiltInAtomicType.NAME); |
| 367 |
} else { |
| 368 |
return vf; |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
public ValidationFailure validate( CharSequence input) { |
| 374 |
|
| 375 |
CharSequence trimmed = Whitespace.trimWhitespace(input); |
| 376 |
if (NameChecker.isValidNCName(trimmed)) { |
| 377 |
return null; |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
FastStringBuffer buff = new FastStringBuffer(trimmed.length()); |
| 382 |
buff.append(trimmed); |
| 383 |
for (int i = 0; i < buff.length(); i++) { |
| 384 |
if (buff.charAt(i) == ':') { |
| 385 |
buff.setCharAt(i, '_'); |
| 386 |
} |
| 387 |
} |
| 388 |
if (NameChecker.isValidNCName(buff)) { |
| 389 |
return null; |
| 390 |
} else { |
| 391 |
return new ValidationFailure("The value '" + trimmed + "' is not a valid xs:Name"); |
| 392 |
} |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
public static class StringToStringSubtype extends StringConverter { |
| 401 |
AtomicType targetType; |
| 402 |
int whitespaceAction; |
| 403 |
|
| 404 |
public StringToStringSubtype(ConversionRules rules, AtomicType targetType) { |
| 405 |
super(rules); |
| 406 |
this.targetType = targetType; |
| 407 |
this.whitespaceAction = targetType.getWhitespaceAction(); |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
public ConversionResult convertString( CharSequence input) { |
| 412 |
CharSequence cs = Whitespace.applyWhitespaceNormalization(whitespaceAction, input); |
| 413 |
try { |
| 414 |
cs = targetType.preprocess(cs); |
| 415 |
} catch (ValidationException err) { |
| 416 |
return err.getValidationFailure(); |
| 417 |
} |
| 418 |
StringValue sv = new StringValue(cs); |
| 419 |
ValidationFailure f = targetType.validate(sv, cs, getConversionRules()); |
| 420 |
if (f == null) { |
| 421 |
sv.setTypeLabel(targetType); |
| 422 |
return sv; |
| 423 |
} else { |
| 424 |
return f; |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
public ValidationFailure validate( CharSequence input) { |
| 429 |
CharSequence cs = Whitespace.applyWhitespaceNormalization(whitespaceAction, input); |
| 430 |
try { |
| 431 |
cs = targetType.preprocess(cs); |
| 432 |
} catch (ValidationException err) { |
| 433 |
return err.getValidationFailure(); |
| 434 |
} |
| 435 |
return targetType.validate(new StringValue(cs), cs, getConversionRules()); |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
public static class StringToDerivedStringSubtype extends StringConverter { |
| 444 |
AtomicType targetType; |
| 445 |
StringConverter builtInValidator; |
| 446 |
int whitespaceAction; |
| 447 |
|
| 448 |
public StringToDerivedStringSubtype( ConversionRules rules, AtomicType targetType) { |
| 449 |
super(rules); |
| 450 |
this.targetType = targetType; |
| 451 |
this.whitespaceAction = targetType.getWhitespaceAction(); |
| 452 |
builtInValidator = ((AtomicType) targetType.getBuiltInBaseType()).getStringConverter(rules); |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
public ConversionResult convertString( CharSequence input) { |
| 457 |
CharSequence cs = Whitespace.applyWhitespaceNormalization(whitespaceAction, input); |
| 458 |
ValidationFailure f = builtInValidator.validate(cs); |
| 459 |
if (f != null) { |
| 460 |
return f; |
| 461 |
} |
| 462 |
try { |
| 463 |
cs = targetType.preprocess(cs); |
| 464 |
} catch (ValidationException err) { |
| 465 |
return err.getValidationFailure(); |
| 466 |
} |
| 467 |
StringValue sv = new StringValue(cs); |
| 468 |
f = targetType.validate(sv, cs, getConversionRules()); |
| 469 |
if (f == null) { |
| 470 |
sv.setTypeLabel(targetType); |
| 471 |
return sv; |
| 472 |
} else { |
| 473 |
return f; |
| 474 |
} |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
public static class StringToFloat extends StringConverter { |
| 484 |
public StringToFloat(ConversionRules rules) { |
| 485 |
super(rules); |
| 486 |
} |
| 487 |
|
| 488 |
|
| 489 |
public ConversionResult convertString( CharSequence input) { |
| 490 |
try { |
| 491 |
float flt = (float) getConversionRules().getStringToDoubleConverter().stringToNumber(input); |
| 492 |
return new FloatValue(flt); |
| 493 |
} catch (NumberFormatException err) { |
| 494 |
ValidationFailure ve = new ValidationFailure("Cannot convert string to float: " + input); |
| 495 |
ve.setErrorCode("FORG0001"); |
| 496 |
return ve; |
| 497 |
} |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
public static class StringToDecimal extends StringConverter { |
| 506 |
public static final StringToDecimal INSTANCE = new StringToDecimal(); |
| 507 |
|
| 508 |
public ConversionResult convertString( CharSequence input) { |
| 509 |
return BigDecimalValue.makeDecimalValue(input, true); |
| 510 |
} |
| 511 |
|
| 512 |
|
| 513 |
@Override |
| 514 |
public ValidationFailure validate( CharSequence input) { |
| 515 |
if (BigDecimalValue.castableAsDecimal(input)) { |
| 516 |
return null; |
| 517 |
} else { |
| 518 |
return new ValidationFailure("Cannot convert string to decimal: " + input); |
| 519 |
} |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
public static class StringToInteger extends StringConverter { |
| 528 |
public static final StringToInteger INSTANCE = new StringToInteger(); |
| 529 |
|
| 530 |
public ConversionResult convert(StringValue input) { |
| 531 |
return IntegerValue.stringToInteger(input.getStringValueCS()); |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
public ConversionResult convertString( CharSequence input) { |
| 536 |
return IntegerValue.stringToInteger(input); |
| 537 |
} |
| 538 |
|
| 539 |
@Override |
| 540 |
public ValidationFailure validate( CharSequence input) { |
| 541 |
return IntegerValue.castableAsInteger(input); |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
public static class StringToIntegerSubtype extends StringConverter { |
| 550 |
|
| 551 |
BuiltInAtomicType targetType; |
| 552 |
|
| 553 |
public StringToIntegerSubtype(BuiltInAtomicType targetType) { |
| 554 |
this.targetType = targetType; |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
public ConversionResult convertString( CharSequence input) { |
| 559 |
ConversionResult iv = IntegerValue.stringToInteger(input); |
| 560 |
if (iv instanceof Int64Value) { |
| 561 |
boolean ok = IntegerValue.checkRange(((Int64Value) iv).longValue(), targetType); |
| 562 |
if (ok) { |
| 563 |
return ((Int64Value) iv).copyAsSubType(targetType); |
| 564 |
} else { |
| 565 |
return new ValidationFailure("Integer value is out of range for type " + targetType); |
| 566 |
} |
| 567 |
} else if (iv instanceof BigIntegerValue) { |
| 568 |
boolean ok = IntegerValue.checkBigRange(((BigIntegerValue) iv).asBigInteger(), targetType); |
| 569 |
if (ok) { |
| 570 |
((BigIntegerValue) iv).setTypeLabel(targetType); |
| 571 |
return iv; |
| 572 |
} else { |
| 573 |
return new ValidationFailure("Integer value is out of range for type " + targetType); |
| 574 |
} |
| 575 |
} else { |
| 576 |
assert iv instanceof ValidationFailure; |
| 577 |
return iv; |
| 578 |
} |
| 579 |
} |
| 580 |
} |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
public static class StringToDuration extends StringConverter { |
| 587 |
public static final StringToDuration INSTANCE = new StringToDuration(); |
| 588 |
|
| 589 |
public ConversionResult convertString( CharSequence input) { |
| 590 |
return DurationValue.makeDuration(input); |
| 591 |
} |
| 592 |
} |
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
public static class StringToDayTimeDuration extends StringConverter { |
| 600 |
public static final StringToDayTimeDuration INSTANCE = new StringToDayTimeDuration(); |
| 601 |
|
| 602 |
public ConversionResult convertString(CharSequence input) { |
| 603 |
return DayTimeDurationValue.makeDayTimeDurationValue(input); |
| 604 |
} |
| 605 |
} |
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
public static class StringToYearMonthDuration extends StringConverter { |
| 612 |
public static final StringToYearMonthDuration INSTANCE = new StringToYearMonthDuration(); |
| 613 |
|
| 614 |
public ConversionResult convertString( CharSequence input) { |
| 615 |
return YearMonthDurationValue.makeYearMonthDurationValue(input); |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
public static class StringToDateTime extends StringConverter { |
| 624 |
public StringToDateTime(ConversionRules rules) { |
| 625 |
super(rules); |
| 626 |
} |
| 627 |
|
| 628 |
|
| 629 |
public ConversionResult convertString( CharSequence input) { |
| 630 |
return DateTimeValue.makeDateTimeValue(input, getConversionRules()); |
| 631 |
} |
| 632 |
} |
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
public static class StringToDateTimeStamp extends StringConverter { |
| 639 |
public StringToDateTimeStamp(ConversionRules rules) { |
| 640 |
super(rules); |
| 641 |
} |
| 642 |
|
| 643 |
|
| 644 |
public ConversionResult convertString( CharSequence input) { |
| 645 |
ConversionResult val = DateTimeValue.makeDateTimeValue(input, getConversionRules()); |
| 646 |
if (val instanceof DateTimeValue) { |
| 647 |
if (!((DateTimeValue) val).hasTimezone()) { |
| 648 |
return new ValidationFailure("Supplied DateTimeStamp value " + input + " has no time zone"); |
| 649 |
} else { |
| 650 |
((DateTimeValue) val).setTypeLabel(BuiltInAtomicType.DATE_TIME_STAMP); |
| 651 |
} |
| 652 |
} |
| 653 |
return val; |
| 654 |
} |
| 655 |
} |
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
public static class StringToDate extends StringConverter { |
| 662 |
public StringToDate(ConversionRules rules) { |
| 663 |
super(rules); |
| 664 |
} |
| 665 |
|
| 666 |
|
| 667 |
public ConversionResult convertString( CharSequence input) { |
| 668 |
return DateValue.makeDateValue(input, getConversionRules()); |
| 669 |
} |
| 670 |
} |
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
public static class StringToGMonth extends StringConverter { |
| 677 |
public static final StringToGMonth INSTANCE = new StringToGMonth(); |
| 678 |
|
| 679 |
public ConversionResult convertString( CharSequence input) { |
| 680 |
return GMonthValue.makeGMonthValue(input); |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
public static class StringToGYearMonth extends StringConverter { |
| 689 |
public StringToGYearMonth(ConversionRules rules) { |
| 690 |
super(rules); |
| 691 |
} |
| 692 |
|
| 693 |
|
| 694 |
public ConversionResult convertString( CharSequence input) { |
| 695 |
return GYearMonthValue.makeGYearMonthValue(input, getConversionRules()); |
| 696 |
} |
| 697 |
} |
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
public static class StringToGYear extends StringConverter { |
| 704 |
public StringToGYear(ConversionRules rules) { |
| 705 |
super(rules); |
| 706 |
} |
| 707 |
|
| 708 |
|
| 709 |
public ConversionResult convertString( CharSequence input) { |
| 710 |
return GYearValue.makeGYearValue(input, getConversionRules()); |
| 711 |
} |
| 712 |
} |
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
public static class StringToGMonthDay extends StringConverter { |
| 719 |
public static final StringToGMonthDay INSTANCE = new StringToGMonthDay(); |
| 720 |
|
| 721 |
public ConversionResult convertString(CharSequence input) { |
| 722 |
return GMonthDayValue.makeGMonthDayValue(input); |
| 723 |
} |
| 724 |
} |
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
public static class StringToGDay extends StringConverter { |
| 731 |
public static final StringToGDay INSTANCE = new StringToGDay(); |
| 732 |
|
| 733 |
|
| 734 |
public ConversionResult convertString( CharSequence input) { |
| 735 |
return GDayValue.makeGDayValue(input); |
| 736 |
} |
| 737 |
} |
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
public static class StringToTime extends StringConverter { |
| 744 |
public static final StringToTime INSTANCE = new StringToTime(); |
| 745 |
|
| 746 |
public ConversionResult convertString(CharSequence input) { |
| 747 |
return TimeValue.makeTimeValue(input); |
| 748 |
} |
| 749 |
} |
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
public static class StringToBoolean extends StringConverter { |
| 756 |
public static final StringToBoolean INSTANCE = new StringToBoolean(); |
| 757 |
|
| 758 |
public ConversionResult convertString( CharSequence input) { |
| 759 |
return BooleanValue.fromString(input); |
| 760 |
} |
| 761 |
} |
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
public static class StringToHexBinary extends StringConverter { |
| 768 |
public static final StringToHexBinary INSTANCE = new StringToHexBinary(); |
| 769 |
|
| 770 |
public ConversionResult convertString( CharSequence input) { |
| 771 |
try { |
| 772 |
return new HexBinaryValue(input); |
| 773 |
} catch (XPathException e) { |
| 774 |
return ValidationFailure.fromException(e); |
| 775 |
} |
| 776 |
} |
| 777 |
} |
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
public static class StringToBase64Binary extends StringConverter { |
| 784 |
public static final StringToBase64Binary INSTANCE = new StringToBase64Binary(); |
| 785 |
|
| 786 |
public ConversionResult convertString( CharSequence input) { |
| 787 |
try { |
| 788 |
return new Base64BinaryValue(input); |
| 789 |
} catch (XPathException e) { |
| 790 |
return ValidationFailure.fromException(e); |
| 791 |
} |
| 792 |
} |
| 793 |
} |
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
public static class StringToQName extends StringConverter { |
| 801 |
|
| 802 |
NamespaceResolver nsResolver; |
| 803 |
|
| 804 |
public StringToQName(ConversionRules rules) { |
| 805 |
super(rules); |
| 806 |
} |
| 807 |
|
| 808 |
public StringToQName setNamespaceResolver(NamespaceResolver resolver) { |
| 809 |
StringToQName c = new StringToQName(getConversionRules()); |
| 810 |
c.nsResolver = resolver; |
| 811 |
return c; |
| 812 |
} |
| 813 |
|
| 814 |
@Override |
| 815 |
public NamespaceResolver getNamespaceResolver() { |
| 816 |
return nsResolver; |
| 817 |
} |
| 818 |
|
| 819 |
|
| 820 |
public ConversionResult convertString( CharSequence input) { |
| 821 |
if (nsResolver == null) { |
| 822 |
throw new UnsupportedOperationException("Cannot validate a QName without a namespace resolver"); |
| 823 |
} |
| 824 |
try { |
| 825 |
String[] parts = NameChecker.getQNameParts(Whitespace.trimWhitespace(input)); |
| 826 |
String uri = nsResolver.getURIForPrefix(parts[0], true); |
| 827 |
if (uri == null) { |
| 828 |
ValidationFailure failure = new ValidationFailure("Namespace prefix " + |
| 829 |
Err.wrap(parts[0]) + " has not been declared"); |
| 830 |
failure.setErrorCode("FONS0004"); |
| 831 |
return failure; |
| 832 |
} |
| 833 |
return new QNameValue(parts[0], uri, parts[1], BuiltInAtomicType.QNAME, false); |
| 834 |
} catch (QNameException err) { |
| 835 |
return new ValidationFailure("Invalid lexical QName " + Err.wrap(input)); |
| 836 |
} catch (XPathException err) { |
| 837 |
return new ValidationFailure(err.getMessage()); |
| 838 |
} |
| 839 |
} |
| 840 |
} |
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
public static class StringToNotation extends StringConverter { |
| 847 |
|
| 848 |
NamespaceResolver nsResolver; |
| 849 |
|
| 850 |
public StringToNotation(ConversionRules rules) { |
| 851 |
super(rules); |
| 852 |
} |
| 853 |
|
| 854 |
@Override |
| 855 |
public StringToNotation setNamespaceResolver(NamespaceResolver resolver) { |
| 856 |
StringToNotation c = new StringToNotation(getConversionRules()); |
| 857 |
c.nsResolver = resolver; |
| 858 |
return c; |
| 859 |
} |
| 860 |
|
| 861 |
@Override |
| 862 |
public NamespaceResolver getNamespaceResolver() { |
| 863 |
return nsResolver; |
| 864 |
} |
| 865 |
|
| 866 |
|
| 867 |
public ConversionResult convertString( CharSequence input) { |
| 868 |
if (getNamespaceResolver() == null) { |
| 869 |
throw new UnsupportedOperationException("Cannot validate a NOTATION without a namespace resolver"); |
| 870 |
} |
| 871 |
try { |
| 872 |
String[] parts = NameChecker.getQNameParts(Whitespace.trimWhitespace(input)); |
| 873 |
String uri = getNamespaceResolver().getURIForPrefix(parts[0], true); |
| 874 |
if (uri == null) { |
| 875 |
return new ValidationFailure("Namespace prefix " + Err.wrap(parts[0]) + " has not been declared"); |
| 876 |
} |
| 877 |
|
| 878 |
|
| 879 |
if (!getConversionRules().isDeclaredNotation(uri, parts[1])) { |
| 880 |
return new ValidationFailure("Notation {" + uri + "}" + parts[1] + " is not declared in the schema"); |
| 881 |
} |
| 882 |
return new NotationValue(parts[0], uri, parts[1], false); |
| 883 |
} catch (QNameException err) { |
| 884 |
return new ValidationFailure("Invalid lexical QName " + Err.wrap(input)); |
| 885 |
} catch (XPathException err) { |
| 886 |
return new ValidationFailure(err.getMessage()); |
| 887 |
} |
| 888 |
} |
| 889 |
} |
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
public static class StringToAnyURI extends StringConverter { |
| 896 |
public StringToAnyURI(ConversionRules rules) { |
| 897 |
super(rules); |
| 898 |
} |
| 899 |
|
| 900 |
|
| 901 |
public ConversionResult convertString( CharSequence input) { |
| 902 |
if (getConversionRules().isValidURI(input)) { |
| 903 |
return new AnyURIValue(input); |
| 904 |
} else { |
| 905 |
return new ValidationFailure("Invalid URI: " + input); |
| 906 |
} |
| 907 |
} |
| 908 |
|
| 909 |
|
| 910 |
@Override |
| 911 |
public ValidationFailure validate( CharSequence input) { |
| 912 |
if (getConversionRules().isValidURI(input)) { |
| 913 |
return null; |
| 914 |
} else { |
| 915 |
return new ValidationFailure("Invalid URI: " + input); |
| 916 |
} |
| 917 |
} |
| 918 |
} |
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
public static class StringToUnionConverter extends StringConverter { |
| 925 |
|
| 926 |
SimpleType targetType; |
| 927 |
ConversionRules rules; |
| 928 |
|
| 929 |
public StringToUnionConverter(PlainType targetType, ConversionRules rules) { |
| 930 |
if (!targetType.isPlainType()) { |
| 931 |
throw new IllegalArgumentException(); |
| 932 |
} |
| 933 |
if (((SimpleType) targetType).isNamespaceSensitive()) { |
| 934 |
throw new IllegalArgumentException(); |
| 935 |
} |
| 936 |
this.targetType = (SimpleType) targetType; |
| 937 |
this.rules = rules; |
| 938 |
} |
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
@Override |
| 949 |
public ConversionResult convertString( CharSequence input) { |
| 950 |
try { |
| 951 |
return targetType.getTypedValue(input, null, rules).head(); |
| 952 |
} catch (ValidationException err) { |
| 953 |
return err.getValidationFailure(); |
| 954 |
} |
| 955 |
} |
| 956 |
} |
| 957 |
} |
| 958 |
|