| 1 55 56 package org.jboss.axis.wsdl.fromJava; 57 58 import org.jboss.axis.AxisFault; 59 import org.jboss.axis.Constants; 60 import org.jboss.axis.InternalException; 61 import org.jboss.axis.description.ServiceDesc; 62 import org.jboss.axis.encoding.Serializer; 63 import org.jboss.axis.encoding.SerializerFactory; 64 import org.jboss.axis.encoding.SimpleType; 65 import org.jboss.axis.encoding.TypeMapping; 66 import org.jboss.axis.encoding.ser.BeanSerializerFactory; 67 import org.jboss.axis.encoding.ser.EnumSerializerFactory; 68 import org.jboss.axis.enums.Style; 69 import org.jboss.axis.utils.JavaUtils; 70 import org.jboss.axis.utils.Messages; 71 import org.jboss.axis.utils.XMLUtils; 72 import org.jboss.axis.wsdl.symbolTable.BaseTypeMapping; 73 import org.jboss.axis.wsdl.symbolTable.SymbolTable; 74 import org.jboss.logging.Logger; 75 import org.w3c.dom.Attr ; 76 import org.w3c.dom.Document ; 77 import org.w3c.dom.Element ; 78 import org.w3c.dom.NamedNodeMap ; 79 import org.w3c.dom.Node ; 80 import org.w3c.dom.NodeList ; 81 import org.xml.sax.SAXException ; 82 83 import javax.wsdl.Definition; 84 import javax.wsdl.WSDLException; 85 import javax.xml.namespace.QName ; 86 import javax.xml.parsers.ParserConfigurationException ; 87 import javax.xml.rpc.holders.Holder ; 88 import java.io.IOException ; 89 import java.lang.reflect.Field ; 90 import java.lang.reflect.Modifier ; 91 import java.net.URL ; 92 import java.util.ArrayList ; 93 import java.util.HashMap ; 94 import java.util.Iterator ; 95 import java.util.List ; 96 97 105 public class Types 106 { 107 private static Logger log = Logger.getLogger(Types.class.getName()); 108 109 Definition def; 110 Namespaces namespaces = null; 111 TypeMapping tm; 112 TypeMapping defaultTM; 113 String targetNamespace; 114 Element wsdlTypesElem = null; 115 HashMap schemaTypes = null; 116 HashMap schemaElementNames = null; 117 HashMap schemaUniqueElementNames = null; 118 HashMap wrapperMap = new HashMap (); 119 List stopClasses = null; 120 List beanCompatErrs = new ArrayList (); 121 ServiceDesc serviceDesc = null; 122 123 133 public Types(Definition def, 134 TypeMapping tm, 135 TypeMapping defaultTM, 136 Namespaces namespaces, 137 String targetNamespace, 138 List stopClasses, 139 ServiceDesc serviceDesc) 140 { 141 this.def = def; 142 this.serviceDesc = serviceDesc; 143 createDocumentFragment(); 144 this.tm = tm; 145 this.defaultTM = defaultTM; 146 this.namespaces = namespaces; 147 this.targetNamespace = targetNamespace; 148 this.stopClasses = stopClasses; 149 schemaElementNames = new HashMap (); 150 schemaUniqueElementNames = new HashMap (); 151 schemaTypes = new HashMap (); 152 } 153 154 157 public Namespaces getNamespaces() 158 { 159 return namespaces; 160 } 161 162 167 public void loadInputSchema(String inputSchema) 168 throws IOException , WSDLException, SAXException , 169 ParserConfigurationException  170 { 171 Document doc = XMLUtils.newDocument(inputSchema); 173 174 Element root = doc.getDocumentElement(); 176 if (root.getLocalName().equals("schema") && 177 Constants.isSchemaXSD(root.getNamespaceURI())) 178 { 179 Node schema = docHolder.importNode(root, true); 180 if (null == wsdlTypesElem) 181 { 182 writeWsdlTypesElement(); 183 } 184 wsdlTypesElem.appendChild(schema); 185 186 BaseTypeMapping btm = 188 new BaseTypeMapping() 189 { 190 public String getBaseName(QName qNameIn) 191 { 192 QName qName = new QName (qNameIn.getNamespaceURI(), 193 qNameIn.getLocalPart()); 194 Class cls = defaultTM.getClassForQName(qName); 195 if (cls == null) 196 return null; 197 else 198 return JavaUtils.getTextClassName(cls.getName()); 199 } 200 }; 201 SymbolTable symbolTable = new SymbolTable(btm, 202 true, false, false); 203 symbolTable.populateTypes(new URL (inputSchema), doc); 204 205 processSymTabEntries(symbolTable); 206 } 207 else 208 { 209 ; 212 } 213 } 214 215 222 private void processSymTabEntries(SymbolTable symbolTable) 223 { 224 Iterator iterator = symbolTable.getElementIndex().keySet().iterator(); 225 while (iterator.hasNext()) 226 { 227 QName name = (QName )iterator.next(); 228 addToElementsList(name); 229 } 230 iterator = symbolTable.getTypeIndex().keySet().iterator(); 231 while (iterator.hasNext()) 232 { 233 QName name = (QName )iterator.next(); 234 addToTypesList(name); 235 } 236 } 237 238 243 public void loadInputTypes(String inputWSDL) 244 throws IOException , WSDLException, 245 SAXException , ParserConfigurationException  246 { 247 248 Document doc = XMLUtils.newDocument(inputWSDL); 250 251 NodeList elements = doc.getChildNodes(); 253 if (elements.getLength() > 0 && 254 elements.item(0).getLocalName().equals("definitions")) 255 { 256 elements = elements.item(0).getChildNodes(); 257 for (int i = 0; 258 i < elements.getLength() && wsdlTypesElem == null; 259 i++) 260 { 261 Node node = elements.item(i); 262 if (node.getLocalName() != null && 263 node.getLocalName().equals("types")) 264 { 265 wsdlTypesElem = (Element )node; 266 } 267 } 268 } 269 270 if (wsdlTypesElem == null) 272 { 273 return; 274 } 275 276 wsdlTypesElem = 278 (Element )docHolder.importNode(wsdlTypesElem, true); 279 docHolder.appendChild(wsdlTypesElem); 280 281 BaseTypeMapping btm = 283 new BaseTypeMapping() 284 { 285 public String getBaseName(QName qNameIn) 286 { 287 QName qName = new QName (qNameIn.getNamespaceURI(), 288 qNameIn.getLocalPart()); 289 Class cls = defaultTM.getClassForQName(qName); 290 if (cls == null) 291 return null; 292 else 293 return JavaUtils.getTextClassName(cls.getName()); 294 } 295 }; 296 SymbolTable symbolTable = new SymbolTable(btm, 297 true, false, false); 298 symbolTable.populate(null, doc); 299 300 processSymTabEntries(symbolTable); 301 } 302 303 313 public QName writeTypeForPart(Class type, QName qname) throws AxisFault 314 { 315 323 if (type.getName().equals("void")) 324 { 325 return null; 326 } 327 328 if (Holder .class.isAssignableFrom(type)) 329 { 330 type = JavaUtils.getHolderValueType(type); 331 } 332 333 if (qname == null || 335 (Constants.isSOAP_ENC(qname.getNamespaceURI()) && 336 "Array".equals(qname.getLocalPart()))) 337 { 338 qname = getTypeQName(type); 339 if (qname == null) 340 { 341 throw new AxisFault("Class:" + type.getName()); 342 } 343 } 344 345 if (!makeTypeElement(type, qname, null)) 346 { 347 qname = Constants.XSD_ANYTYPE; 348 } 349 350 return qname; 351 } 352 353 361 public QName writeElementForPart(Class type, QName qname) throws AxisFault 362 { 363 371 if (type.getName().equals("void")) 372 { 373 return null; 374 } 375 376 if (Holder .class.isAssignableFrom(type)) 377 { 378 type = JavaUtils.getHolderValueType(type); 379 } 380 381 if (qname == null || 383 (Constants.isSOAP_ENC(qname.getNamespaceURI()) && 384 "Array".equals(qname.getLocalPart()))) 385 { 386 qname = getTypeQName(type); 387 if (qname == null) 388 { 389 throw new AxisFault("Class:" + type.getName()); 390 } 391 } 392 393 String nsURI = qname.getNamespaceURI(); 395 if (Constants.isSchemaXSD(nsURI) || 396 (Constants.isSOAP_ENC(nsURI) && 397 !"Array".equals(qname.getLocalPart()))) 398 { 399 return null; 400 } 401 402 if (wsdlTypesElem == null) 404 { 405 writeWsdlTypesElement(); 406 } 407 408 if (writeTypeAsElement(type, qname) == null) 410 { 411 qname = null; 412 } 413 return qname; 414 } 415 416 433 public Element writeWrapperElement(QName qname, 434 boolean request, 435 boolean hasParams) throws AxisFault 436 { 437 if (wsdlTypesElem == null) 439 { 440 writeWsdlTypesElement(); 441 } 442 443 writeTypeNamespace(qname); 445 446 Element wrapperElement = docHolder.createElement("element"); 448 writeSchemaElement(qname, wrapperElement); 449 wrapperElement.setAttribute("name", qname.getLocalPart()); 450 451 Element complexType = docHolder.createElement("complexType"); 453 wrapperElement.appendChild(complexType); 454 455 if (hasParams) 458 { 459 Element sequence = docHolder.createElement("sequence"); 460 complexType.appendChild(sequence); 461 return sequence; 462 } 463 464 return null; 465 } 466 467 475 public void writeWrappedParameter(Element sequence, String name, 476 QName type, Class javaType) 477 throws AxisFault 478 { 479 480 if (javaType == void.class) 481 { 482 return; 483 } 484 485 if (type == null) 486 { 487 type = writeTypeForPart(javaType, type); 488 } 489 490 if (type == null) 491 { 492 } 494 495 Element childElem; 496 if (isAnonymousType(type)) 497 { 498 childElem = createElementWithAnonymousType(name, javaType, 499 false, docHolder); 500 } 501 else 502 { 503 childElem = docHolder.createElement("element"); 505 childElem.setAttribute("name", name); 506 507 String prefix = namespaces.getCreatePrefix(type.getNamespaceURI()); 508 String prefixedName = prefix + ":" + type.getLocalPart(); 509 childElem.setAttribute("type", prefixedName); 510 } 511 sequence.appendChild(childElem); 512 } 513 514 private boolean isAnonymousType(QName type) 515 { 516 return type.getLocalPart().indexOf(SymbolTable.ANON_TOKEN) != -1; 517 } 518 519 525 private QName writeTypeAsElement(Class type, QName qName) throws AxisFault 526 { 527 if (qName == null || 528 Constants.equals(Constants.SOAP_ARRAY, qName)) 529 { 530 qName = getTypeQName(type); 531 } 532 QName typeQName = writeTypeNamespace(type, qName); 533 534 String elementType = writeType(type, qName); 535 if (elementType != null) 536 { 537 Element element = createElementDecl(qName.getLocalPart(), type, qName, isNullable(type), false); 538 if (element != null) 539 writeSchemaElement(typeQName, element); 540 return qName; 541 } 542 return null; 543 } 544 545 553 private QName writeTypeNamespace(Class type, QName qName) 554 { 555 if (qName == null) 556 { 557 qName = getTypeQName(type); 558 } 559 writeTypeNamespace(qName); 560 return qName; 561 } 562 563 568 private void writeTypeNamespace(QName qName) 569 { 570 if (qName != null && !qName.getNamespaceURI().equals("")) 571 { 572 String pref = def.getPrefix(qName.getNamespaceURI()); 573 if (pref == null) 574 def.addNamespace(namespaces.getCreatePrefix(qName.getNamespaceURI()), 575 qName.getNamespaceURI()); 576 577 } 578 } 579 580 586 public QName getTypeQName(Class javaType) 587 { 588 QName qName = null; 589 590 QName dQName = null; 592 if (defaultTM != null) 593 { 594 dQName = defaultTM.getTypeQName(javaType); 595 } 596 if (tm != null) 597 { 598 qName = tm.getTypeQName(javaType); 599 } 600 if (qName == null) 601 { 602 qName = dQName; 603 } 604 else if (qName != null && qName != dQName) 605 { 606 if (Constants.isSchemaXSD(qName.getNamespaceURI())) 610 { 611 qName = dQName; 612 } 613 } 614 615 if (javaType.isArray() && 619 qName != null && 620 Constants.equals(Constants.SOAP_ARRAY, qName)) 621 { 622 Class componentType = javaType.getComponentType(); 623 QName cqName = getTypeQName(componentType); 628 if (targetNamespace.equals(cqName.getNamespaceURI())) 629 { 630 qName = new QName (targetNamespace, 631 "ArrayOf" + cqName.getLocalPart()); 632 } 633 else 634 { 635 String pre = namespaces.getCreatePrefix(cqName.getNamespaceURI()); 636 qName = new QName (targetNamespace, 637 "ArrayOf_" + pre + "_" + cqName.getLocalPart()); 638 } 639 return qName; 640 } 641 642 if (qName == null) 645 { 646 String pkg = getPackageNameFromFullName(javaType.getName()); 647 String lcl = getLocalNameFromFullName(javaType.getName()); 648 649 String ns = namespaces.getCreate(pkg); 650 namespaces.getCreatePrefix(ns); 651 String localPart = lcl.replace('$', '_'); 652 qName = new QName (ns, localPart); 653 } 654 655 return qName; 656 } 657 658 666 public String getQNameString(QName qname) 667 { 668 String prefix = namespaces.getCreatePrefix(qname.getNamespaceURI()); 669 return prefix + ":" + qname.getLocalPart(); 670 } 671 672 678 public static String getPackageNameFromFullName(String full) 679 { 680 if (full.lastIndexOf('.') < 0) 681 return ""; 682 else 683 return full.substring(0, full.lastIndexOf('.')); 684 } 685 686 692 public static String getLocalNameFromFullName(String full) 693 { 694 if (full.lastIndexOf('.') < 0) 695 return full; 696 else 697 return full.substring(full.lastIndexOf('.') + 1); 698 } 699 700 707 public void writeSchemaElement(QName qName, Element element) 708 throws AxisFault 709 { 710 if (wsdlTypesElem == null) 711 { 712 try 713 { 714 writeWsdlTypesElement(); 715 } 716 catch (Exception e) 717 { 718 log.error(e); 719 return; 720 } 721 } 722 String namespaceURI = qName.getNamespaceURI(); 723 if (namespaceURI == null || namespaceURI.equals("")) 724 { 725 throw new AxisFault(Constants.FAULT_SERVER_GENERAL, 726 Messages.getMessage("noNamespace00", 727 qName.toString()), 728 null, null); 729 } 730 731 Element schemaElem = null; 732 NodeList nl = wsdlTypesElem.getChildNodes(); 733 for (int i = 0; i < nl.getLength(); i++) 734 { 735 NamedNodeMap attrs = nl.item(i).getAttributes(); 736 if (attrs != null) 737 { 738 for (int n = 0; n < attrs.getLength(); n++) 739 { 740 Attr a = (Attr )attrs.item(n); 741 if (a.getName().equals("targetNamespace") && 742 a.getValue().equals(namespaceURI)) 743 schemaElem = (Element )nl.item(i); 744 } 745 } 746 } 747 if (schemaElem == null) 748 { 749 schemaElem = docHolder.createElement("schema"); 750 wsdlTypesElem.appendChild(schemaElem); 751 schemaElem.setAttribute("xmlns", Constants.URI_DEFAULT_SCHEMA_XSD); 752 schemaElem.setAttribute("targetNamespace", namespaceURI); 753 754 if (serviceDesc.getStyle() == Style.RPC) 756 { 757 Element importElem = docHolder.createElement("import"); 758 schemaElem.appendChild(importElem); 759 importElem.setAttribute("namespace", Constants.URI_DEFAULT_SOAP_ENC); 760 } 761 762 writeTypeNamespace(qName); 763 } 764 schemaElem.appendChild(element); 765 } 766 767 770 private void writeWsdlTypesElement() 771 { 772 if (wsdlTypesElem == null) 773 { 774 wsdlTypesElem = 776 docHolder.createElementNS(Constants.NS_URI_WSDL11, "types"); 777 wsdlTypesElem.setPrefix(Constants.NS_PREFIX_WSDL); 778 } 779 } 780 781 791 public String writeType(Class type) throws AxisFault 792 { 793 return writeType(type, null); 794 } 795 796 807 public String writeType(Class type, QName qName) throws AxisFault 808 { 809 if (qName == null || 811 Constants.equals(Constants.SOAP_ARRAY, qName)) 812 { 813 qName = getTypeQName(type); 814 } 815 816 if (!makeTypeElement(type, qName, null)) 817 { 818 return null; 819 } 820 return getQNameString(qName); 821 } 822 823 public Element createArrayElement(String componentTypeName) 824 { 825 Element complexType = docHolder.createElement("complexType"); 827 828 Element complexContent = docHolder.createElement("complexContent"); 829 complexType.appendChild(complexContent); 830 831 Element restriction = docHolder.createElement("restriction"); 832 complexContent.appendChild(restriction); 833 restriction.setAttribute("base", 834 Constants.NS_PREFIX_SOAP_ENC + ":Array"); 835 836 Element attribute = docHolder.createElement("attribute"); 837 restriction.appendChild(attribute); 838 attribute.setAttribute("ref", 839 Constants.NS_PREFIX_SOAP_ENC + ":arrayType"); 840 attribute.setAttribute(Constants.NS_PREFIX_WSDL + ":arrayType", 841 componentTypeName); 842 843  
|