1 16 package org.apache.axis.wsdl.toJava; 17 18 import org.apache.axis.Constants; 19 import org.apache.axis.constants.Scope; 20 import org.apache.axis.description.ServiceDesc; 21 import org.apache.axis.encoding.TypeMapping; 22 import org.apache.axis.encoding.TypeMappingRegistryImpl; 23 import org.apache.axis.i18n.Messages; 24 import org.apache.axis.utils.ClassUtils; 25 import org.apache.axis.utils.JavaUtils; 26 import org.apache.axis.wsdl.gen.GeneratorFactory; 27 import org.apache.axis.wsdl.gen.Parser; 28 import org.apache.axis.wsdl.symbolTable.BaseTypeMapping; 29 import org.apache.axis.wsdl.symbolTable.SymTabEntry; 30 import org.apache.axis.wsdl.symbolTable.SymbolTable; 31 import org.w3c.dom.Document ; 32 import org.xml.sax.SAXException ; 33 34 import javax.wsdl.WSDLException; 35 import javax.xml.namespace.QName ; 36 import javax.xml.parsers.ParserConfigurationException ; 37 38 import java.io.FileInputStream ; 39 import java.io.IOException ; 40 import java.lang.reflect.Constructor ; 41 import java.util.ArrayList ; 42 import java.util.Enumeration ; 43 import java.util.HashMap ; 44 import java.util.Iterator ; 45 import java.util.List ; 46 import java.util.Properties ; 47 import java.util.Vector ; 48 49 58 public class Emitter extends Parser { 59 60 61 public static final String DEFAULT_NSTOPKG_FILE = "NStoPkg.properties"; 62 63 64 protected HashMap namespaceMap = new HashMap (); 65 66 67 protected String typeMappingVersion = "1.2"; 68 69 70 protected BaseTypeMapping baseTypeMapping = null; 71 72 73 protected Namespaces namespaces = null; 74 75 76 protected String NStoPkgFilename = null; 77 78 79 private boolean bEmitServer = false; 80 81 82 private boolean bDeploySkeleton = false; 83 84 85 private boolean bEmitTestCase = false; 86 87 88 private boolean bGenerateAll = false; 89 90 91 private boolean bHelperGeneration = false; 92 93 private boolean bBuildFileGeneration = false; 94 95 private boolean typeCollisionProtection = true; 96 97 98 private boolean allowInvalidURL = false; 99 100 101 private String packageName = null; 102 103 104 private Scope scope = null; 105 106 107 private GeneratedFileInfo fileInfo = new GeneratedFileInfo(); 108 109 110 private HashMap delayedNamespacesMap = new HashMap (); 111 112 113 private String outputDir = null; 114 115 120 protected List nsIncludes = new ArrayList (); 121 122 127 protected List nsExcludes = new ArrayList (); 128 129 132 protected List properties = new ArrayList (); 133 134 139 private String implementationClassName = null; 140 141 142 private TypeMapping defaultTM = null; 144 private TypeMappingRegistryImpl tmr = new TypeMappingRegistryImpl(); 145 146 147 private HashMap qName2ClassMap; 148 149 150 private ServiceDesc serviceDesc; 151 152 153 private boolean isDeploy; 154 155 158 public Emitter() { 159 setFactory(new JavaGeneratorFactory(this)); 160 } 162 167 172 public void setServerSide(boolean value) { 173 this.bEmitServer = value; 174 } 176 181 public boolean isServerSide() { 182 return bEmitServer; 183 } 185 190 public void setSkeletonWanted(boolean value) { 191 bDeploySkeleton = value; 192 } 194 199 public boolean isSkeletonWanted() { 200 return bDeploySkeleton; 201 } 203 208 public void setHelperWanted(boolean value) { 209 bHelperGeneration = value; 210 } 212 217 public boolean isHelperWanted() { 218 return bHelperGeneration; 219 } 221 226 public void setTestCaseWanted(boolean value) { 227 this.bEmitTestCase = value; 228 } 230 235 public boolean isTestCaseWanted() { 236 return bEmitTestCase; 237 } 239 243 public boolean isBuildFileWanted(){ 244 return bBuildFileGeneration; 245 } 246 247 251 public void setBuildFileWanted(boolean value){ 252 bBuildFileGeneration = value; 253 } 254 255 264 public void setAllWanted(boolean all) { 265 bGenerateAll = all; 266 } 268 273 public boolean isAllWanted() { 274 return bGenerateAll; 275 } 277 282 public Namespaces getNamespaces() { 283 return namespaces; 284 } 286 291 public void setOutputDir(String outputDir) { 292 this.outputDir = outputDir; 293 } 294 295 300 public String getOutputDir() { 301 return outputDir; 302 } 303 304 309 public String getPackageName() { 310 return packageName; 311 } 312 313 318 public void setPackageName(String packageName) { 319 this.packageName = packageName; 320 } 321 322 330 public void setScope(Scope scope) { 331 this.scope = scope; 332 } 334 339 public Scope getScope() { 340 return scope; 341 } 343 348 public void setNStoPkg(String NStoPkgFilename) { 349 350 if (NStoPkgFilename != null) { 351 this.NStoPkgFilename = NStoPkgFilename; 352 } 353 } 355 360 public void setNamespaceMap(HashMap map) { 361 delayedNamespacesMap = map; 362 } 363 364 369 public HashMap getNamespaceMap() { 370 return delayedNamespacesMap; 371 } 372 373 376 public void setNamespaceIncludes(List nsIncludes) { 377 this.nsIncludes = nsIncludes; 378 } 379 380 383 public List getNamespaceIncludes() { 384 return this.nsIncludes; 385 } 386 387 390 public void setNamespaceExcludes(List nsExcludes) { 391 this.nsExcludes = nsExcludes; 392 } 393 394 397 public List getNamespaceExcludes() { 398 return this.nsExcludes; 399 } 400 401 404 public void setProperties(List properties) { 405 this.properties = properties; 406 } 407 408 411 public List getProperties() { 412 return this.properties; 413 } 414 415 420 public TypeMapping getDefaultTypeMapping() { 421 if (defaultTM == null) { 422 defaultTM = 423 (TypeMapping)tmr.getTypeMapping(Constants.URI_SOAP11_ENC); 424 } 425 return defaultTM; 426 } 427 428 433 public void setDefaultTypeMapping(TypeMapping defaultTM) { 434 this.defaultTM = defaultTM; 435 } 436 437 442 public void setFactory(String factory) { 443 444 try { 445 Class clazz = ClassUtils.forName(factory); 446 GeneratorFactory genFac; 447 try { 448 Constructor ctor = clazz.getConstructor(new Class []{ 449 getClass()}); 450 451 genFac = (GeneratorFactory) ctor.newInstance(new Object []{ 452 this}); 453 } catch (NoSuchMethodException ex) { 454 genFac = (GeneratorFactory) clazz.newInstance(); 455 } 456 457 setFactory(genFac); 458 } catch (Exception ex) { 459 ex.printStackTrace(); 460 } 461 } 463 468 475 public GeneratedFileInfo getGeneratedFileInfo() { 476 return fileInfo; 477 } 478 479 484 public List getGeneratedClassNames() { 485 return fileInfo.getClassNames(); 486 } 487 488 493 public List getGeneratedFileNames() { 494 return fileInfo.getFileNames(); 495 } 496 497 503 public String getPackage(String namespace) { 504 return namespaces.getCreate(namespace); 505 } 506 507 513 public String getPackage(QName qName) { 514 return getPackage(qName.getNamespaceURI()); 515 } 516 517 523 public String getJavaName(QName qName) { 524 525 if (qName.getLocalPart().indexOf("[") > 0) { 528 String localPart = qName.getLocalPart().substring(0, 529 qName.getLocalPart().indexOf("[")); 530 QName eQName = new QName (qName.getNamespaceURI(), localPart); 531 532 return getJavaName(eQName) + "[]"; 533 } 534 535 if (qName.getNamespaceURI().equalsIgnoreCase("java")) { 537 return qName.getLocalPart(); 538 } 539 540 String fullJavaName = 542 getFactory().getBaseTypeMapping().getBaseName(qName); 543 544 if (fullJavaName != null) { 545 return fullJavaName; 546 } 547 548 fullJavaName = getJavaNameHook(qName); 549 if (fullJavaName != null) { 550 return fullJavaName; 551 } 552 String pkg = getPackage(qName.getNamespaceURI()); 554 555 if (pkg != null && pkg.length() > 0) { 556 fullJavaName = pkg + "." 557 + Utils.xmlNameToJavaClass(qName.getLocalPart()); 558 } else { 559 fullJavaName = Utils.xmlNameToJavaClass(qName.getLocalPart()); 560 } 561 562 return fullJavaName; 563 } 565 protected String getJavaNameHook(QName qname) { return null; } 566 567 568 573 public String getJavaVariableName(QName typeQName, QName xmlName, boolean isElement) { 574 String javaName = getJavaVariableNameHook(typeQName, xmlName, isElement); 575 if (javaName == null) { 576 String elemName = Utils.getLastLocalPart(xmlName.getLocalPart()); 577 javaName = Utils.xmlNameToJava(elemName); 578 } 579 return javaName; 580 } 581 582 protected String getJavaVariableNameHook(QName typeQName, QName xmlName, boolean isElement) { 583 return null; 584 } 585 586 587 588 597 public void run(String wsdlURL) throws Exception { 598 setup(); 599 super.run(wsdlURL); 600 } 602 616 public void run(String context, Document doc) 617 throws IOException , SAXException , WSDLException, 618 ParserConfigurationException { 619 setup(); 620 super.run(context, doc); 621 } 623 628 private void setup() throws IOException { 629 630 if (baseTypeMapping == null) { 631 setTypeMappingVersion(typeMappingVersion); 632 } 633 634 getFactory().setBaseTypeMapping(baseTypeMapping); 635 636 namespaces = new Namespaces(outputDir); 637 638 if (packageName != null) { 639 namespaces.setDefaultPackage(packageName); 640 } else { 641 642 getNStoPkgFromPropsFile(namespaces); 646 647 if (delayedNamespacesMap != null) { 648 namespaces.putAll(delayedNamespacesMap); 649 } 650 } 651 } 653 658 protected void sanityCheck(SymbolTable symbolTable) { 659 660 Iterator it = symbolTable.getHashMap().values().iterator(); 661 662 while (it.hasNext()) { 663 Vector v = (Vector ) it.next(); 664 665 for (int i = 0; i < v.size(); ++i) { 666 SymTabEntry entry = (SymTabEntry) v.elementAt(i); 667 String namespace = entry.getQName().getNamespaceURI(); 668 String packageName = 669 org.apache.axis.wsdl.toJava.Utils.makePackageName( 670 namespace); 671 String localName = entry.getQName().getLocalPart(); 672 673 if (localName.equals(packageName) 674 && packageName.equals( 675 namespaces.getCreate(namespace))) { 676 packageName += "_pkg"; 677 678 namespaces.put(namespace, packageName); 679 } 680 } 681 } 682 } 683 684 702 private void getNStoPkgFromPropsFile(HashMap namespaces) 703 throws IOException { 704 705 Properties mappings = new Properties (); 706 707 if (NStoPkgFilename != null) { 708 try { 709 mappings.load(new FileInputStream (NStoPkgFilename)); 710 711 if (verbose) { 712 System.out.println( 713 Messages.getMessage( 714 "nsToPkgFileLoaded00", NStoPkgFilename)); 715 } 716 } catch (Throwable t) { 717 718 throw new IOException ( 721 Messages.getMessage( 722 "nsToPkgFileNotFound00", NStoPkgFilename)); 723 } 724 } else { 725 try { 726 mappings.load(new FileInputStream (DEFAULT_NSTOPKG_FILE)); 727 728 if (verbose) { 729 System.out.println( 730 Messages.getMessage( 731 "nsToPkgFileLoaded00", DEFAULT_NSTOPKG_FILE)); 732 } 733 } catch (Throwable t) { 734 try { 735 mappings.load(ClassUtils.getResourceAsStream(Emitter.class, 736 DEFAULT_NSTOPKG_FILE)); 737 738 if (verbose) { 739 System.out.println( 740 Messages.getMessage( 741 "nsToPkgDefaultFileLoaded00", 742 DEFAULT_NSTOPKG_FILE)); 743 } 744 } catch (Throwable t1) { 745 746 } 750 } 751 } 752 753 Enumeration keys = mappings.propertyNames(); 754 755 while (keys.hasMoreElements()) { 756 String key = (String ) keys.nextElement(); 757 758 namespaces.put(key, mappings.getProperty(key)); 759 } 760 } 762 765 public String getTypeMappingVersion() { 766 return typeMappingVersion; 767 } 768 769 774 public void setTypeMappingVersion(String typeMappingVersion) { 775 this.typeMappingVersion = typeMappingVersion; 776 tmr.doRegisterFromVersion(typeMappingVersion); 777 baseTypeMapping = new BaseTypeMapping() { 778 779 final TypeMapping defaultTM = getDefaultTypeMapping(); 780 781 public String getBaseName(QName qNameIn) { 782 783 javax.xml.namespace.QName qName = 784 new javax.xml.namespace.QName (qNameIn.getNamespaceURI(), 785 qNameIn.getLocalPart()); 786 Class cls = 787 defaultTM.getClassForQName(qName); 788 789 if (cls == null) { 790 return null; 791 } else { 792 return JavaUtils.getTextClassName(cls.getName()); 793 } 794 } 795 }; 796 } 797 798 800 807 public GeneratorFactory getWriterFactory() { 808 return getFactory(); 809 } 811 818 public void emit(String uri) throws Exception { 819 run(uri); 820 } 822 837 public void emit(String context, Document doc) 838 throws IOException , SAXException , WSDLException, 839 ParserConfigurationException { 840 run(context, doc); 841 } 843 849 public void generateServerSide(boolean value) { 850 setServerSide(value); 851 } 852 853 859 public boolean getGenerateServerSide() { 860 return isServerSide(); 861 } 862 863 869 public void deploySkeleton(boolean value) { 870 setSkeletonWanted(value); 871 } 872 873 879 public boolean getDeploySkeleton() { 880 return isSkeletonWanted(); 881 } 882 883 889 public void setHelperGeneration(boolean value) { 890 setHelperWanted(value); 891 } 892 893 899 public boolean getHelperGeneration() { 900 return isHelperWanted(); 901 } 902 903 909 public void generateImports(boolean generateImports) { 910 setImports(generateImports); 911 } 913 919 public void debug(boolean value) { 920 setDebug(value); 921 } 923 929 public boolean getDebug() { 930 return isDebug(); 931 } 933 939 public void verbose(boolean value) { 940 setVerbose(value); 941 } 942 943 949 public boolean getVerbose() { 950 return isVerbose(); 951 } 952 953 959 public void generateTestCase(boolean value) { 960 setTestCaseWanted(value); 961 } 962 963 967 public void generateAll(boolean all) { 968 setAllWanted(all); 969 } 971 975 public boolean isTypeCollisionProtection(){ 976 return this.typeCollisionProtection; 977 } 978 979 983 public void setTypeCollisionProtection(boolean value){ 984 this.typeCollisionProtection = value; 985 } 986 987 991 public String getImplementationClassName() { 992 return implementationClassName; 993 } 994 995 1000 public void setImplementationClassName(String implementationClassName) { 1001 this.implementationClassName = implementationClassName; 1002 } 1003 1004 1007 public boolean isAllowInvalidURL() { 1008 return allowInvalidURL; 1009 } 1010 1011 1014 public void setAllowInvalidURL(boolean allowInvalidURL) { 1015 this.allowInvalidURL = allowInvalidURL; 1016 } 1017 1018 1022 public void setQName2ClassMap(HashMap map) { 1023 qName2ClassMap = map; 1024 } 1025 1026 1030 public HashMap getQName2ClassMap() { 1031 return qName2ClassMap; 1032 } 1033 1034 1038 public ServiceDesc getServiceDesc() { 1039 return serviceDesc; 1040 } 1041 1042 1046 public void setServiceDesc(ServiceDesc serviceDesc) { 1047 this.serviceDesc = serviceDesc; 1048 } 1049 1050 1054 public boolean isDeploy() { 1055 return isDeploy; 1056 } 1057 1058 1062 public void setDeploy(boolean isDeploy) { 1063 this.isDeploy = isDeploy; 1064 } 1065 1066 1072 protected boolean doesExist(String className) { 1073 try { 1074 ClassUtils.forName(className); 1075 } catch (ClassNotFoundException e) { 1076 return false; 1077 } 1078 1079 return true; 1080 } 1081 1082 public void setWrapArrays(boolean wrapArrays) { 1083 this.wrapArrays = wrapArrays; 1084 } 1085} 1086 | Popular Tags |