| 1 7 package org.jboss.xml.binding; 8 9 import org.jboss.logging.Logger; 10 11 import java.io.Serializable ; 12 import java.io.ByteArrayOutputStream ; 13 import java.math.BigInteger ; 14 import java.math.BigDecimal ; 15 import java.text.ParseException ; 16 import java.util.Calendar ; 17 import java.util.TimeZone ; 18 import java.util.StringTokenizer ; 19 import java.net.URISyntaxException ; 20 21 25 public final class TypeBinding 26 implements Serializable  27 { 28 static final long serialVersionUID = 2815361862736153588L; 29 public static final String XS_ANYSIMPLETYPE_NAME = "anySimpleType"; 30 31 public static final String XS_STRING_NAME = "string"; 35 public static final String XS_BOOLEAN_NAME = "boolean"; 36 public static final String XS_DECIMAL_NAME = "decimal"; 37 public static final String XS_FLOAT_NAME = "float"; 38 public static final String XS_DOUBLE_NAME = "double"; 39 public static final String XS_DURATION_NAME = "duration"; 40 public static final String XS_DATETIME_NAME = "dateTime"; 41 public static final String XS_TIME_NAME = "time"; 42 public static final String XS_DATE_NAME = "date"; 43 public static final String XS_GYEARMONTH_NAME = "gYearMonth"; 44 public static final String XS_GYEAR_NAME = "gYear"; 45 public static final String XS_GMONTHDAY_NAME = "gMonthDay"; 46 public static final String XS_GDAY_NAME = "gDay"; 47 public static final String XS_GMONTH_NAME = "gMonth"; 48 public static final String XS_HEXBINARY_NAME = "hexBinary"; 49 public static final String XS_BASE64BINARY_NAME = "base64Binary"; 50 public static final String XS_ANYURI_NAME = "anyURI"; 51 public static final String XS_QNAME_NAME = "QName"; 52 public static final String XS_NOTATION_NAME = "NOTATION"; 53 54 58 public static final String XS_NORMALIZEDSTRING_NAME = "normalizedString"; 59 public static final String XS_TOKEN_NAME = "token"; 60 public static final String XS_LANGUAGE_NAME = "language"; 61 public static final String XS_NMTOKEN_NAME = "NMToken"; 62 public static final String XS_NMTOKENS_NAME = "NMTokens"; 63 public static final String XS_NAME_NAME = "Name"; 64 public static final String XS_NCNAME_NAME = "NCName"; 65 public static final String XS_ID_NAME = "ID"; 66 public static final String XS_IDREF_NAME = "IDREF"; 67 public static final String XS_IDREFS_NAME = "IDREFS"; 68 public static final String XS_ENTITY_NAME = "ENTITY"; 69 public static final String XS_ENTITIES_NAME = "ENTITIES"; 70 public static final String XS_INTEGER_NAME = "integer"; 71 public static final String XS_NONPOSITIVEINTEGER_NAME = "nonPositiveInteger"; 72 public static final String XS_NEGATIVEINTEGER_NAME = "negativeInteger"; 73 public static final String XS_LONG_NAME = "long"; 74 public static final String XS_INT_NAME = "int"; 75 public static final String XS_SHORT_NAME = "short"; 76 public static final String XS_BYTE_NAME = "byte"; 77 public static final String XS_NONNEGATIVEINTEGER_NAME = "nonNegativeInteger"; 78 public static final String XS_UNSIGNEDLONG_NAME = "unsignedLong"; 79 public static final String XS_UNSIGNEDINT_NAME = "unsignedInt"; 80 public static final String XS_UNSIGNEDSHORT_NAME = "unsignedShort"; 81 public static final String XS_UNSIGNEDBYTE_NAME = "unsignedByte"; 82 public static final String XS_POSITIVEINTEGER_NAME = "positiveInteger"; 83 84 public static final int XS_INT = XS_INT_NAME.hashCode(); 85 public static final int XS_LONG = XS_LONG_NAME.hashCode(); 86 public static final int XS_SHORT = XS_SHORT_NAME.hashCode(); 87 public static final int XS_FLOAT = XS_FLOAT_NAME.hashCode(); 88 public static final int XS_DOUBLE = XS_DOUBLE_NAME.hashCode(); 89 public static final int XS_BOOLEAN = XS_BOOLEAN_NAME.hashCode(); 90 public static final int XS_BYTE = XS_BYTE_NAME.hashCode(); 91 public static final int XS_STRING = XS_STRING_NAME.hashCode(); 92 public static final int XS_INTEGER = XS_INTEGER_NAME.hashCode(); 93 public static final int XS_DECIMAL = XS_DECIMAL_NAME.hashCode(); 94 public static final int XS_DATETIME = XS_DATETIME_NAME.hashCode(); 95 public static final int XS_QNAME = XS_QNAME_NAME.hashCode(); 96 public static final int XS_ANYURI = XS_ANYURI_NAME.hashCode(); 97 public static final int XS_UNSIGNEDLONG = XS_UNSIGNEDLONG_NAME.hashCode(); 98 public static final int XS_UNSIGNEDINT = XS_UNSIGNEDINT_NAME.hashCode(); 99 public static final int XS_UNSIGNEDSHORT = XS_UNSIGNEDSHORT_NAME.hashCode(); 100 public static final int XS_UNSIGNEDBYTE = XS_UNSIGNEDBYTE_NAME.hashCode(); 101 public static final int XS_DATE = XS_DATE_NAME.hashCode(); 102 public static final int XS_TIME = XS_TIME_NAME.hashCode(); 103 public static final int XS_BASE64BINARY = XS_BASE64BINARY_NAME.hashCode(); 104 public static final int XS_HEXBINARY = XS_HEXBINARY_NAME.hashCode(); 105 public static final int XS_ANYSIMPLETYPE = XS_ANYSIMPLETYPE_NAME.hashCode(); 106 public static final int XS_DURATION = XS_DURATION_NAME.hashCode(); 107 public static final int XS_GYEARMONTH = XS_GYEARMONTH_NAME.hashCode(); 108 public static final int XS_GYEAR = XS_GYEAR_NAME.hashCode(); 109 public static final int XS_GMONTHDAY = XS_GMONTHDAY_NAME.hashCode(); 110 public static final int XS_GMONTH = XS_GMONTH_NAME.hashCode(); 111 public static final int XS_GDAY = XS_GDAY_NAME.hashCode(); 112 public static final int XS_NORMALIZEDSTRING = XS_NORMALIZEDSTRING_NAME.hashCode(); 113 public static final int XS_TOKEN = XS_TOKEN_NAME.hashCode(); 114 public static final int XS_LANGUAGE = XS_LANGUAGE_NAME.hashCode(); 115 public static final int XS_NAME = XS_NAME_NAME.hashCode(); 116 public static final int XS_NCNAME = XS_NCNAME_NAME.hashCode(); 117 public static final int XS_ID = XS_ID_NAME.hashCode(); 118 public static final int XS_NMTOKEN = XS_NMTOKEN_NAME.hashCode(); 119 public static final int XS_NMTOKENS = XS_NMTOKENS_NAME.hashCode(); 120 public static final int XS_NONPOSITIVEINTEGER = XS_NONPOSITIVEINTEGER_NAME.hashCode(); 121 public static final int XS_NEGATIVEINTEGER = XS_NEGATIVEINTEGER_NAME.hashCode(); 122 public static final int XS_NONNEGATIVEINTEGER = XS_NONNEGATIVEINTEGER_NAME.hashCode(); 123 public static final int XS_POSITIVEINTEGER = XS_POSITIVEINTEGER_NAME.hashCode(); 124 public static final int XS_NOTATION = XS_NOTATION_NAME.hashCode(); 125 public static final int XS_IDREF = XS_IDREF_NAME.hashCode(); 126 public static final int XS_IDREFS = XS_IDREFS_NAME.hashCode(); 127 public static final int XS_ENTITY = XS_ENTITY_NAME.hashCode(); 128 public static final int XS_ENTITIES = XS_ENTITIES_NAME.hashCode(); 129 130 static 132 { 133 int[] codes = new int[45]; 134 String [] names = new String [codes.length]; 135 int i = 0; 136 137 names[i] = XS_INT_NAME; 138 codes[i++] = XS_INT; 139 140 names[i] = XS_LONG_NAME; 141 codes[i++] = XS_LONG; 142 143 names[i] = XS_SHORT_NAME; 144 codes[i++] = XS_SHORT; 145 146 names[i] = XS_FLOAT_NAME; 147 codes[i++] = XS_FLOAT; 148 149 names[i] = XS_DOUBLE_NAME; 150 codes[i++] = XS_DOUBLE; 151 152 names[i] = XS_BOOLEAN_NAME; 153 codes[i++] = XS_BOOLEAN; 154 155 names[i] = XS_BYTE_NAME; 156 codes[i++] = XS_BYTE; 157 158 names[i] = XS_STRING_NAME; 159 codes[i++] = XS_STRING; 160 161 names[i] = XS_INTEGER_NAME; 162 codes[i++] = XS_INTEGER; 163 164 names[i] = XS_DECIMAL_NAME; 165 codes[i++] = XS_DECIMAL; 166 167 names[i] = XS_DATETIME_NAME; 168 codes[i++] = XS_DATETIME; 169 170 names[i] = XS_QNAME_NAME; 171 codes[i++] = XS_QNAME; 172 173 names[i] = XS_ANYURI_NAME; 174 codes[i++] = XS_ANYURI; 175 176 names[i] = XS_UNSIGNEDINT_NAME; 177 codes[i++] = XS_UNSIGNEDINT; 178 179 names[i] = XS_UNSIGNEDSHORT_NAME; 180 codes[i++] = XS_UNSIGNEDSHORT; 181 182 names[i] = XS_UNSIGNEDBYTE_NAME; 183 codes[i++] = XS_UNSIGNEDBYTE; 184 185 names[i] = XS_DATE_NAME; 186 codes[i++] = XS_DATE; 187 188 names[i] = XS_TIME_NAME; 189 codes[i++] = XS_TIME; 190 191 names[i] = XS_BASE64BINARY_NAME; 192 codes[i++] = XS_BASE64BINARY; 193 194 names[i] = XS_HEXBINARY_NAME; 195 codes[i++] = XS_HEXBINARY; 196 197 names[i] = XS_ANYSIMPLETYPE_NAME; 198 codes[i++] = XS_ANYSIMPLETYPE; 199 200 names[i] = XS_DURATION_NAME; 201 codes[i++] = XS_DURATION; 202 203 names[i] = XS_GYEARMONTH_NAME; 204 codes[i++] = XS_GYEARMONTH; 205 206 names[i] = XS_GYEAR_NAME; 207 codes[i++] = XS_GYEAR; 208 209 names[i] = XS_GMONTHDAY_NAME; 210 codes[i++] = XS_GMONTHDAY; 211 212 names[i] = XS_GMONTH_NAME; 213 codes[i++] = XS_GMONTH; 214 215 names[i] = XS_GDAY_NAME; 216 codes[i++] = XS_GDAY; 217 218 names[i] = XS_NORMALIZEDSTRING_NAME; 219 codes[i++] = XS_NORMALIZEDSTRING; 220 221 names[i] = XS_TOKEN_NAME; 222 codes[i++] = XS_TOKEN; 223 224 names[i] = XS_LANGUAGE_NAME; 225 codes[i++] = XS_LANGUAGE; 226 227 names[i] = XS_NAME_NAME; 228 codes[i++] = XS_NAME; 229 230 names[i] = XS_NCNAME_NAME; 231 codes[i++] = XS_NCNAME; 232 233 names[i] = XS_ID_NAME; 234 codes[i++] = XS_ID; 235 236 names[i] = XS_NMTOKEN_NAME; 237 codes[i++] = XS_NMTOKEN; 238 239 names[i] = XS_NMTOKENS_NAME; 240 codes[i++] = XS_NMTOKENS; 241 242 names[i] = XS_NONPOSITIVEINTEGER_NAME; 243 codes[i++] = XS_NONPOSITIVEINTEGER; 244 245 names[i] = XS_NONNEGATIVEINTEGER_NAME; 246 codes[i++] = XS_NONNEGATIVEINTEGER; 247 248 names[i] = XS_POSITIVEINTEGER_NAME; 249 codes[i++] = XS_POSITIVEINTEGER; 250 251 names[i] = XS_NEGATIVEINTEGER_NAME; 252 codes[i++] = XS_NEGATIVEINTEGER; 253 254 names[i] = XS_UNSIGNEDLONG_NAME; 255 codes[i++] = XS_UNSIGNEDLONG; 256 257 names[i] = XS_NOTATION_NAME; 258 codes[i++] = XS_NOTATION; 259 260 names[i] = XS_IDREF_NAME; 261 codes[i++] = XS_IDREF; 262 263 names[i] = XS_IDREFS_NAME; 264 codes[i++] = XS_IDREFS; 265 266 names[i] = XS_ENTITY_NAME; 267 codes[i++] = XS_ENTITY; 268 269 names[i] = XS_ENTITIES_NAME; 270 codes[i++] = XS_ENTITIES; 271 272 Logger log = Logger.getLogger(TypeBinding.class); 273 boolean allAreUnique = true; 274 for(int outer = 0; outer < names.length; ++outer) 275 { 276 int outerCode = codes[outer]; 277 String outerName = names[outer]; 278 279 for(int inner = outer + 1; inner < names.length; ++inner) 280 { 281 int innerCode = codes[inner]; 282 String innerName = names[inner]; 283 284 if(outerCode == innerCode) 285 { 286 log.error("Types have the same hash code " + outerCode + ": " + outerName + " and " + innerName); 287 allAreUnique = false; 288 } 289 } 290 } 291 292 if(!allAreUnique) 293 { 294 throw new IllegalStateException ("Not all the schema types have unique hash codes! See log for more details."); 295 } 296 } 297 298 public static Object unmarshal(String xsdType, String value) 299 { 300 int typeCode = xsdType.hashCode(); 301 Object result; 302 if(typeCode == XS_INT) 303 { 304 result = Integer.valueOf(value); 305 } 306 else if(typeCode == XS_LONG) 307 { 308 result = Long.valueOf(value); 309 } 310 else if(typeCode == XS_SHORT) 311 { 312 result = Short.valueOf(value); 313 } 314 else if(typeCode == XS_BYTE) 315 { 316 result = Byte.valueOf(value); 317 } 318 else if(typeCode == XS_FLOAT) 319 { 320 if("INF".equals(value)) 321 { 322 result = new Float (Float.POSITIVE_INFINITY); 323 } 324 else if("-INF".equals(value)) 325 { 326 result = new Float (Float.NEGATIVE_INFINITY); 327 } 328 else 329 { 330 result = Float.valueOf(value); 331 } 332 } 333 else if(typeCode == XS_DOUBLE) 334 { 335 if("INF".equals(value)) 336 { 337 result = new Double (Double.POSITIVE_INFINITY); 338 } 339 else if("-INF".equals(value)) 340 { 341 result = new Double (Double.NEGATIVE_INFINITY); 342 } 343 else 344 { 345 result = Double.valueOf(value); 346 } 347 } 348 else if(typeCode == XS_BOOLEAN) 349 { 350 if(value.length() == 1) 351 { 352 switch(value.charAt(0)) 353 { 354 case '1': 355 result = Boolean.TRUE; 356 break; 357 case '0': 358 result = Boolean.FALSE; 359 break; 360 default: 361 throw new JBossXBValueFormatException("An instance of a datatype that is defined as ?boolean? can have the following legal literals" + 362 " {true, false, 1, 0}. But got: " + value 363 ); 364 } 365 } 366 else 367 { 368 result = Boolean.valueOf(value); 369 } 370 } 371 else if(typeCode == XS_STRING) 372 { 373 result = value; 374 } 375 else if(typeCode == XS_INTEGER) 376 { 377 result = new BigInteger (value); 378 } 379 else if(typeCode == XS_DECIMAL) 380 { 381 result = new BigDecimal (value); 382 } 383 else if(typeCode == XS_DATETIME) 384 { 385 result = unmarshalDateTime(value); 386 } 387 else if(typeCode == XS_QNAME) 388 { 389 throw new IllegalStateException ("Recognized but not supported xsdType: " + XS_QNAME_NAME); 391 } 392 else if(typeCode == XS_ANYURI) 393 { 394 try 403 { 404 result = new java.net.URI (value); 405 } 406 catch(URISyntaxException e) 407 { 408 throw new JBossXBValueFormatException("Failed to unmarshal anyURI value " + value, e); 409 } 410 } 411 else if(typeCode == XS_UNSIGNEDLONG) 412 { 413 double d = Double.parseDouble(value); 414 if(d < 0 || d > 18446744073709551615D) 415 { 416 throw new JBossXBValueFormatException("Invalid unsignedLong value: " + value); 417 } 418 result = new Double (d); 419 } 420 else if(typeCode == XS_UNSIGNEDINT) 421 { 422 long l = Long.parseLong(value); 423 if(l < 0 || l > 4294967295L) 424 { 425 throw new JBossXBValueFormatException("Invalid unsignedInt value: " + value); 426 } 427 result = new Long (l); 428 } 429 else if(typeCode == XS_UNSIGNEDSHORT) 430 { 431 int i = Integer.parseInt(value); 432 if(i < 0 || i > 65535) 433 { 434 throw new JBossXBValueFormatException("Invalid unsignedShort value: " + value); 435 } 436 result = new Integer (i); 437 } 438 else if(typeCode == XS_UNSIGNEDBYTE) 439 { 440 short s = Short.parseShort(value); 441 if(s < 0 || s > 255) 442 { 443 throw new JBossXBValueFormatException("Invalid unsignedByte value: " + value); 444 } 445 result = new Short (s); 446 } 447 else if(typeCode == XS_DATE) 448 { 449 result = unmarshalDate(value); 450 } 451 else if(typeCode == XS_TIME) 452 { 453 result = unmarshalTime(value); 454 } 455 else if(typeCode == XS_BASE64BINARY) 456 { 457 throw new IllegalStateException ("Recognized but not supported xsdType: " + XS_BASE64BINARY_NAME); 459 } 460 else if(typeCode == XS_HEXBINARY) 461 { 462 result = unmarshalHexBinary(value); 463 } 464 else if(typeCode == XS_ANYSIMPLETYPE) 465 { 466 result = value; 467 } 468 else if(typeCode == XS_DURATION) 469 { 470 throw new IllegalStateException ("Recognized but not supported xsdType: " + XS_DURATION_NAME); 472 } 473 else if(typeCode == XS_GYEARMONTH) 474 { 475 result = unmarshalGYearMonth(value); 476 } 477 else if(typeCode == XS_GYEAR) 478 { 479 result = unmarshalGYear(value); 480 } 481 else if(typeCode == XS_GMONTHDAY) 482 { 483 result = unmarshalGMonthDay(value); 484 } 485 else if(typeCode == XS_GMONTH) 486 { 487 return unmarshalGMonth(value); 488 } 489 else if(typeCode == XS_GDAY) 490 { 491 return unmarshalGDay(value); 492 } 493 else if(typeCode == XS_NORMALIZEDSTRING) 494 { 495 if(isNormalizedString(value)) 496 { 497 result = value; 498 } 499 else 500 { 501 throw new JBossXBValueFormatException("Invalid normalizedString value: " + value); 502 } 503 } 504 else if(typeCode == XS_TOKEN) 505 { 506 if(isValidToken(value)) 507 { 508 result = value; 509 } 510 else 511 { 512 throw new JBossXBValueFormatException("Invalid token value: " + value); 513 } 514 } 515 else if(typeCode == XS_LANGUAGE) 516 { 517 result = value; 518 } 519 else if(typeCode == XS_NAME) 520 { 521 result = value; 522 } 523 else if(typeCode == XS_NCNAME) 524 { 525 result = value; 526 } 527 else if(typeCode == XS_ID) 528 { 529 result = value; 530 } 531 else if(typeCode == XS_NMTOKEN) 532 { 533 result = value; 534 } 535 else if(typeCode == XS_NMTOKENS) 536 { 537 result = unmarshalNMTokens(value); 538 } 539 else if(typeCode == XS_NONPOSITIVEINTEGER) 540 { 541 result = new BigInteger (value); 542 if(BigInteger.ZERO.compareTo(result) < 0) 543 { 544 throw new JBossXBValueFormatException("Invalid nonPositiveInteger value: " + value); 545 } 546 } 547 else if(typeCode == XS_NEGATIVEINTEGER) 548 { 549 result = new BigInteger (value); 550 if(BigInteger.ZERO.compareTo(result) <= 0) 551 { 552 throw new JBossXBValueFormatException("Invalid negativeInteger value: " + value); 553 } 554 } 555 else if(typeCode == XS_NONNEGATIVEINTEGER) 556 { 557 result = new BigInteger (value); 558 if(BigInteger.ZERO.compareTo(result) > 0) 559 { 560 throw new JBossXBValueFormatException("Invalid nonNegativeInteger value: " + value); 561 } 562 } 563 else if(typeCode == XS_POSITIVEINTEGER) 564 { 565 result = new BigInteger (value); 566 if(BigInteger.ZERO.compareTo(result) >= 0) 567 { 568 throw new JBossXBValueFormatException("Invalid positiveInteger value: " + value); 569 } 570 } 571 else if(typeCode == XS_NOTATION) 572 { 573 result = value; 575 } 576 else if(typeCode == XS_IDREF) 577 { 578 result = value; 579 } 580 else if(typeCode == XS_IDREFS) 581 { 582 result = unmarshalIdRefs(value); 583 } 584 else if(typeCode == XS_ENTITY) 585 { 586 result = value; 587 } 588 else if(typeCode == XS_ENTITIES) 589 { 590 result = unmarshalIdRefs(value); 591 } 592 else 593 { 594 throw new IllegalStateException ("Not supported xsdType: " + xsdType + ", hashCode=" + xsdType.hashCode()); 595 } 596 return result; 597 } 598 599 public static Object unmarshal(String value, Class javaType) 600 { 601 Object result; 602 if(String .class == javaType) 603 { 604 result = value; 605 } 606 else if(int.class == javaType || Integer .class == javaType) 607 { 608 result = Integer.valueOf(value); 609 } 610 else if(long.class == javaType || Long .class == javaType) 611 { 612 result = Long.valueOf(value); 613 } 614 else if(double.class == javaType || Double .class == javaType) 615 { 616 result = Double.valueOf(value); 617 } 618 else if(float.class == javaType || Float .class == javaType) 619 { 620 result = Float.valueOf(value); 621 } 622 else if(short.class == javaType || Short .class == javaType) 623 { 624 result = Short.valueOf(value); 625 } 626 else if(byte.class == javaType || Byte .class == javaType) 627 { 628 result = Byte.valueOf(value); 629 } 630 else if(char.class == javaType || Character .class == javaType) 631 { 632 result = new Character (value.charAt(0)); 633 } 634 else if(java.util.Date .class == javaType) 635 { 636 final String FORMAT = "yyyy-MM-dd"; 637 try 638 { 639 result = new java.text.SimpleDateFormat (FORMAT).parse(value); 640 } 641 catch(ParseException e) 642 { 643 throw new IllegalStateException ( 644 "Failed to parse date accroding to " + FORMAT + " format: " + value + ": " + e.getMessage() 645 ); 646 } 647 } 648 else if(Object .class == javaType) 649 { 650 result = value; 651 } 652 else 653 { 654 throw new IllegalStateException ("Unexpected field type " + javaType); 656 } 657 658 return result; 659 } 660 661 public static String marshal(String xsdType, Object value) 662 { 663 if(value == null) 664 { 665 throw new IllegalArgumentException ("Can't marshal null value!"); 666 } 667 668 int typeCode = xsdType.hashCode(); 669 String result; 670 if(typeCode == XS_INT) 671 { 672 Integer i = (Integer )value; 673 result = i.toString(); 674 } 675 else if(typeCode == XS_LONG) 676 { 677 Long l = (Long )value; 678 result = l.toString(); 679 } 680 else if(typeCode == XS_SHORT) 681 { 682 Short s = (Short )value; 683 result = s.toString(); 684 } 685 else if(typeCode == XS_BYTE) 686 { 687 Byte b = (Byte )value; 688 result = b.toString(); 689 } 690 else if(typeCode == XS_FLOAT) 691 { 692 Float f = (Float )value; 693 if(f.floatValue() == Float.POSITIVE_INFINITY) 694 { 695 result = "INF"; 696 } 697 else if(f.floatValue() == Float.NEGATIVE_INFINITY) 698 { 699 result = "-INF"; 700 } 701 else 702 { 703 result = f.toString(); 704 } 705 } 706 else if(typeCode == XS_DOUBLE) 707 { 708 Double d = (Double )value; 709 if(d.doubleValue() == Double.POSITIVE_INFINITY) 710 { 711 result = "INF"; 712 } 713 else if(d.doubleValue() == Double.NEGATIVE_INFINITY) 714 { 715 result = "-INF"; 716 } 717 else 718 { 719 result = d.toString(); 720 } 721 } 722 else if(typeCode == XS_BOOLEAN) 723 { 724 if(value instanceof Boolean ) 725 { 726 result = ((Boolean )value).booleanValue() ? "true" : "false"; 727 } 728 else if(value instanceof Number ) 729 { 730 Number n = (Number )value; 731 switch(n.byteValue()) 732 { 733 case 1: 734 result = "1"; 735 break; 736 case 0: 737 result = "0"; 738 break; 739 default: 740 throw new JBossXBValueFormatException("An instance of a datatype that is defined as ?boolean? can have the following legal literals" + 741 " {true, false, 1, 0}. But got: " + value 742 ); 743 } 744 } 745 else 746 { 747 throw new JBossXBValueFormatException("Java value for XSD boolean type expected to be an instance of java.lang.Boolean or java.lang.Number. But the value is of type " + 748 value.getClass().getName() 749 ); 750 } 751 } 752 else if(typeCode == XS_STRING) 753 { 754 result = (String )value; 755 } 756 else if(typeCode == XS_INTEGER) 757 { 758 BigInteger bi = (BigInteger )value; 759 result = bi.toString(); 760 } 761 else if(typeCode == XS_DECIMAL) 762 { 763 BigDecimal bd = (BigDecimal )value; 764 result = bd.toString(); 765 } 766 else if(typeCode == XS_DATETIME) 767 { 768 Calendar c = (Calendar )value; 769 result = marshalDateTime(c); 770 } 771 else if(typeCode == XS_QNAME) 772 { 773 throw new IllegalStateException ("Recognized but not supported xsdType: " + xsdType); 775 } 776 else if(typeCode == XS_ANYURI) 777 { 778 java.net.URI u = (java.net.URI )value; 779 result = u.toString(); 780 } 781 else if(typeCode == XS_UNSIGNEDLONG) 782 { 783 Double d = (Double )value; 784 if(d.doubleValue() < 0 || d.doubleValue() > 18446744073709551615D) 785 { 786 throw new JBossXBValueFormatException("Invalid unsignedLong value: " + value);
|