1 57 58 package org.apache.soap.encoding; 59 60 import java.io.*; 61 import java.util.*; 62 import java.math.*; 63 import org.w3c.dom.*; 64 import org.apache.soap.util.*; 65 import org.apache.soap.util.xml.*; 66 import org.apache.soap.*; 67 import org.apache.soap.rpc.*; 68 import org.apache.soap.encoding.literalxml.*; 69 import org.apache.soap.encoding.soapenc.*; 70 import org.apache.soap.encoding.xmi.*; 71 72 83 public class SOAPMappingRegistry extends XMLJavaMappingRegistry 84 { 85 private static SOAPMappingRegistry baseReg1999; 86 private static SOAPMappingRegistry baseReg2000; 87 private static SOAPMappingRegistry baseReg2001; 88 89 private SOAPMappingRegistry parent = null; 90 private String schemaURI; 91 92 private static String soapEncURI = Constants.NS_URI_SOAP_ENC; 93 94 private static QName arrayQName = new QName(soapEncURI, "Array"); 95 96 99 private static StringDeserializer stringDeser = new StringDeserializer(); 100 private static IntDeserializer intDeser = new IntDeserializer(); 101 private static DecimalDeserializer decimalDeser = new DecimalDeserializer(); 102 private static FloatDeserializer floatDeser = new FloatDeserializer(); 103 private static DoubleDeserializer doubleDeser = new DoubleDeserializer(); 104 private static BooleanDeserializer booleanDeser = new BooleanDeserializer(); 105 private static LongDeserializer longDeser = new LongDeserializer(); 106 private static ShortDeserializer shortDeser = new ShortDeserializer(); 107 private static ByteDeserializer byteDeser = new ByteDeserializer(); 108 private static HexDeserializer hexDeser = new HexDeserializer(); 109 110 private static QNameSerializer qNameSer = new QNameSerializer(); 111 private static ParameterSerializer paramSer = new ParameterSerializer(); 112 private static ArraySerializer arraySer = new ArraySerializer(); 113 private static VectorSerializer vectorSer = new VectorSerializer(); 114 private static HashtableSerializer hashtableSer = new HashtableSerializer(); 115 private static XMLParameterSerializer xmlParamSer = 116 new XMLParameterSerializer(); 117 private static DateSerializer dateSer = new DateSerializer(); 118 private static CalendarSerializer calSer = new CalendarSerializer(); 119 private static UrTypeDeserializer objDeser = new UrTypeDeserializer(); 120 public static MimePartSerializer partSer = new MimePartSerializer(); 121 122 131 private static QName schema1999QNames [] = { 132 Constants.string1999QName, 133 Constants.int1999QName, 134 Constants.int1999QName, 135 Constants.decimal1999QName, 136 Constants.float1999QName, 137 Constants.float1999QName, 138 Constants.double1999QName, 139 Constants.double1999QName, 140 Constants.boolean1999QName, 141 Constants.boolean1999QName, 142 Constants.long1999QName, 143 Constants.long1999QName, 144 Constants.short1999QName, 145 Constants.short1999QName, 146 Constants.byte1999QName, 147 Constants.byte1999QName, 148 Constants.hex1999QName, 149 Constants.qName1999QName, 150 Constants.date1999QName, 151 Constants.timeInst1999QName, 152 Constants.object1999QName, 153 Constants.object1999QName, 154 Constants.object1999QName, 155 Constants.object1999QName, 156 Constants.object1999QName, 157 }; 158 159 private static QName schema2000QNames [] = { 160 Constants.string2000QName, 161 Constants.int2000QName, 162 Constants.int2000QName, 163 Constants.decimal2000QName, 164 Constants.float2000QName, 165 Constants.float2000QName, 166 Constants.double2000QName, 167 Constants.double2000QName, 168 Constants.boolean2000QName, 169 Constants.boolean2000QName, 170 Constants.long2000QName, 171 Constants.long2000QName, 172 Constants.short2000QName, 173 Constants.short2000QName, 174 Constants.byte2000QName, 175 Constants.byte2000QName, 176 Constants.hex2000QName, 177 Constants.qName2000QName, 178 Constants.date2000QName, 179 Constants.timeInst2000QName, 180 Constants.object2000QName, 181 Constants.object2000QName, 182 Constants.object2000QName, 183 Constants.object2000QName, 184 Constants.object2000QName, 185 }; 186 187 private static QName schema2001QNames [] = { 188 Constants.string2001QName, 189 Constants.int2001QName, 190 Constants.int2001QName, 191 Constants.decimal2001QName, 192 Constants.float2001QName, 193 Constants.float2001QName, 194 Constants.double2001QName, 195 Constants.double2001QName, 196 Constants.boolean2001QName, 197 Constants.boolean2001QName, 198 Constants.long2001QName, 199 Constants.long2001QName, 200 Constants.short2001QName, 201 Constants.short2001QName, 202 Constants.byte2001QName, 203 Constants.byte2001QName, 204 Constants.hex2001QName, 205 Constants.qName2001QName, 206 Constants.date2001QName, 207 Constants.timeInst2001QName, 208 Constants.object2001QName, 209 Constants.object2001QName, 210 Constants.object2001QName, 211 Constants.object2001QName, 212 Constants.object2001QName, 213 }; 214 215 private static Class classes [] = { 216 String .class, 217 Integer .class, 218 int.class, 219 BigDecimal.class, 220 Float .class, 221 float.class, 222 Double .class, 223 double.class, 224 Boolean .class, 225 boolean.class, 226 Long .class, 227 long.class, 228 Short .class, 229 short.class, 230 Byte .class, 231 byte.class, 232 Hex.class, 233 QName.class, 234 GregorianCalendar.class, 235 Date.class, 236 javax.mail.internet.MimeBodyPart .class, 237 java.io.InputStream .class, 238 javax.activation.DataSource .class, 239 javax.activation.DataHandler .class, 240 Object .class, 241 }; 242 243 248 private static Serializer cleanSer = new Serializer() 249 { 250 public void marshall(String inScopeEncStyle, Class javaType, Object src, 251 Object context, Writer sink, NSStack nsStack, 252 XMLJavaMappingRegistry xjmr, SOAPContext ctx) 253 throws IllegalArgumentException , IOException { 254 nsStack.pushScope(); 255 256 if (src == null) 257 { 258 SoapEncUtils.generateNullStructure(inScopeEncStyle, javaType, context, 259 sink, nsStack, xjmr); 260 } 261 else 262 { 263 SoapEncUtils.generateStructureHeader(inScopeEncStyle, 264 javaType, 265 context, 266 sink, 267 nsStack, 268 xjmr); 269 270 sink.write(Utils.cleanString(src.toString()) + "</" + context + '>'); 271 } 272 nsStack.popScope(); 273 } 274 }; 275 276 281 private static Serializer ser = new Serializer() 282 { 283 public void marshall(String inScopeEncStyle, Class javaType, Object src, 284 Object context, Writer sink, NSStack nsStack, 285 XMLJavaMappingRegistry xjmr, SOAPContext ctx) 286 throws IllegalArgumentException , IOException { 287 nsStack.pushScope(); 288 289 SoapEncUtils.generateStructureHeader(inScopeEncStyle, 290 javaType, 291 context, 292 sink, 293 nsStack, 294 xjmr); 295 296 sink.write(src + "</" + context + '>'); 297 298 nsStack.popScope(); 299 } 300 }; 301 302 private static Serializer serializers [] = { 303 cleanSer, ser, ser, ser, ser, ser, ser, ser, ser, ser, ser, ser, ser, ser, ser, ser, ser, qNameSer, calSer, dateSer, partSer, partSer, partSer, partSer, null, }; 329 330 private static Deserializer deserializers [] = { 331 stringDeser, 332 null, 333 intDeser, 334 decimalDeser, 335 null, 336 floatDeser, 337 null, 338 doubleDeser, 339 null, 340 booleanDeser, 341 null, 342 longDeser, 343 null, 344 shortDeser, 345 null, 346 byteDeser, 347 hexDeser, 348 qNameSer, 349 calSer, 350 dateSer, 351 null, 352 null, 353 null, 354 null, 355 objDeser, 356 }; 357 358 362 public SOAPMappingRegistry() 363 { 364 this.schemaURI = Constants.NS_URI_CURRENT_SCHEMA_XSD; 365 this.parent = getBaseRegistry (Constants.NS_URI_CURRENT_SCHEMA_XSD); 366 } 367 368 373 public SOAPMappingRegistry(SOAPMappingRegistry parent) 374 { 375 this(parent, Constants.NS_URI_CURRENT_SCHEMA_XSD); 376 } 377 378 393 public SOAPMappingRegistry(SOAPMappingRegistry parent, String schemaURI) 394 { 395 this.parent = parent; 396 if (parent == null) { 397 initializeRegistry(schemaURI); 398 } 399 this.schemaURI = schemaURI; 400 } 401 402 407 public static SOAPMappingRegistry getBaseRegistry (String schemaURI) { 408 synchronized (SOAPMappingRegistry.class) { 409 if (baseReg1999 == null) { 410 baseReg1999 = 411 new SOAPMappingRegistry (null, Constants.NS_URI_1999_SCHEMA_XSD); 412 baseReg2000 = 413 new SOAPMappingRegistry (null, Constants.NS_URI_2000_SCHEMA_XSD); 414 baseReg2001 = 415 new SOAPMappingRegistry (null, Constants.NS_URI_2001_SCHEMA_XSD); 416 } 417 } 418 419 if (schemaURI.equals(Constants.NS_URI_1999_SCHEMA_XSD)) { 420 return baseReg1999; 421 } else if (schemaURI.equals(Constants.NS_URI_2000_SCHEMA_XSD)) { 422 return baseReg2000; 423 } else { 424 return baseReg2001; 425 } 426 } 427 428 432 public void setDefaultEncodingStyle(String defEncStyle) { 433 super.setDefaultEncodingStyle(defEncStyle); 434 if (parent != null) 435 parent.setDefaultEncodingStyle(defEncStyle); 436 } 437 438 442 public String getSchemaURI () { 443 return schemaURI; 444 } 445 446 450 public SOAPMappingRegistry getParent () { 451 return parent; 452 } 453 454 458 private void initializeRegistry(String schemaURI) 459 { 460 QName schemaQNames[] = null; 461 462 if (schemaURI.equals(Constants.NS_URI_1999_SCHEMA_XSD)) { 463 schemaQNames = schema1999QNames; 464 mapSchemaTypes(schema2000QNames, false); 465 mapSchemaTypes(schema2001QNames, false); 466 } else if (schemaURI.equals(Constants.NS_URI_2000_SCHEMA_XSD)) { 467 mapSchemaTypes(schema1999QNames, false); 468 schemaQNames = schema2000QNames; 469 mapSchemaTypes(schema2001QNames, false); 470 } else { 471 if (!schemaURI.equals(Constants.NS_URI_2001_SCHEMA_XSD)) { 472 System.err.println("WARNING: Unrecognized Schema URI '" + schemaURI + 473 "' specified. Defaulting to '" + 474 Constants.NS_URI_2001_SCHEMA_XSD + "'."); 475 } 476 mapSchemaTypes(schema1999QNames, false); 477 mapSchemaTypes(schema2000QNames, false); 478 schemaQNames = schema2001QNames; 479 } 480 481 mapSchemaTypes(schemaQNames, true); 483 484 mapTypes(soapEncURI, RPCConstants.Q_ELEM_PARAMETER, Parameter.class, 486 paramSer, paramSer); 487 488 mapTypes(soapEncURI, arrayQName, null, null, arraySer); 490 491 mapTypes(Constants.NS_URI_LITERAL_XML, RPCConstants.Q_ELEM_PARAMETER, 493 Parameter.class, xmlParamSer, xmlParamSer); 494 495 try { 496 Class XMISerializer = 497 Class.forName("org.apache.soap.util.xml.XMISerializer"); 498 Class XMIParameterSerializer = 499 Class.forName("org.apache.soap.encoding.xmi.XMIParameterSerializer"); 500 501 mapTypes(Constants.NS_URI_XMI_ENC, null, null, 503 (Serializer)XMISerializer.newInstance(), 504 (Deserializer)XMIParameterSerializer.newInstance()); 505 506 mapTypes(Constants.NS_URI_XMI_ENC, null, Parameter.class, 508 (Serializer)XMIParameterSerializer.newInstance(), null); 509 } catch (IllegalAccessException iae) { 510 } catch (InstantiationException ie) { 511 } catch (ClassNotFoundException cnfe) { 512 } catch (NoClassDefFoundError ncdfe) { 513 } 515 516 520 mapTypes(soapEncURI, new QName(Constants.NS_URI_XML_SOAP, "Vector"), 521 Vector.class, vectorSer, vectorSer); 522 mapTypes(soapEncURI, new QName(Constants.NS_URI_XML_SOAP, "Map"), 523 Hashtable.class, hashtableSer, hashtableSer); 524 525 try { 526 Class mapClass = Class.forName("java.util.Map"); 527 MapSerializer mapSer = new MapSerializer(); 528 529 mapTypes(soapEncURI, new QName(Constants.NS_URI_XML_SOAP, "Map"), 530 mapClass, mapSer, mapSer); 531 } catch (ClassNotFoundException cnfe) { 532 } catch (NoClassDefFoundError ncdfe) { 533 } 535 536 539 Base64Serializer base64Ser = new Base64Serializer(); 540 QName base64QName = new QName(Constants.NS_URI_2001_SCHEMA_XSD, "base64Binary"); 541 mapTypes(soapEncURI, base64QName, byte[].class, base64Ser, base64Ser); 542 base64QName = new QName(soapEncURI, "base64"); 543 mapTypes(soapEncURI, base64QName, byte[].class, base64Ser, base64Ser); 544 } 545 546 552 private void mapSchemaTypes(QName [] schemaQNames, boolean serialize) 553 { 554 for (int i = 0; i < schemaQNames.length; i++) { 555 QName qName = schemaQNames[i]; 556 Class cls = classes[i]; 557 Serializer ser = serialize ? serializers[i] : null; 558 Deserializer dser = deserializers[i]; 559 560 mapTypes(soapEncURI, qName, cls, ser, dser); 561 } 562 } 563 564 571 protected QName queryElementType_(Class javaType, String encodingStyleURI) 572 { 573 QName qn = super.queryElementType_(javaType, encodingStyleURI); 574 if (qn != null) { 575 return qn; 576 } 577 if (parent != null) { 578 qn = parent.queryElementType_(javaType, encodingStyleURI); 579 if (qn != null) { 580 return qn; 581 } 582 } 583 584 if (javaType != null 585 && (javaType.isArray()) 586 && encodingStyleURI != null 587 && encodingStyleURI.equals(soapEncURI)) { 588 return arrayQName; 589 } 590 591 return null; 592 } 593 594 601 protected Serializer querySerializer_(Class javaType, 602 String encodingStyleURI) 603 { 604 Serializer s = super.querySerializer_(javaType, encodingStyleURI); 605 if (s != null) { 606 return s; 607 } 608 if (parent != null) { 609 s = parent.querySerializer_(javaType, encodingStyleURI); 610 if (s != null) { 611 return s; 612 } 613 } 614 615 if (javaType != null 616 && encodingStyleURI != null 617 && encodingStyleURI.equals(soapEncURI)) { 618 if (javaType.isArray()) { 619 return arraySer; 620 } 621 } 622 623 return null; 624 } 625 626 630 protected Deserializer queryDeserializer_(QName elementType, 631 String encodingStyleURI) 632 { 633 Deserializer ds = super.queryDeserializer_(elementType, encodingStyleURI); 634 if (ds != null) { 635 return ds; 636 } 637 if (parent != null) { 638 ds = parent.queryDeserializer_(elementType, encodingStyleURI); 639 if (ds != null) { 640 return ds; 641 } 642 } 643 return null; 644 } 645 646 650 protected Class queryJavaType_(QName elementType, String encodingStyleURI) 651 { 652 Class jt = super.queryJavaType_(elementType, encodingStyleURI); 653 if (jt != null) { 654 return jt; 655 } 656 if (parent != null) { 657 jt = parent.queryJavaType_(elementType, encodingStyleURI); 658 if (jt != null) { 659 return jt; 660 } 661 } 662 return null; 663 } 664 } 665 | Popular Tags |