1 16 package org.apache.axis.wsdl.toJava; 17 18 import org.apache.axis.components.logger.LogFactory; 19 import org.apache.axis.utils.JavaUtils; 20 import org.apache.axis.utils.Messages; 21 import org.apache.axis.wsdl.gen.Generator; 22 import org.apache.axis.wsdl.gen.GeneratorFactory; 23 import org.apache.axis.wsdl.gen.NoopGenerator; 24 import org.apache.axis.wsdl.symbolTable.BaseTypeMapping; 25 import org.apache.axis.wsdl.symbolTable.BindingEntry; 26 import org.apache.axis.wsdl.symbolTable.ContainedAttribute; 27 import org.apache.axis.wsdl.symbolTable.Element; 28 import org.apache.axis.wsdl.symbolTable.ElementDecl; 29 import org.apache.axis.wsdl.symbolTable.FaultInfo; 30 import org.apache.axis.wsdl.symbolTable.MessageEntry; 31 import org.apache.axis.wsdl.symbolTable.Parameter; 32 import org.apache.axis.wsdl.symbolTable.Parameters; 33 import org.apache.axis.wsdl.symbolTable.PortTypeEntry; 34 import org.apache.axis.wsdl.symbolTable.SchemaUtils; 35 import org.apache.axis.wsdl.symbolTable.ServiceEntry; 36 import org.apache.axis.wsdl.symbolTable.SymTabEntry; 37 import org.apache.axis.wsdl.symbolTable.SymbolTable; 38 import org.apache.axis.wsdl.symbolTable.Type; 39 import org.apache.axis.wsdl.symbolTable.TypeEntry; 40 import org.apache.commons.logging.Log; 41 42 import javax.wsdl.Binding; 43 import javax.wsdl.Definition; 44 import javax.wsdl.Fault; 45 import javax.wsdl.Message; 46 import javax.wsdl.Operation; 47 import javax.wsdl.OperationType; 48 import javax.wsdl.Port; 49 import javax.wsdl.PortType; 50 import javax.wsdl.Service; 51 import javax.xml.namespace.QName ; 52 import javax.xml.rpc.holders.BooleanHolder ; 53 import java.io.IOException ; 54 import java.lang.reflect.Constructor ; 55 import java.util.ArrayList ; 56 import java.util.HashMap ; 57 import java.util.HashSet ; 58 import java.util.Iterator ; 59 import java.util.List ; 60 import java.util.Map ; 61 import java.util.Vector ; 62 63 66 public class JavaGeneratorFactory implements GeneratorFactory { 67 private static final Log log_ = 68 LogFactory.getLog(JavaGeneratorFactory.class.getName()); 69 70 71 protected Emitter emitter; 72 73 74 protected SymbolTable symbolTable; 75 76 77 public static String COMPLEX_TYPE_FAULT = "ComplexTypeFault"; 78 79 80 public static String EXCEPTION_CLASS_NAME = "ExceptionClassName"; 81 82 83 public static String EXCEPTION_DATA_TYPE = "ExceptionDataType"; 84 85 86 private static final String SERVICE_SUFFIX = "_Service"; 87 private static final String PORT_TYPE_SUFFIX = "_PortType"; private static final String TYPE_SUFFIX = "_Type"; 89 private static final String ELEMENT_SUFFIX = "_Element"; private static final String EXCEPTION_SUFFIX = "_Exception"; 91 private static final String BINDING_SUFFIX = "_Binding"; 92 93 94 98 public JavaGeneratorFactory() { 99 addGenerators(); 100 } 102 107 public JavaGeneratorFactory(Emitter emitter) { 108 109 this.emitter = emitter; 110 111 addGenerators(); 112 } 114 119 public void setEmitter(Emitter emitter) { 120 this.emitter = emitter; 121 } 123 126 private void addGenerators() { 127 128 addMessageGenerators(); 129 addPortTypeGenerators(); 130 addBindingGenerators(); 131 addServiceGenerators(); 132 addTypeGenerators(); 133 addDefinitionGenerators(); 134 } 136 147 protected void addMessageGenerators() { 148 } 150 153 protected void addPortTypeGenerators() { 154 } 156 159 protected void addBindingGenerators() { 160 } 162 165 protected void addServiceGenerators() { 166 } 168 171 protected void addTypeGenerators() { 172 } 174 177 protected void addDefinitionGenerators() { 178 179 addGenerator(Definition.class, JavaDefinitionWriter.class); addGenerator(Definition.class, 181 JavaDeployWriter.class); addGenerator(Definition.class, 183 JavaUndeployWriter.class); addGenerator(Definition.class, 185 JavaBuildFileWriter.class); 187 } 189 190 198 public void generatorPass(Definition def, SymbolTable symbolTable) { 199 200 this.symbolTable = symbolTable; 201 202 javifyNames(symbolTable); 203 setFaultContext(symbolTable); 204 resolveNameClashes(symbolTable); 205 determineInterfaceNames(symbolTable); 206 207 if (emitter.isAllWanted()) { 208 setAllReferencesToTrue(); 209 } else { 210 ignoreNonSOAPBindings(symbolTable); 211 } 212 213 constructSignatures(symbolTable); 214 determineIfHoldersNeeded(symbolTable); 215 } 217 218 private Writers messageWriters = new Writers(); 219 220 227 public Generator getGenerator(Message message, SymbolTable symbolTable) { 228 if (include(message.getQName())) { 229 MessageEntry mEntry = symbolTable.getMessageEntry(message.getQName()); 230 messageWriters.addStuff(new NoopGenerator(), mEntry, symbolTable); 231 return messageWriters; 232 } 233 else { 234 return new NoopGenerator(); 235 } 236 } 238 239 private Writers portTypeWriters = new Writers(); 240 241 248 public Generator getGenerator(PortType portType, SymbolTable symbolTable) { 249 if (include(portType.getQName())) { 250 PortTypeEntry ptEntry = 251 symbolTable.getPortTypeEntry(portType.getQName()); 252 portTypeWriters.addStuff(new NoopGenerator(), ptEntry, symbolTable); 253 return portTypeWriters; 254 } 255 else { 256 return new NoopGenerator(); 257 } 258 } 260 261 protected Writers bindingWriters = new Writers(); 262 263 270 public Generator getGenerator(Binding binding, SymbolTable symbolTable) { 271 if (include(binding.getQName())) { 272 Generator writer = new JavaBindingWriter(emitter, binding, 273 symbolTable); 274 BindingEntry bEntry = symbolTable.getBindingEntry(binding.getQName()); 275 bindingWriters.addStuff(writer, bEntry, symbolTable); 276 return bindingWriters; 277 } 278 else { 279 return new NoopGenerator(); 280 } 281 } 283 284 protected Writers serviceWriters = new Writers(); 285 286 293 public Generator getGenerator(Service service, SymbolTable symbolTable) { 294 if (include(service.getQName())) { 295 Generator writer = new JavaServiceWriter(emitter, service, 296 symbolTable); 297 ServiceEntry sEntry = symbolTable.getServiceEntry(service.getQName()); 298 serviceWriters.addStuff(writer, sEntry, symbolTable); 299 return serviceWriters; 300 } 301 else { 302 return new NoopGenerator(); 303 } 304 } 306 307 private Writers typeWriters = new Writers(); 308 309 316 public Generator getGenerator(TypeEntry type, SymbolTable symbolTable) { 317 if (include(type.getQName())) { 318 Generator writer = new JavaTypeWriter(emitter, type, symbolTable); 319 typeWriters.addStuff(writer, type, symbolTable); 320 return typeWriters; 321 } 322 else { 323 return new NoopGenerator(); 324 } 325 } 327 328 private Writers defWriters = new Writers(); 329 330 337 public Generator getGenerator(Definition definition, 338 SymbolTable symbolTable) { 339 if (include(definition.getQName())) { 340 defWriters.addStuff(null, definition, symbolTable); 341 return defWriters; 342 } 343 else { 344 return new NoopGenerator(); 345 } 346 } 348 350 355 protected class Writers implements Generator { 356 357 358 Vector writers = new Vector (); 359 360 361 SymbolTable symbolTable = null; 362 363 364 Generator baseWriter = null; 365 366 368 369 SymTabEntry entry = null; 370 371 372 Definition def = null; 373 374 379 public void addGenerator(Class writer) { 380 writers.add(writer); 381 } 383 390 public void addStuff(Generator baseWriter, SymTabEntry entry, 391 SymbolTable symbolTable) { 392 393 this.baseWriter = baseWriter; 394 this.entry = entry; 395 this.symbolTable = symbolTable; 396 } 398 405 public void addStuff(Generator baseWriter, Definition def, 406 SymbolTable symbolTable) { 407 408 this.baseWriter = baseWriter; 409 this.def = def; 410 this.symbolTable = symbolTable; 411 } 413 418 public void generate() throws IOException { 419 420 if (baseWriter != null) { 421 baseWriter.generate(); 422 } 423 424 Class [] formalArgs; 425 Object [] actualArgs; 426 427 if (entry != null) { 428 formalArgs = new Class []{Emitter.class, entry.getClass(), 429 SymbolTable.class}; 430 actualArgs = new Object []{emitter, entry, symbolTable}; 431 } else { 432 formalArgs = new Class []{Emitter.class, Definition.class, 433 SymbolTable.class}; 434 actualArgs = new Object []{emitter, def, symbolTable}; 435 } 436 437 for (int i = 0; i < writers.size(); ++i) { 438 Class wClass = (Class ) writers.get(i); 439 Generator gen; 440 441 try { 442 Constructor ctor = wClass.getConstructor(formalArgs); 443 444 gen = (Generator) ctor.newInstance(actualArgs); 445 } catch (Throwable t) { 446 throw new IOException (Messages.getMessage("exception01", 447 t.getMessage())); 448 } 449 450 gen.generate(); 451 } 452 } } 455 461 public void addGenerator(Class wsdlClass, Class generator) { 462 463 if (Message.class.isAssignableFrom(wsdlClass)) { 465 messageWriters.addGenerator(generator); 466 } else if (PortType.class.isAssignableFrom(wsdlClass)) { 467 portTypeWriters.addGenerator(generator); 468 } else if (Binding.class.isAssignableFrom(wsdlClass)) { 469 bindingWriters.addGenerator(generator); 470 } else if (Service.class.isAssignableFrom(wsdlClass)) { 471 serviceWriters.addGenerator(generator); 472 } else if (TypeEntry.class.isAssignableFrom(wsdlClass)) { 473 typeWriters.addGenerator(generator); 474 } else if (Definition.class.isAssignableFrom(wsdlClass)) { 475 defWriters.addGenerator(generator); 476 } 477 } 479 486 protected void javifyNames(SymbolTable symbolTable) { 487 488 int uniqueNum = 0; 489 HashMap anonQNames = new HashMap (); 490 Iterator it = symbolTable.getHashMap().values().iterator(); 491 492 while (it.hasNext()) { 493 Vector v = (Vector ) it.next(); 494 495 for (int i = 0; i < v.size(); ++i) { 496 SymTabEntry entry = (SymTabEntry) v.elementAt(i); 497 498 if (entry.getName() != null) { 499 continue; 500 } 501 502 if (entry instanceof TypeEntry) { 504 uniqueNum = javifyTypeEntryName(symbolTable, (TypeEntry) entry, anonQNames, uniqueNum); 505 } 506 507 else { 510 entry.setName(emitter.getJavaName(entry.getQName())); 511 } 512 } 513 } 514 } 516 517 protected int javifyTypeEntryName(SymbolTable symbolTable, TypeEntry entry, HashMap anonQNames, int uniqueNum) { 518 TypeEntry tEntry = entry; 519 String dims = tEntry.getDimensions(); 520 TypeEntry refType = tEntry.getRefType(); 521 while (refType != null) { 522 tEntry = refType; 523 dims += tEntry.getDimensions(); 524 refType = tEntry.getRefType(); 525 } 526 527 TypeEntry te = tEntry; 528 while (te != null) { 529 TypeEntry base = SchemaUtils.getBaseType(te, symbolTable); 530 if (base == null) 531 break; 532 533 uniqueNum = javifyTypeEntryName(symbolTable, base, anonQNames, uniqueNum); 534 535 if (Utils.getEnumerationBaseAndValues(te.getNode(), symbolTable) == null 536 &&SchemaUtils.getComplexElementExtensionBase(te.getNode(), symbolTable) == null 537 && te.getContainedAttributes() == null) { 538 if(!SchemaUtils.isSimpleTypeWithUnion(te.getNode())) { 539 if (base.isSimpleType()) { 540 te.setSimpleType(true); 546 te.setName(base.getName()); 547 te.setRefType(base); 548 } 549 550 if (base.isBaseType()) { 551 te.setBaseType(true); 557 te.setName(base.getName()); 558 te.setRefType(base); 559 } 560 } 561 } 562 563 if (!te.isSimpleType()) 564 break; 565 566 te = base; 567 } 568 569 if (tEntry.getName() == null) { 572 boolean processed = false; QName typeQName = tEntry.getQName(); 576 577 QName itemType = SchemaUtils.getListItemType(tEntry.getNode()); 580 if (itemType != null) { 581 TypeEntry itemEntry = symbolTable.getTypeEntry(itemType, false); 583 javifyTypeEntryName(symbolTable, itemEntry, anonQNames, uniqueNum); 586 TypeEntry refedEntry = itemEntry.getRefType(); 588 QName baseName = refedEntry == null ? itemEntry.getQName() : 589 refedEntry.getQName(); 590 typeQName = new QName (baseName.getNamespaceURI(), 591 baseName.getLocalPart() + "[]"); 592 } 593 594 if (emitter.isDeploy()) { 595 Class class1 = (Class ) emitter.getQName2ClassMap().get(typeQName); 596 if (class1 != null && !class1.isArray()) { 597 tEntry.setName(getJavaClassName(class1)); 598 processed = true; 599 } 600 } 601 602 if (!processed) { 603 if ((typeQName.getLocalPart(). 604 indexOf(SymbolTable.ANON_TOKEN) < 0)) { 605 tEntry.setName(emitter.getJavaName(typeQName)); 609 } else { 610 String localName = typeQName.getLocalPart(); 623 624 StringBuffer sb = new StringBuffer (localName); 630 int aidx; 631 632 while ((aidx = sb.toString().indexOf(SymbolTable.ANON_TOKEN)) > -1) { 633 sb.replace(aidx, aidx + SymbolTable.ANON_TOKEN.length(), ""); 634 char c = sb.charAt(aidx); 635 if (Character.isLetter(c) && Character.isLowerCase(c)) { 636 sb.setCharAt(aidx, Character.toUpperCase(c)); 637 } 638 } 639 640 localName = sb.toString(); 641 typeQName = new QName (typeQName.getNamespaceURI(), 642 localName); 643 644 if (emitter.isTypeCollisionProtection() && 645 !emitter.getNamespaceExcludes().contains(new NamespaceSelector(typeQName.getNamespaceURI()))) { 646 if (symbolTable.getType(typeQName) != null || 652 anonQNames.get(typeQName) != null) { 653 localName += "Type" + uniqueNum++; 654 typeQName = 655 new QName (typeQName.getNamespaceURI(), 656 localName); 657 } 658 659 anonQNames.put(typeQName, typeQName); 660 } 661 662 tEntry.setName(emitter.getJavaName(typeQName)); 664 } 665 } 667 Vector elements = tEntry.getContainedElements(); 668 if (elements != null) { 669 for (int i = 0; i < elements.size(); i++) { 670 ElementDecl elem = (ElementDecl) elements.get(i); 671 String varName = emitter.getJavaVariableName(typeQName, elem.getQName(), true); 672 elem.setName(varName); 673 } 674 } 675 676 Vector attributes = tEntry.getContainedAttributes(); 677 if (attributes != null) { 678 for (int i = 0; i < attributes.size(); i++) { 679 ContainedAttribute attr = (ContainedAttribute) attributes.get(i); 680 String varName = emitter.getJavaVariableName(typeQName, attr.getQName(), false); 681 attr.setName(varName); 682 } 683 } 684 } 685 686 entry.setName(tEntry.getName() + dims); 689 690 return uniqueNum; 691 } 692 693 699 private static String getJavaClassName(Class clazz) { 700 Class class1 = clazz; 701 702 while (class1.isArray()) { 703 class1 = class1.getComponentType(); 704 } 705 706 String name = class1.getName(); 707 name.replace('$', '.'); 708 return name; 709 } 710 711 724 private void setFaultContext(SymbolTable symbolTable) { 725 726 Iterator it = symbolTable.getHashMap().values().iterator(); 727 728 while (it.hasNext()) { 729 Vector v = (Vector ) it.next(); 730 731 for (int i = 0; i < v.size(); ++i) { 732 SymTabEntry entry = (SymTabEntry) v.elementAt(i); 733 734 if (entry instanceof BindingEntry) { 736 BindingEntry bEntry = (BindingEntry) entry; 737 HashMap allOpFaults = bEntry.getFaults(); 738 Iterator ops = allOpFaults.values().iterator(); 739 740 while (ops.hasNext()) { 742 ArrayList faults = (ArrayList ) ops.next(); 743 744 for (int j = 0; j < faults.size(); ++j) { 745 FaultInfo info = (FaultInfo) faults.get(j); 746 747 setFaultContext(info, symbolTable); 748 } 749 } 750 } 751 } 752 } 753 } 755 764 private void setFaultContext(FaultInfo fault, SymbolTable symbolTable) { 765 766 QName faultXmlType = null; 767 Vector parts = new Vector (); 768 769 try { 773 symbolTable.getParametersFromParts( 774 parts, fault.getMessage().getOrderedParts(null), false, 775 fault.getName(), null); 776 } catch (IOException e) { 777 } 778 779 String exceptionClassName = null; 781 782 for (int j = 0; j < parts.size(); j++) { 783 TypeEntry te = ((Parameter) (parts.elementAt(j))).getType(); 784 785 TypeEntry elementTE = null; 788 789 if (te instanceof Element) { 790 elementTE = te; 791 te = te.getRefType(); 792 } 793 794 faultXmlType = te.getQName(); 796 797 if ((te.getBaseType() != null) || te.isSimpleType() 801 || ((te.getDimensions().length() > 0) 802 && (te.getRefType().getBaseType() != null))) { 803 804 } else { 806 807 Boolean isComplexFault = (Boolean ) te.getDynamicVar( 809 JavaGeneratorFactory.COMPLEX_TYPE_FAULT); 810 811 if ((isComplexFault == null) || !isComplexFault.booleanValue()) { 812 813 te.setDynamicVar(JavaGeneratorFactory.COMPLEX_TYPE_FAULT, 815 Boolean.TRUE); 816 817 if (elementTE != null) { 818 te.setDynamicVar( 819 JavaGeneratorFactory.COMPLEX_TYPE_FAULT, 820 Boolean.TRUE); 821 } 822 823 HashSet derivedSet = 825 org.apache.axis.wsdl.symbolTable.Utils.getDerivedTypes( 826 te, symbolTable); 827 Iterator derivedI = derivedSet.iterator(); 828 829 while (derivedI.hasNext()) { 830 TypeEntry derivedTE = (TypeEntry) derivedI.next(); 831 832 derivedTE.setDynamicVar( 833 JavaGeneratorFactory.COMPLEX_TYPE_FAULT, 834 Boolean.TRUE); 835 } 836 837 TypeEntry base = 839 SchemaUtils.getComplexElementExtensionBase(te.getNode(), 840 symbolTable); 841 842 while (base != null) { 843 base.setDynamicVar( 844 JavaGeneratorFactory.COMPLEX_TYPE_FAULT, 845 Boolean.TRUE); 846 847 base = SchemaUtils.getComplexElementExtensionBase( 848 base.getNode(), symbolTable); 849 } 850 } 851 852 exceptionClassName = te.getName(); 854 } 855 } 856 857 String excName = getExceptionJavaNameHook(fault.getMessage().getQName()); if (excName != null) { 859 exceptionClassName = excName; 860 } 861 862 MessageEntry me = 865 symbolTable.getMessageEntry(fault.getMessage().getQName()); 866 867 if (me != null) { 868 me.setDynamicVar(JavaGeneratorFactory.EXCEPTION_DATA_TYPE, 869 faultXmlType); 870 871 if (exceptionClassName != null) { 872 me.setDynamicVar(JavaGeneratorFactory.COMPLEX_TYPE_FAULT, 873 Boolean.TRUE); 874 me.setDynamicVar(JavaGeneratorFactory.EXCEPTION_CLASS_NAME, 875 exceptionClassName); 876 } else { 877 me.setDynamicVar(JavaGeneratorFactory.EXCEPTION_CLASS_NAME, 878 emitter.getJavaName(me.getQName())); 879 } 880 } 881 } 882 883 protected String getExceptionJavaNameHook(QName qname) { 884 return null; 885 } 886 887 892 protected void determineInterfaceNames(SymbolTable symbolTable) { 893 894 Iterator it = symbolTable.getHashMap().values().iterator(); 895 896 while (it.hasNext()) { 897 Vector v = (Vector ) it.next(); 898 899 for (int i = 0; i < v.size(); ++i) { 900 SymTabEntry entry = (SymTabEntry) v.elementAt(i); 901 902 if (entry instanceof BindingEntry) { 903 904 BindingEntry bEntry = (BindingEntry) entry; 907 PortTypeEntry ptEntry = symbolTable.getPortTypeEntry( 908 bEntry.getBinding().getPortType().getQName()); 909 910 String seiName = getServiceEndpointInterfaceJavaNameHook(ptEntry, bEntry); 911 if (seiName == null) { 912 seiName = ptEntry.getName(); 913 } 914 915 bEntry.setDynamicVar(JavaBindingWriter.INTERFACE_NAME, 916 seiName); 917 } else if (entry instanceof ServiceEntry) { 918 ServiceEntry sEntry = (ServiceEntry) entry; 919 String siName = getServiceInterfaceJavaNameHook(sEntry); if (siName != null) { 921 sEntry.setName(siName); 922 } 923 924 Service service = sEntry.getService(); 925 Map portMap = service.getPorts(); 926 Iterator portIterator = portMap.values().iterator(); 927 928 while (portIterator.hasNext()) { 929 Port p = (Port) portIterator.next(); 930 931 Binding binding = p.getBinding(); 932 BindingEntry bEntry = 933 symbolTable.getBindingEntry(binding.getQName()); 934 935 if (bEntry.getBindingType() != BindingEntry.TYPE_SOAP) { 937 continue; 938 } 939 940 String portName = getPortJavaNameHook(p.getName()); if (portName != null) { 942 bEntry.setDynamicVar(JavaServiceWriter.PORT_NAME + ":" + p.getName(), 943 portName); 944 } 945 } 946 } 947 } 948 } 949 } 951 protected String getServiceEndpointInterfaceJavaNameHook(PortTypeEntry ptEntry, BindingEntry bEntry) { 952 return null; 953 } 954 955 protected String getServiceInterfaceJavaNameHook(ServiceEntry sEntry) { 956 return null; 957 } 958 959 protected String getPortJavaNameHook(String portName) { 960 return null; 961 } 962 963 969 protected void resolveNameClashes(SymbolTable symbolTable) { 970 971 HashSet anonTypes = new HashSet (); 973 List collisionCandidates = new ArrayList (); 975 List localParts = new ArrayList (); for (Iterator i = symbolTable.getHashMap().keySet().iterator(); i.hasNext(); ) { 977 QName qName = (QName )i.next(); 978 String localPart = qName.getLocalPart(); 979 if (!localParts.contains(localPart)) 980 localParts.add(localPart); 981 } 982 983 Map pkg2NamespacesMap = emitter.getNamespaces().getPkg2NamespacesMap(); 984 for (Iterator i = pkg2NamespacesMap.values().iterator(); i.hasNext(); ) { 985 Vector namespaces = (Vector )i.next(); 987 for (int j = 0; j < localParts.size(); j++) { 989 Vector v = new Vector (); 990 for (int k = 0; k < namespaces.size(); k++) { 991 QName qName = new QName ((String )namespaces.get(k), (String )localParts.get(j)); 992 if (symbolTable.getHashMap().get(qName) != null) { 993 v.addAll((Vector )symbolTable.getHashMap().get(qName)); 994 } 995 } 996 if(v.size()>0) { 997 collisionCandidates.add(v); 998 } 999 } 1000 } 1001 Iterator it = collisionCandidates.iterator(); 1002 1003 while (it.hasNext()) { 1004 Vector v = new Vector ( 1005 (Vector ) it.next()); 1007 int index = 0; 1009 1010 while (index < v.size()) { 1011 if (v.elementAt(index) instanceof MessageEntry) { 1012 MessageEntry msgEntry = (MessageEntry) v.elementAt(index); 1014 if (msgEntry.getDynamicVar(EXCEPTION_CLASS_NAME) == null) { 1015 v.removeElementAt(index); 1016 } else { 1017 index++; 1018 } 1019 } else { 1020 index++; 1021 } 1022 } 1023 1024 if (v.size() > 1) { 1025 boolean resolve = true; 1026 1027 if (v.size() == 2 && 1032 ((v.elementAt(0) instanceof Element && 1033 v.elementAt(1) instanceof Type) || 1034 (v.elementAt(1) instanceof Element && 1035 v.elementAt(0) instanceof Type))) { 1036 Element e; 1037 if (v.elementAt(0) instanceof Element) { 1038 e = (Element) v.elementAt(0); 1039 } else { 1040 e = (Element) v.elementAt(1); 1041 } 1042 1043 BooleanHolder forElement = new BooleanHolder (); 1044 QName eType = Utils.getTypeQName(e.getNode(), 1045 forElement, false); 1046 1047 if ((eType != null) && eType.equals(e.getQName()) 1048 && !forElement.value) { 1049 resolve = false; 1050 } 1051 } 1052 1053 if (resolve) { 1056 resolve = false; 1058 String name = null; 1059 1060 for (int i = 0; (i < v.size()) && !resolve; ++i) { 1061 SymTabEntry entry = (SymTabEntry) v.elementAt(i); 1062 1063 if ((entry instanceof MessageEntry) 1064 || (entry instanceof BindingEntry)) { 1065 String exceptionClassName = (String ) entry.getDynamicVar(EXCEPTION_CLASS_NAME); 1067 if (exceptionClassName != null) { 1068 if (name == null) { 1069 name = exceptionClassName; 1070 } else if (name.equals(exceptionClassName)) { 1071 resolve = true; 1072 } 1073 } 1074 } else if (name == null) { 1075 name = entry.getName(); 1076 } else if (name.equals(entry.getName())) { 1077 resolve = true; } 1079 } 1080 } 1081 1082 if (resolve) { 1084 boolean firstType = true; 1085 1086 for (int i = 0; i < v.size(); ++i) { 1087 SymTabEntry entry = (SymTabEntry) v.elementAt(i); 1088 1089 if (entry instanceof Element) { 1090 entry.setName(mangleName(entry.getName(), ELEMENT_SUFFIX)); 1091 1092 QName anonQName = 1096 new QName (entry.getQName().getNamespaceURI(), 1097 SymbolTable.ANON_TOKEN 1098 + entry.getQName().getLocalPart()); 1099 1100 TypeEntry anonType = 1101 symbolTable.getType(anonQName); 1102 1103 if (anonType != null) { 1104 anonType.setName(entry.getName()); 1105 anonTypes.add(anonType); 1106 } 1107 } else if (entry instanceof TypeEntry) { 1108 1109 if (firstType) { 1113 firstType = false; 1114 1115 Iterator types = 1116 symbolTable.getTypeIndex().values().iterator(); 1117 1118 while (types.hasNext()) { 1119 TypeEntry type = (TypeEntry) types.next(); 1120 1121 if ((type != entry) 1122 && (type.getBaseType() == null) 1123 && sameJavaClass(entry.getName(), 1124 type.getName())) { 1125 v.add(type); 1126 } 1127 } 1128 } 1129 1130 if (!anonTypes.contains(entry)) { 1133 1136 boolean needResolve = false; 1137 1138 for (int j = 0; j < v.size(); j++) { 1140 SymTabEntry e = (SymTabEntry) v.elementAt(j); 1141 if ((e instanceof PortTypeEntry 1142 || e instanceof ServiceEntry 1143 || e instanceof BindingEntry)) { 1144 needResolve = true; 1145 break; 1146 } 1147 } 1148 1149 if (!needResolve) { 1150 continue; 1151 } 1152 1153 Boolean isComplexTypeFault = (Boolean )entry.getDynamicVar(COMPLEX_TYPE_FAULT); 1155 if ((isComplexTypeFault != null) && isComplexTypeFault.booleanValue()) { 1156 entry.setName(mangleName(entry.getName(), EXCEPTION_SUFFIX)); 1157 } 1158 else { 1159 entry.setName(mangleName(entry.getName(), TYPE_SUFFIX)); 1160 } 1161 1162 Map elementIndex = symbolTable.getElementIndex(); 1164 List elements = new ArrayList (elementIndex.values()); 1165 for (int j = 0; j < elementIndex.size(); j++) { 1166 TypeEntry te = (TypeEntry) elements.get(j); 1167 TypeEntry ref = te.getRefType(); 1168 if (ref != null && entry.getQName().equals(ref.getQName())) { 1169 te.setName(entry.getName()); 1170 } 1171 } 1172 1173 if ((isComplexTypeFault != null) && isComplexTypeFault.booleanValue()) { 1175 List messageEntries = symbolTable.getMessageEntries(); 1177 for (int j = 0; j < messageEntries.size(); j++) { 1178 MessageEntry messageEntry = (MessageEntry)messageEntries.get(j); 1179 Boolean isComplexTypeFaultMsg = (Boolean )messageEntry.getDynamicVar(COMPLEX_TYPE_FAULT); 1180 if ((isComplexTypeFaultMsg != null) && (isComplexTypeFaultMsg.booleanValue())) { 1181 QName exceptionDataType = (QName )messageEntry.getDynamicVar(EXCEPTION_DATA_TYPE); 1182 if (((TypeEntry)entry).getQName().equals(exceptionDataType)) { 1183 String className = (String )messageEntry.getDynamicVar(EXCEPTION_CLASS_NAME); 1184 messageEntry.setDynamicVar(EXCEPTION_CLASS_NAME, className + EXCEPTION_SUFFIX); 1185 } 1186 } 1187 } 1188 } 1189 } 1190 } else if (entry instanceof PortTypeEntry) { 1191 entry.setName(mangleName(entry.getName(), PORT_TYPE_SUFFIX)); } else if (entry instanceof ServiceEntry) { 1193 entry.setName(mangleName(entry.getName(), SERVICE_SUFFIX)); 1194 } else if (entry instanceof MessageEntry) { 1195 Boolean complexTypeFault = 1196 (Boolean ) entry.getDynamicVar(COMPLEX_TYPE_FAULT); 1197 if ((complexTypeFault == null) || !complexTypeFault.booleanValue()) { 1198 String exceptionClassName = (String ) entry.getDynamicVar(EXCEPTION_CLASS_NAME); 1199 entry.setDynamicVar(EXCEPTION_CLASS_NAME, exceptionClassName + EXCEPTION_SUFFIX); 1200 } 1201 } 1202 else if (entry instanceof BindingEntry) { 1206 BindingEntry bEntry = (BindingEntry) entry; 1207 1208 if (bEntry.hasLiteral()) { 1215 entry.setName(mangleName(entry.getName(), 1216 BINDING_SUFFIX)); 1217 } 1218 } 1219 } 1220 } 1221 } 1222 } 1223 } 1225 1232 private String mangleName(String name, String mangle) { 1233 1234 int index = name.indexOf("["); 1235 1236 if (index >= 0) { 1237 String pre = name.substring(0, index); 1238 String post = name.substring(index); 1239 1240 return pre + mangle + post; 1241 } else { 1242 return name + mangle; 1243 } 1244 } 1245 1246 1253 private boolean sameJavaClass(String one, String two) { 1254 1255 int index1 = one.indexOf("["); 1256 int index2 = two.indexOf("["); 1257 1258 if (index1 > 0) { 1259 one = one.substring(0, index1); 1260 } 1261 1262 if (index2 > 0) { 1263 two = two.substring(0, index2); 1264 } 1265 1266 return one.equals(two); 1267 } 1268 1269 1274 protected void setAllReferencesToTrue() { 1275 1276 Iterator it = symbolTable.getHashMap().values().iterator(); 1277 1278 while (it.hasNext()) { 1279 Vector v = (Vector ) it.next(); 1280 1281 for (int i = 0; i < v.size(); ++i) { 1282 SymTabEntry entry = (SymTabEntry) v.elementAt(i); 1283 1284 if ((entry instanceof BindingEntry) 1285 && ((BindingEntry) entry).getBindingType() 1286 != BindingEntry.TYPE_SOAP) { 1287 entry.setIsReferenced(false); 1288 } else { 1289 entry.setIsReferenced(true); 1290 } 1291 } 1292 } 1293 } 1295 1301 protected void ignoreNonSOAPBindings(SymbolTable symbolTable) { 1302 1303 Vector unusedPortTypes = new Vector (); 1306 Vector usedPortTypes = new Vector (); 1307 Iterator it = symbolTable.getHashMap().values().iterator(); 1308 1309 while (it.hasNext()) { 1310 Vector v = (Vector ) it.next(); 1311 1312 for (int i = 0; i < v.size(); ++i) { 1313 SymTabEntry entry = (SymTabEntry) v.elementAt(i); 1314 1315 if (entry instanceof BindingEntry) { 1316 BindingEntry bEntry = (BindingEntry) entry; 1317 Binding binding = bEntry.getBinding(); 1318 PortType portType = binding.getPortType(); 1319 PortTypeEntry ptEntry = 1320 symbolTable.getPortTypeEntry(portType.getQName()); 1321 1322 if (bEntry.getBindingType() == BindingEntry.TYPE_SOAP) { 1323 1324 usedPortTypes.add(ptEntry); 1329 1330 if (unusedPortTypes.contains(ptEntry)) { 1331 unusedPortTypes.remove(ptEntry); 1332 } 1333 } else { 1334 bEntry.setIsReferenced(false); 1335 1336 if (!usedPortTypes.contains(ptEntry)) { 1339 unusedPortTypes.add(ptEntry); 1340 } 1341 } 1342 } 1343 } 1344 } 1345 1346 for (int i = 0; i < unusedPortTypes.size(); ++i) { 1349 PortTypeEntry ptEntry = (PortTypeEntry) unusedPortTypes.get(i); 1350 1351 ptEntry.setIsReferenced(false); 1352 } 1353 } 1355 1360 protected void constructSignatures(SymbolTable symbolTable) { 1361 1362 Iterator it = symbolTable.getHashMap().values().iterator(); 1363 1364 while (it.hasNext()) { 1365 Vector v = (Vector ) it.next(); 1366 1367 for (int i = 0; i < v.size(); ++i) { 1368 SymTabEntry entry = (SymTabEntry) v.elementAt(i); 1369 1370 if (entry instanceof BindingEntry) { 1371 BindingEntry bEntry = (BindingEntry) entry; 1372 Binding binding = bEntry.getBinding(); 1373 PortTypeEntry ptEntry = symbolTable.getPortTypeEntry( 1374 binding.getPortType().getQName()); 1375 PortType portType = ptEntry.getPortType(); 1376 Iterator operations = 1377 portType.getOperations().iterator(); 1378 1379 while (operations.hasNext()) { 1380 Operation operation = 1381 (Operation) operations.next(); 1382 String wsdlOpName = operation.getName(); 1383 OperationType type = operation.getStyle(); 1384 1385 String javaOpName = getOperationJavaNameHook(bEntry, wsdlOpName); if (javaOpName == null) { 1387 javaOpName = operation.getName(); 1388 } 1389 1390 Parameters parameters = 1391 bEntry.getParameters(operation); 1392 1393 if (OperationType.SOLICIT_RESPONSE.equals(type)) { 1394 parameters.signature = 1395 " // " 1396 + Messages.getMessage("invalidSolResp00", javaOpName); 1397 1398 System.err.println( 1399 Messages.getMessage("invalidSolResp00", javaOpName)); 1400 } else if (OperationType.NOTIFICATION.equals(type)) { 1401 parameters.signature = 1402 " // " 1403 + Messages.getMessage("invalidNotif00", javaOpName); 1404 1405 System.err.println( 1406 Messages.getMessage("invalidNotif00", javaOpName)); 1407 } else { if (parameters != null) { 1409 String returnType = getReturnTypeJavaNameHook(bEntry, wsdlOpName); 1410 if (returnType != null) { 1411 if (parameters.returnParam != null) { parameters.returnParam.getType().setName(returnType); 1413 } 1414 } 1415 for (int j = 0; j < parameters.list.size(); ++j) { 1416 Parameter p = (Parameter) parameters.list.get(j); 1417 String paramType = getParameterTypeJavaNameHook(bEntry, wsdlOpName, j); 1418 if (paramType != null) { 1419 p.getType().setName(paramType); 1420 } 1421 } 1422 parameters.signature = 1423 constructSignature(parameters, javaOpName); 1424 } 1425 } 1426 } 1427 } 1428 } 1429 } 1430 } 1432 protected String getOperationJavaNameHook(BindingEntry bEntry, String wsdlOpName) { 1433 return null; 1434 } 1435 1436 protected String getReturnTypeJavaNameHook(BindingEntry bEntry, String wsdlOpName) { 1437 return null; 1438 } 1439 1440 protected String getParameterTypeJavaNameHook(BindingEntry bEntry, String wsdlOpName, int pos) { 1441 return null; 1442 } 1443 1444 1451 private String constructSignature(Parameters parms, String opName) { 1452 1453 String name = Utils.xmlNameToJava(opName); 1454 String ret = "void"; 1455 1456 if ((parms != null) && (parms.returnParam != null)) { 1457 ret = Utils.getParameterTypeName(parms.returnParam); 1458 } 1459 1460 String signature = " public " + ret + " " + name + "("; 1461 boolean needComma = false; 1462 1463 for (int i = 0; (parms != null) && (i < parms.list.size()); ++i) { 1464 Parameter p = (Parameter) parms.list.get(i); 1465 1466 if (needComma) { 1467 signature = signature + ", "; 1468 } else { 1469 needComma = true; 1470 } 1471 1472 String javifiedName = Utils.xmlNameToJava(p.getName()); 1473 1474 if (p.getMode() == Parameter.IN) { 1475 signature += Utils.getParameterTypeName(p) + " " + javifiedName; 1476 } else { 1477 signature += Utils.holder(p, emitter) + " " + javifiedName; 1478 } 1479 } 1480 1481 signature = signature + ") throws java.rmi.RemoteException"; 1482 1483 if ((parms != null) && (parms.faults != null)) { 1484 1485 Iterator i = parms.faults.values().iterator(); 1487 1488 while (i.hasNext()) { 1489 Fault fault = (Fault) i.next(); 1490 String exceptionName = 1491 Utils.getFullExceptionName(fault.getMessage(), symbolTable); 1492 1493 if (exceptionName != null) { 1494 signature = signature + ", " + exceptionName; 1495 } 1496 } 1497 } 1498 1499 return signature; 1500 } 1502 1508 protected void determineIfHoldersNeeded(SymbolTable symbolTable) { 1509 1510 Iterator it = symbolTable.getHashMap().values().iterator(); 1511 1512 while (it.hasNext()) { 1513 Vector v = (Vector ) it.next(); 1514 1515 for (int i = 0; i < v.size(); ++i) { 1516 if (v.get(i) instanceof BindingEntry) { 1517 1518 BindingEntry bEntry = (BindingEntry) v.get(i); 1521 1522 Iterator operations = 1525 bEntry.getParameters().values().iterator(); 1526 1527 while (operations.hasNext()) { 1528 Parameters parms = (Parameters) operations.next(); 1529 1530 for (int j = 0; j < parms.list.size(); ++j) { 1531 Parameter p = (Parameter) parms.list.get(j); 1532 1533 if (p.getMode() != Parameter.IN) { 1536 TypeEntry typeEntry = p.getType(); 1537 1538 typeEntry.setDynamicVar( 1539 JavaTypeWriter.HOLDER_IS_NEEDED, 1540 Boolean.TRUE); 1541 1542 if (!typeEntry.isSimpleType() 1545 && (typeEntry.getRefType() != null)) { 1546 typeEntry.getRefType().setDynamicVar( 1547 JavaTypeWriter.HOLDER_IS_NEEDED, 1548 Boolean.TRUE); 1549 } 1550 1551 QName anonQName = 1554 SchemaUtils.getElementAnonQName( 1555 p.getType().getNode()); 1556 1557 if (anonQName != null) { 1558 TypeEntry anonType = 1559 symbolTable.getType(anonQName); 1560 1561 if (anonType != null) { 1562 anonType.setDynamicVar( 1563 JavaTypeWriter.HOLDER_IS_NEEDED, 1564 Boolean.TRUE); 1565 } 1566 } 1567 } 1568 } 1569 } 1570 } 1571 } 1572 } 1573 } 1575 1579 BaseTypeMapping btm = null; 1580 1581 1586 public void setBaseTypeMapping(BaseTypeMapping btm) { 1587 this.btm = btm; 1588 } 1589 1590 1595 public BaseTypeMapping getBaseTypeMapping() { 1596 if (btm == null) { 1597 btm = new BaseTypeMapping() { 1598 1599 public String getBaseName(QName qNameIn) { 1600 1601 javax.xml.namespace.QName qName = 1602 new javax.xml.namespace.QName (qNameIn.getNamespaceURI(), 1603 qNameIn.getLocalPart()); 1604 Class cls = 1605 emitter.getDefaultTypeMapping().getClassForQName(qName); 1606 1607 if (cls == null) { 1608 return null; 1609 } else { 1610 return JavaUtils.getTextClassName(cls.getName()); 1611 } 1612 } 1613 }; 1614 } 1615 1616 return btm; 1617 } 1618 1619 1637 protected boolean include(QName qName) { 1638 String namespace = 1639 (qName != null && qName.getNamespaceURI() != null) 1640 ? qName.getNamespaceURI() 1641 : ""; 1642 1643 boolean doInclude = false; 1644 NamespaceSelector selector = new NamespaceSelector(namespace); 1645 if (qName == null 1646 || emitter == null 1647 || emitter.getNamespaceIncludes().contains(selector) 1648 || (emitter.getNamespaceIncludes().size() == 0 1649 && !emitter.getNamespaceExcludes().contains(selector))) { 1650 doInclude = true; 1651 } 1652 else { 1653 log_.info( 1654 "excluding code generation for non-included QName:" + qName); 1655 1656 } 1657 return doInclude; 1658 } 1659 1660} | Popular Tags |