1 55 56 package org.jboss.axis.encoding; 57 58 import org.jboss.axis.Constants; 59 import org.jboss.axis.encoding.ser.BeanDeserializerFactory; 60 import org.jboss.axis.encoding.ser.BeanSerializerFactory; 61 import org.jboss.axis.utils.ClassUtils; 62 import org.jboss.axis.utils.Messages; 63 import org.jboss.logging.Logger; 64 65 import javax.xml.namespace.QName ; 66 import javax.xml.rpc.JAXRPCException ; 67 import java.util.ArrayList ; 68 import java.util.HashMap ; 69 import java.util.List ; 70 71 99 public class TypeMappingImpl implements TypeMapping 100 { 101 private static Logger log = Logger.getLogger(TypeMappingImpl.class.getName()); 102 103 public class Pair 104 { 105 public Class javaType; 106 public QName xmlType; 107 108 public Pair(Class javaType, QName xmlType) 109 { 110 this.javaType = javaType; 111 this.xmlType = xmlType; 112 } 113 114 public boolean equals(Object o) 115 { 116 if (o == null) return false; 117 Pair p = (Pair)o; 118 if (p.xmlType == this.xmlType && 120 p.javaType == this.javaType) 121 { 122 return true; 123 } 124 return (p.xmlType.equals(this.xmlType) && 125 p.javaType.equals(this.javaType)); 126 } 127 128 public int hashCode() 129 { 130 int hashcode = 0; 131 if (javaType != null) 132 { 133 hashcode ^= javaType.hashCode(); 134 } 135 if (xmlType != null) 136 { 137 hashcode ^= xmlType.hashCode(); 138 } 139 return hashcode; 140 } 141 } 142 143 private HashMap qName2Pair; private HashMap class2Pair; private HashMap pair2SF; private HashMap pair2DF; protected TypeMapping delegate; private ArrayList namespaces; 150 154 private boolean doAutoTypes = false; 155 156 159 public TypeMappingImpl(TypeMapping delegate) 160 { 161 qName2Pair = new HashMap (); 162 class2Pair = new HashMap (); 163 pair2SF = new HashMap (); 164 pair2DF = new HashMap (); 165 this.delegate = delegate; 166 namespaces = new ArrayList (); 167 } 168 169 172 public void setDelegate(TypeMapping delegate) 173 { 174 this.delegate = delegate; 175 } 176 177 180 public TypeMapping getDelegate() 181 { 182 return delegate; 183 } 184 185 186 187 193 public String [] getSupportedEncodings() 194 { 195 String [] stringArray = new String [namespaces.size()]; 196 return (String [])namespaces.toArray(stringArray); 197 } 198 199 206 public void setSupportedEncodings(String [] namespaceURIs) 207 { 208 namespaces.clear(); 209 for (int i = 0; i < namespaceURIs.length; i++) 210 { 211 if (!namespaces.contains(namespaceURIs[i])) 212 { 213 namespaces.add(namespaceURIs[i]); 214 } 215 } 216 } 217 218 234 public boolean isRegistered(Class javaType, QName xmlType) 235 { 236 if (javaType == null || xmlType == null) 237 { 238 throw new JAXRPCException (Messages.getMessage(javaType == null ? 241 "badJavaType" : "badXmlType")); 242 } 243 if (pair2SF.keySet().contains(new Pair(javaType, xmlType))) 244 { 245 return true; 246 } 247 if (delegate != null) 248 { 249 return delegate.isRegistered(javaType, xmlType); 250 } 251 return false; 252 } 253 254 264 public void register(Class javaType, QName xmlType, 265 javax.xml.rpc.encoding.SerializerFactory sf, 266 javax.xml.rpc.encoding.DeserializerFactory dsf) 267 throws JAXRPCException 268 { 269 if (sf == null && dsf == null) 271 { 272 throw new JAXRPCException (Messages.getMessage("badSerFac")); 273 } 274 275 internalRegister(javaType, xmlType, sf, dsf); 276 } 277 278 287 protected void internalRegister(Class javaType, QName xmlType, 288 javax.xml.rpc.encoding.SerializerFactory sf, 289 javax.xml.rpc.encoding.DeserializerFactory dsf) 290 throws JAXRPCException 291 { 292 if (javaType == null || xmlType == null) 294 { 295 throw new JAXRPCException (Messages.getMessage(javaType == null ? 296 "badJavaType" : "badXmlType")); 297 } 298 299 309 Pair pair = new Pair(javaType, xmlType); 310 311 if ((dsf != null) || (qName2Pair.get(xmlType) == null)) 313 qName2Pair.put(xmlType, pair); 314 if ((sf != null) || (class2Pair.get(javaType) == null)) 315 class2Pair.put(javaType, pair); 316 317 if (sf != null) 318 pair2SF.put(pair, sf); 319 if (dsf != null) 320 pair2DF.put(pair, dsf); 321 } 322 323 335 public javax.xml.rpc.encoding.SerializerFactory 336 getSerializer(Class javaType, QName xmlType) 337 throws JAXRPCException 338 { 339 340 javax.xml.rpc.encoding.SerializerFactory sf = null; 341 342 if (xmlType == null) 344 { 345 xmlType = getTypeQName(javaType); 346 if (xmlType == null) 349 { 350 return null; 351 } 352 353 if (doAutoTypes && 356 xmlType.getNamespaceURI().equals(Constants.NS_URI_JAVA)) 357 { 358 return new BeanSerializerFactory(javaType, xmlType); 359 } 360 } 361 362 Pair pair = new Pair(javaType, xmlType); 364 365 sf = (javax.xml.rpc.encoding.SerializerFactory )pair2SF.get(pair); 367 368 if (sf == null) 372 { 373 if (javaType.isArray()) 374 { 375 pair = (Pair)qName2Pair.get(Constants.SOAP_ARRAY); 376 } 377 else 378 { 379 pair = (Pair)class2Pair.get(pair.javaType); 380 } 381 if (pair != null) 382 { 383 sf = (javax.xml.rpc.encoding.SerializerFactory )pair2SF.get(pair); 384 } 385 } 386 387 if (sf == null && delegate != null) 388 { 389 sf = delegate.getSerializer(javaType, xmlType); 390 } 391 return sf; 392 } 393 394 411 public QName getXMLType(Class javaType, QName xmlType) 412 throws JAXRPCException 413 { 414 javax.xml.rpc.encoding.SerializerFactory sf = null; 415 416 if (xmlType == null) 418 { 419 xmlType = getTypeQNameRecursive(javaType); 420 421 if (xmlType == null) 424 { 425 return null; 426 } 427 428 if (doAutoTypes && 430 xmlType.getNamespaceURI().equals(Constants.NS_URI_JAVA)) 431 { 432 return xmlType; 433 } 434 } 435 436 Pair pair = new Pair(javaType, xmlType); 438 439 sf = (javax.xml.rpc.encoding.SerializerFactory )pair2SF.get(pair); 441 442 if (sf == null) 446 { 447 if (javaType.isArray()) 448 { 449 pair = (Pair)qName2Pair.get(pair.xmlType); 450 } 451 else 452 { 453 pair = (Pair)class2Pair.get(pair.javaType); 454 } 455 if (pair != null) 456 { 457 sf = (javax.xml.rpc.encoding.SerializerFactory )pair2SF.get(pair); 458 } 459 } 460 461 if (sf == null && delegate != null) 462 { 463 return ((TypeMappingImpl)delegate).getXMLType(javaType, xmlType); 464 } 465 466 if (pair != null) 467 { 468 xmlType = pair.xmlType; 469 } 470 471 return xmlType; 472 } 473 474 486 public javax.xml.rpc.encoding.DeserializerFactory 487 getDeserializer(Class javaType, QName xmlType) 488 throws JAXRPCException 489 { 490 javax.xml.rpc.encoding.DeserializerFactory df = null; 491 492 if (javaType == null) 493 { 494 javaType = getClassForQName(xmlType); 495 if (javaType == null) 498 { 499 return null; 500 } 501 502 if (doAutoTypes && 503 Constants.NS_URI_JAVA.equals(xmlType.getNamespaceURI())) 504 { 505 try 506 { 507 javaType = ClassUtils.forName(xmlType.getLocalPart()); 508 } 509 catch (ClassNotFoundException e) 510 { 511 return null; 512 } 513 return new BeanDeserializerFactory(javaType, xmlType); 514 } 515 } 516 517 Pair pair = new Pair(javaType, xmlType); 518 519 df = (javax.xml.rpc.encoding.DeserializerFactory )pair2DF.get(pair); 520 521 if (df == null && delegate != null) 522 { 523 df = delegate.getDeserializer(javaType, xmlType); 524 } 525 return df; 526 } 527 528 537 public void removeSerializer(Class javaType, QName xmlType) 538 throws JAXRPCException 539 { 540 if (javaType == null || xmlType == null) 541 { 542 throw new JAXRPCException (Messages.getMessage(javaType == null ? 543 "badJavaType" : "badXmlType")); 544 } 545 546 Pair pair = new Pair(javaType, xmlType); 547 pair2SF.remove(pair); 548 } 549 550 559 public void removeDeserializer(Class javaType, QName xmlType) 560 throws JAXRPCException 561 { 562 if (javaType == null || xmlType == null) 563 { 564 throw new JAXRPCException (Messages.getMessage(javaType == null ? 565 "badJavaType" : "badXmlType")); 566 } 567 Pair pair = new Pair(javaType, xmlType); 568 pair2DF.remove(pair); 569 } 570 571 572 573 574 580 public QName getTypeQNameRecursive(Class javaType) 581 { 582 QName ret = null; 583 while (javaType != null) 584 { 585 ret = getTypeQName(javaType); 586 if (ret != null) 587 return ret; 588 589 Class [] interfaces = javaType.getInterfaces(); 591 if (interfaces != null) 592 { 593 for (int i = 0; i < interfaces.length; i++) 594 { 595 Class iface = interfaces[i]; 596 ret = getTypeQName(iface); 597 if (ret != null) 598 return ret; 599 } 600 } 601 602 javaType = javaType.getSuperclass(); 603 } 604 return null; 605 } 606 607 public QName getTypeQName(Class javaType) 608 { 609 if (javaType == null) 611 return null; 612 613 QName xmlType = null; 614 Pair pair = (Pair)class2Pair.get(javaType); 615 if (pair == null && delegate != null) 616 { 617 xmlType = delegate.getTypeQName(javaType); 618 } 619 else if (pair != null) 620 { 621 xmlType = pair.xmlType; 622 } 623 624 if (xmlType == null && doAutoTypes) 625 { 626 xmlType = new QName (Constants.NS_URI_JAVA, 627 javaType.getName()); 628 } 629 630 if (xmlType == null && (javaType.isArray() || 632 javaType == List .class || 633 List .class.isAssignableFrom(javaType))) 634 { 635 636 pair = (Pair)class2Pair.get(Object [].class); 638 if (pair != null) 642 { 643 xmlType = pair.xmlType; 644 } 645 else 646 { 647 xmlType = Constants.SOAP_ARRAY; 648 } 649 } 650 651 return xmlType; 653 } 654 655 661 public Class getClassForQName(QName xmlType) 662 { 663 if (xmlType == null) 664 return null; 665 666 Class javaType = null; 667 Pair pair = (Pair)qName2Pair.get(xmlType); 668 if (pair == null && delegate != null) 669 { 670 javaType = delegate.getClassForQName(xmlType); 671 } 672 else if (pair != null) 673 { 674 javaType = pair.javaType; 675 } 676 677 if (javaType == null && doAutoTypes && 678 Constants.NS_URI_JAVA.equals(xmlType.getNamespaceURI())) 679 { 680 try 682 { 683 javaType = ClassUtils.forName(xmlType.getLocalPart()); 684 } 685 catch (ClassNotFoundException e) 686 { 687 } 688 } 689 690 return javaType; 691 } 692 693 703 public javax.xml.rpc.encoding.SerializerFactory 704 getSerializer(Class javaType) 705 throws JAXRPCException 706 { 707 return getSerializer(javaType, null); 708 } 709 710 720 public javax.xml.rpc.encoding.DeserializerFactory 721 getDeserializer(QName xmlType) 722 throws JAXRPCException 723 { 724 return getDeserializer(null, xmlType); 725 } 726 727 public void setDoAutoTypes(boolean doAutoTypes) 728 { 729 this.doAutoTypes = doAutoTypes; 730 } 731 732 735 public Class [] getAllClasses() 736 { 737 java.util.HashSet temp = new java.util.HashSet (); 738 if (delegate != null) 739 { 740 temp.addAll(java.util.Arrays.asList(delegate.getAllClasses())); 741 } 742 temp.addAll(class2Pair.keySet()); 743 return (Class [])temp.toArray(new Class [temp.size()]); 744 } 745 } 746 | Popular Tags |