1 55 package org.jboss.axis.wsdl.toJava; 56 57 import org.jboss.axis.encoding.DefaultSOAPEncodingTypeMappingImpl; 58 import org.jboss.axis.encoding.DefaultTypeMappingImpl; 59 import org.jboss.axis.encoding.TypeMapping; 60 import org.jboss.axis.enums.Scope; 61 import org.jboss.axis.i18n.Messages; 62 import org.jboss.axis.utils.ClassUtils; 63 import org.jboss.axis.utils.JavaUtils; 64 import org.jboss.axis.wsdl.gen.GeneratorFactory; 65 import org.jboss.axis.wsdl.gen.Parser; 66 import org.jboss.axis.wsdl.symbolTable.BaseTypeMapping; 67 import org.jboss.axis.wsdl.symbolTable.SymTabEntry; 68 import org.jboss.axis.wsdl.symbolTable.SymbolTable; 69 import org.w3c.dom.Document ; 70 import org.xml.sax.SAXException ; 71 72 import javax.wsdl.WSDLException; 73 import javax.xml.namespace.QName ; 74 import javax.xml.parsers.ParserConfigurationException ; 75 import java.io.FileInputStream ; 76 import java.io.IOException ; 77 import java.lang.reflect.Constructor ; 78 import java.util.Enumeration ; 79 import java.util.HashMap ; 80 import java.util.Iterator ; 81 import java.util.List ; 82 import java.util.Properties ; 83 import java.util.Vector ; 84 85 94 public class Emitter extends Parser 95 { 96 97 public static final String DEFAULT_NSTOPKG_FILE = "NStoPkg.properties"; 98 99 protected HashMap namespaceMap = new HashMap (); 100 protected String typeMappingVersion = "1.1"; 101 protected BaseTypeMapping baseTypeMapping = null; 102 protected Namespaces namespaces = null; 103 protected String NStoPkgFilename = null; 104 105 private boolean bEmitServer = false; 106 private boolean bDeploySkeleton = false; 107 private boolean bEmitTestCase = false; 108 private boolean bGenerateAll = false; 109 private boolean bHelperGeneration = false; 110 private String packageName = null; 111 private Scope scope = null; 112 private GeneratedFileInfo fileInfo = new GeneratedFileInfo(); 113 private HashMap delayedNamespacesMap = new HashMap (); 114 private String outputDir = null; 115 116 119 public Emitter() 120 { 121 setFactory(new JavaGeneratorFactory(this)); 122 } 124 129 134 public void setServerSide(boolean value) 135 { 136 this.bEmitServer = value; 137 } 139 142 public boolean isServerSide() 143 { 144 return bEmitServer; 145 } 147 152 public void setSkeletonWanted(boolean value) 153 { 154 bDeploySkeleton = value; 155 } 157 160 public boolean isSkeletonWanted() 161 { 162 return bDeploySkeleton; 163 } 165 170 public void setHelperWanted(boolean value) 171 { 172 bHelperGeneration = value; 173 } 175 178 public boolean isHelperWanted() 179 { 180 return bHelperGeneration; 181 } 183 188 public void setTestCaseWanted(boolean value) 189 { 190 this.bEmitTestCase = value; 191 } 193 public boolean isTestCaseWanted() 194 { 195 return bEmitTestCase; 196 } 198 205 public void setAllWanted(boolean all) 206 { 207 bGenerateAll = all; 208 } 210 public boolean isAllWanted() 211 { 212 return bGenerateAll; 213 } 215 public Namespaces getNamespaces() 216 { 217 return namespaces; 218 } 220 223 public void setOutputDir(String outputDir) 224 { 225 this.outputDir = outputDir; 226 } 227 228 231 public String getOutputDir() 232 { 233 return outputDir; 234 } 235 236 239 public String getPackageName() 240 { 241 return packageName; 242 } 243 244 247 public void setPackageName(String packageName) 248 { 249 this.packageName = packageName; 250 } 251 252 260 public void setScope(Scope scope) 261 { 262 this.scope = scope; 263 } 265 268 public Scope getScope() 269 { 270 return scope; 271 } 273 276 public void setNStoPkg(String NStoPkgFilename) 277 { 278 if (NStoPkgFilename != null) 279 { 280 this.NStoPkgFilename = NStoPkgFilename; 281 } 282 } 284 285 288 public void setNamespaceMap(HashMap map) 289 { 290 delayedNamespacesMap = map; 291 } 292 293 296 public HashMap getNamespaceMap() 297 { 298 return delayedNamespacesMap; 299 } 300 301 306 public void setFactory(String factory) 307 { 308 try 309 { 310 Class clazz = ClassUtils.forName(factory); 311 GeneratorFactory genFac = null; 312 try 313 { 314 Constructor ctor = 315 clazz.getConstructor(new Class []{getClass()}); 316 genFac = (GeneratorFactory) 317 ctor.newInstance(new Object []{this}); 318 } 319 catch (NoSuchMethodException ex) 320 { 321 genFac = (GeneratorFactory)clazz.newInstance(); 322 } 323 setFactory(genFac); 324 } 325 catch (Exception ex) 326 { 327 ex.printStackTrace(); 328 } 329 } 331 336 343 public GeneratedFileInfo getGeneratedFileInfo() 344 { 345 return fileInfo; 346 } 347 348 351 public List getGeneratedClassNames() 352 { 353 return fileInfo.getClassNames(); 354 } 355 356 359 public List getGeneratedFileNames() 360 { 361 return fileInfo.getFileNames(); 362 } 363 364 367 public String getPackage(String namespace) 368 { 369 return namespaces.getCreate(namespace); 370 } 371 372 375 public String getPackage(QName qName) 376 { 377 return getPackage(qName.getNamespaceURI()); 378 } 379 380 383 public String getJavaName(QName qName) 384 { 385 386 if (qName.getLocalPart().indexOf("[") > 0) 389 { 390 String localPart = qName.getLocalPart().substring(0, qName.getLocalPart().indexOf("[")); 391 QName eQName = new QName (qName.getNamespaceURI(), localPart); 392 return getJavaName(eQName) + "[]"; 393 } 394 395 if (qName.getNamespaceURI().equalsIgnoreCase("java")) 397 { 398 return qName.getLocalPart(); 399 } 400 401 String fullJavaName = getFactory().getBaseTypeMapping().getBaseName(qName); 403 if (fullJavaName != null) 404 return fullJavaName; 405 406 String pkg = getPackage(qName.getNamespaceURI()); 408 if (pkg != null) 409 { 410 fullJavaName = pkg + "." + Utils.xmlNameToJavaClass(qName.getLocalPart()); 411 } 412 else 413 { 414 fullJavaName = Utils.xmlNameToJavaClass(qName.getLocalPart()); 415 } 416 return fullJavaName; 417 } 419 420 426 public void run(String wsdlURL) throws Exception 427 { 428 setup(); 429 super.run(wsdlURL); 430 } 432 442 public void run(String context, Document doc) throws 443 IOException , SAXException , WSDLException, 444 ParserConfigurationException 445 { 446 setup(); 447 super.run(context, doc); 448 } 450 private void setup() throws IOException 451 { 452 if (baseTypeMapping == null) 453 { 454 setTypeMappingVersion(typeMappingVersion); 455 } 456 getFactory().setBaseTypeMapping(baseTypeMapping); 457 458 namespaces = new Namespaces(outputDir); 459 460 if (packageName != null) 461 { 462 namespaces.setDefaultPackage(packageName); 463 } 464 else 465 { 466 getNStoPkgFromPropsFile(namespaces); 470 471 if (delayedNamespacesMap != null) 472 { 473 namespaces.putAll(delayedNamespacesMap); 474 } 475 } 476 } 478 479 protected void sanityCheck(SymbolTable symbolTable) 480 { 481 Iterator it = symbolTable.getHashMap().values().iterator(); 482 while (it.hasNext()) 483 { 484 Vector v = (Vector )it.next(); 485 for (int i = 0; i < v.size(); ++i) 486 { 487 SymTabEntry entry = (SymTabEntry)v.elementAt(i); 488 String namespace = entry.getQName().getNamespaceURI(); 489 String packageName = 490 org.jboss.axis.wsdl.toJava.Utils.makePackageName(namespace); 491 String localName = entry.getQName().getLocalPart(); 492 if (localName.equals(packageName) && 493 packageName.equals(namespaces.getCreate(namespace))) 494 { 495 packageName += "_pkg"; 496 namespaces.put(namespace, packageName); 497 } 498 499 } 500 } 501 } 502 503 520 private void getNStoPkgFromPropsFile(HashMap namespaces) throws IOException 521 { 522 523 Properties mappings = new Properties (); 524 if (NStoPkgFilename != null) 525 { 526 try 527 { 528 mappings.load(new FileInputStream (NStoPkgFilename)); 529 if (verbose) 530 { 531 System.out.println(Messages.getMessage("nsToPkgFileLoaded00", NStoPkgFilename)); 532 } 533 } 534 catch (Throwable t) 535 { 536 throw new IOException (Messages.getMessage("nsToPkgFileNotFound00", NStoPkgFilename)); 539 } 540 } 541 else 542 { 543 try 544 { 545 mappings.load(new FileInputStream (DEFAULT_NSTOPKG_FILE)); 546 if (verbose) 547 { 548 System.out.println(Messages.getMessage("nsToPkgFileLoaded00", DEFAULT_NSTOPKG_FILE)); 549 } 550 } 551 catch (Throwable t) 552 { 553 try 554 { 555 mappings.load(ClassUtils.getResourceAsStream(Emitter.class, DEFAULT_NSTOPKG_FILE)); 556 if (verbose) 557 { 558 System.out.println(Messages.getMessage("nsToPkgDefaultFileLoaded00", DEFAULT_NSTOPKG_FILE)); 559 } 560 561 } 562 catch (Throwable t1) 563 { 564 } 568 } 569 } 570 571 Enumeration keys = mappings.propertyNames(); 572 while (keys.hasMoreElements()) 573 { 574 String key = (String )keys.nextElement(); 575 namespaces.put(key, mappings.getProperty(key)); 576 } 577 } 579 public void setTypeMappingVersion(String typeMappingVersion) 580 { 581 if (typeMappingVersion.equals("1.1")) 582 { 583 baseTypeMapping = 584 new BaseTypeMapping() 585 { 586 final TypeMapping defaultTM = DefaultTypeMappingImpl.getSingleton(); 587 588 public String getBaseName(QName qNameIn) 589 { 590 javax.xml.namespace.QName qName = 591 new javax.xml.namespace.QName (qNameIn.getNamespaceURI(), 592 qNameIn.getLocalPart()); 593 Class cls = defaultTM.getClassForQName(qName); 594 if (cls == null) 595 return null; 596 else 597 return JavaUtils.getTextClassName(cls.getName()); 598 } 599 }; 600 } 601 else 602 { 603 baseTypeMapping = 604 new BaseTypeMapping() 605 { 606 final TypeMapping defaultTM = DefaultSOAPEncodingTypeMappingImpl.create(); 607 608 public String getBaseName(QName qNameIn) 609 { 610 javax.xml.namespace.QName qName = 611 new javax.xml.namespace.QName (qNameIn.getNamespaceURI(), 612 qNameIn.getLocalPart()); 613 Class cls = defaultTM.getClassForQName(qName); 614 if (cls == null) 615 return null; 616 else 617 return JavaUtils.getTextClassName(cls.getName()); 618 } 619 }; 620 } 621 } 622 623 625 631 public GeneratorFactory getWriterFactory() 632 { 633 return getFactory(); 634 } 636 642 public void emit(String uri) throws Exception 643 { 644 run(uri); 645 } 647 658 public void emit(String context, Document doc) 659 throws IOException , SAXException , WSDLException, 660 ParserConfigurationException 661 { 662 run(context, doc); 663 } 665 671 public void generateServerSide(boolean value) 672 { 673 setServerSide(value); 674 } 675 676 681 public boolean getGenerateServerSide() 682 { 683 return isServerSide(); 684 } 685 686 692 public void deploySkeleton(boolean value) 693 { 694 setSkeletonWanted(value); 695 } 696 697 702 public boolean getDeploySkeleton() 703 { 704 return isSkeletonWanted(); 705 } 706 707 713 public void setHelperGeneration(boolean value) 714 { 715 setHelperWanted(value); 716 } 717 718 723 public boolean getHelperGeneration() 724 { 725 return isHelperWanted(); 726 } 727 728 734 public void generateImports(boolean generateImports) 735 { 736 setImports(generateImports); 737 } 739 745 public void debug(boolean value) 746 { 747 setDebug(value); 748 } 750 755 public boolean getDebug() 756 { 757 return isDebug(); 758 } 760 766 public void verbose(boolean value) 767 { 768 setVerbose(value); 769 } 770 771 776 public boolean getVerbose() 777 { 778 return isVerbose(); 779 } 780 781 787 public void generateTestCase(boolean value) 788 { 789 setTestCaseWanted(value); 790 } 791 792 795 public void generateAll(boolean all) 796 { 797 setAllWanted(all); 798 } } 800 | Popular Tags |