1 17 18 package org.apache.geronimo.tools.xmlbeans; 19 20 import java.io.File ; 21 import java.util.Collection ; 22 import java.util.List ; 23 import java.util.Collections ; 24 import java.util.Set ; 25 import java.util.Map ; 26 import java.util.ArrayList ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.net.URI ; 30 31 import org.apache.xmlbeans.SchemaTypeSystem; 32 import org.apache.xmlbeans.SchemaTypeLoader; 33 import org.apache.xmlbeans.XmlBeans; 34 import org.apache.xmlbeans.XmlOptions; 35 import org.apache.xmlbeans.XmlObject; 36 import org.apache.xmlbeans.XmlException; 37 import org.apache.xmlbeans.SimpleValue; 38 import org.apache.xmlbeans.impl.schema.ResourceLoader; 39 import org.apache.xmlbeans.impl.schema.StscState; 40 import org.apache.xmlbeans.impl.schema.SchemaTypeLoaderImpl; 41 import org.apache.xmlbeans.impl.schema.SchemaTypeSystemCompiler; 42 import org.apache.xmlbeans.impl.schema.PathResourceLoader; 43 import org.apache.xmlbeans.impl.common.XmlErrorWatcher; 44 import org.apache.xmlbeans.impl.common.XmlErrorContext; 45 import org.apache.xmlbeans.impl.tool.SchemaCodeGenerator; 46 import org.apache.xmlbeans.impl.tool.CodeGenUtil; 47 import org.apache.xmlbeans.impl.tool.SchemaCompilerExtension; 48 import org.apache.xmlbeans.impl.tool.Extension; 49 import org.w3.x2001.xmlSchema.SchemaDocument; 50 import org.xml.sax.EntityResolver ; 51 import com.bea.x2002.x09.xbean.config.ConfigDocument; 52 53 60 public class SchemaCompiler { 61 public static class Parameters 62 { 63 private File baseDir; 64 private File [] xsdFiles; 65 private File [] wsdlFiles; 66 private File [] javaFiles; 67 private File [] configFiles; 68 private File [] classpath; 69 private File outputJar; 70 private String name; 71 private File srcDir; 72 private File classesDir; 73 private String memoryInitialSize; 74 private String memoryMaximumSize; 75 private String compiler; 76 private String jar; 77 private boolean nojavac; 78 private boolean quiet; 79 private boolean verbose; 80 private boolean download; 81 private Collection errorListener; 82 private boolean noUpa; 83 private boolean noPvr; 84 private boolean debug; 85 private String repackage; 86 private List extensions = Collections.EMPTY_LIST; 87 private boolean jaxb; 88 private Set mdefNamespaces = Collections.EMPTY_SET; 89 private EntityResolver entityResolver; 90 91 public File getBaseDir() 92 { 93 return baseDir; 94 } 95 96 public void setBaseDir(File baseDir) 97 { 98 this.baseDir = baseDir; 99 } 100 101 public File [] getXsdFiles() 102 { 103 return xsdFiles; 104 } 105 106 public void setXsdFiles(File [] xsdFiles) 107 { 108 this.xsdFiles = xsdFiles; 109 } 110 111 public File [] getWsdlFiles() 112 { 113 return wsdlFiles; 114 } 115 116 public void setWsdlFiles(File [] wsdlFiles) 117 { 118 this.wsdlFiles = wsdlFiles; 119 } 120 121 public File [] getJavaFiles() 122 { 123 return javaFiles; 124 } 125 126 public void setJavaFiles(File [] javaFiles) 127 { 128 this.javaFiles = javaFiles; 129 } 130 131 public File [] getConfigFiles() 132 { 133 return configFiles; 134 } 135 136 public void setConfigFiles(File [] configFiles) 137 { 138 this.configFiles = configFiles; 139 } 140 141 public File [] getClasspath() 142 { 143 return classpath; 144 } 145 146 public void setClasspath(File [] classpath) 147 { 148 this.classpath = classpath; 149 } 150 151 public File getOutputJar() 152 { 153 return outputJar; 154 } 155 156 public void setOutputJar(File outputJar) 157 { 158 this.outputJar = outputJar; 159 } 160 161 public String getName() 162 { 163 return name; 164 } 165 166 public void setName(String name) 167 { 168 this.name = name; 169 } 170 171 public File getSrcDir() 172 { 173 return srcDir; 174 } 175 176 public void setSrcDir(File srcDir) 177 { 178 this.srcDir = srcDir; 179 } 180 181 public File getClassesDir() 182 { 183 return classesDir; 184 } 185 186 public void setClassesDir(File classesDir) 187 { 188 this.classesDir = classesDir; 189 } 190 191 public boolean isNojavac() 192 { 193 return nojavac; 194 } 195 196 public void setNojavac(boolean nojavac) 197 { 198 this.nojavac = nojavac; 199 } 200 201 public boolean isQuiet() 202 { 203 return quiet; 204 } 205 206 public void setQuiet(boolean quiet) 207 { 208 this.quiet = quiet; 209 } 210 211 public boolean isVerbose() 212 { 213 return verbose; 214 } 215 216 public void setVerbose(boolean verbose) 217 { 218 this.verbose = verbose; 219 } 220 221 public boolean isDownload() 222 { 223 return download; 224 } 225 226 public void setDownload(boolean download) 227 { 228 this.download = download; 229 } 230 231 public boolean isNoUpa() 232 { 233 return noUpa; 234 } 235 236 public void setNoUpa(boolean noUpa) 237 { 238 this.noUpa = noUpa; 239 } 240 241 public boolean isNoPvr() 242 { 243 return noPvr; 244 } 245 246 public void setNoPvr(boolean noPvr) 247 { 248 this.noPvr = noPvr; 249 } 250 251 public boolean isDebug() 252 { 253 return debug; 254 } 255 256 public void setDebug(boolean debug) 257 { 258 this.debug = debug; 259 } 260 261 public String getMemoryInitialSize() 262 { 263 return memoryInitialSize; 264 } 265 266 public void setMemoryInitialSize(String memoryInitialSize) 267 { 268 this.memoryInitialSize = memoryInitialSize; 269 } 270 271 public String getMemoryMaximumSize() 272 { 273 return memoryMaximumSize; 274 } 275 276 public void setMemoryMaximumSize(String memoryMaximumSize) 277 { 278 this.memoryMaximumSize = memoryMaximumSize; 279 } 280 281 public String getCompiler() 282 { 283 return compiler; 284 } 285 286 public void setCompiler(String compiler) 287 { 288 this.compiler = compiler; 289 } 290 291 public String getJar() 292 { 293 return jar; 294 } 295 296 public void setJar(String jar) 297 { 298 this.jar = jar; 299 } 300 301 302 public void setJaxb(boolean jaxb) 303 { 304 this.jaxb = jaxb; 305 } 306 307 public boolean getJaxb() 308 { 309 return this.jaxb; 310 } 311 312 public Collection getErrorListener() 313 { 314 return errorListener; 315 } 316 317 public void setErrorListener(Collection errorListener) 318 { 319 this.errorListener = errorListener; 320 } 321 322 public String getRepackage() 323 { 324 return repackage; 325 } 326 327 public void setRepackage(String newRepackage) 328 { 329 repackage = newRepackage; 330 } 331 332 public List getExtensions() { 333 return extensions; 334 } 335 336 public void setExtensions(List extensions) { 337 this.extensions = extensions; 338 } 339 340 public Set getMdefNamespaces() 341 { 342 return mdefNamespaces; 343 } 344 345 public void setMdefNamespaces(Set mdefNamespaces) 346 { 347 this.mdefNamespaces = mdefNamespaces; 348 } 349 350 public EntityResolver getEntityResolver() { 351 return entityResolver; 352 } 353 354 public void setEntityResolver(EntityResolver entityResolver) { 355 this.entityResolver = entityResolver; 356 } 357 358 } 359 360 private static SchemaTypeSystem loadTypeSystem( 361 String name, File [] xsdFiles, 362 File [] wsdlFiles, File [] configFiles, ResourceLoader cpResourceLoader, 363 boolean download, boolean noUpa, boolean noPvr, Set mdefNamespaces, 364 File baseDir, Map sourcesToCopyMap, Collection outerErrorListener, 365 EntityResolver entityResolver) 366 { 367 XmlErrorWatcher errorListener = new XmlErrorWatcher(outerErrorListener); 368 369 375 SchemaTypeLoader loader = XmlBeans.typeLoaderForClassLoader(SchemaDocument.class.getClassLoader()); 376 377 ArrayList scontentlist = new ArrayList (); 379 if (xsdFiles != null) 380 { 381 for (int i = 0; i < xsdFiles.length; i++) 382 { 383 try 384 { 385 XmlOptions options = new XmlOptions(); 386 options.setLoadLineNumbers(); 387 options.setLoadMessageDigest(); 388 options.setEntityResolver(entityResolver); 389 390 XmlObject schemadoc = loader.parse(xsdFiles[i], null, options); 391 if (!(schemadoc instanceof SchemaDocument)) 392 { 393 StscState.addError(errorListener, "Document " + xsdFiles[i] + " is not a schema file", XmlErrorContext.CANNOT_LOAD_XSD_FILE, schemadoc); 394 } 395 else 396 { 397 StscState.addInfo(errorListener, "Loading schema file " + xsdFiles[i]); 398 XmlOptions opts = new XmlOptions().setErrorListener(errorListener); 399 if (schemadoc.validate(opts)) 400 scontentlist.add(((SchemaDocument)schemadoc).getSchema()); 401 } 402 } 403 catch (XmlException e) 404 { 405 errorListener.add(e.getError()); 406 } 407 catch (Exception e) 408 { 409 StscState.addError(errorListener, "Cannot load file " + xsdFiles[i] + ": " + e, XmlErrorContext.CANNOT_LOAD_XSD_FILE, xsdFiles[i]); 410 } 411 } 412 } 413 414 if (wsdlFiles != null) 416 { 417 for (int i = 0; i < wsdlFiles.length; i++) 418 { 419 try 420 { 421 XmlOptions options = new XmlOptions(); 422 options.setLoadLineNumbers(); 423 options.setEntityResolver(entityResolver); 424 options.setLoadSubstituteNamespaces(Collections.singletonMap( 425 "http://schemas.xmlsoap.org/wsdl/", "http://www.apache.org/internal/xmlbeans/wsdlsubst" 426 )); 427 428 429 XmlObject wsdldoc = loader.parse(wsdlFiles[i], null, options); 430 431 if (!(wsdldoc instanceof org.apache.internal.xmlbeans.wsdlsubst.DefinitionsDocument)) 432 StscState.addError(errorListener, "Document " + wsdlFiles[i] + " is not a wsdl file", XmlErrorContext.CANNOT_LOAD_XSD_FILE, wsdldoc); 433 else 434 { 435 if (wsdlContainsEncoded(wsdldoc)) 436 StscState.addWarning(errorListener, "The WSDL " + wsdlFiles[i] + " uses SOAP encoding. SOAP encoding is not compatible with literal XML Schema.", XmlErrorContext.CANNOT_LOAD_XSD_FILE, wsdldoc); 437 StscState.addInfo(errorListener, "Loading wsdl file " + wsdlFiles[i]); 438 XmlObject[] types = ((org.apache.internal.xmlbeans.wsdlsubst.DefinitionsDocument)wsdldoc).getDefinitions().getTypesArray(); 439 int count = 0; 440 for (int j = 0; j < types.length; j++) 441 { 442 SchemaDocument.Schema[] schemas = (SchemaDocument.Schema[])types[j].selectPath("declare namespace xs=\"http://www.w3.org/2001/XMLSchema\" xs:schema"); 444 for (int k = 0; k < schemas.length; k++) 445 { 446 if (schemas[k].validate(new XmlOptions().setErrorListener(errorListener))) 447 scontentlist.add(schemas[k]); 448 } 449 count += schemas.length; 450 } 451 StscState.addInfo(errorListener, "Processing " + count + " schema(s) in " + wsdlFiles[i].toString()); 452 } 453 } 454 catch (XmlException e) 455 { 456 errorListener.add(e.getError()); 457 } 458 catch (Exception e) 459 { 460 StscState.addError(errorListener, "Cannot load file " + wsdlFiles[i] + ": " + e, XmlErrorContext.CANNOT_LOAD_XSD_FILE, wsdlFiles[i]); 461 } 462 } 463 } 464 465 SchemaDocument.Schema[] sdocs = (SchemaDocument.Schema[])scontentlist.toArray(new SchemaDocument.Schema[scontentlist.size()]); 466 467 ArrayList cdoclist = new ArrayList (); 469 if (configFiles != null) 470 { 471 for (int i = 0; i < configFiles.length; i++) 472 { 473 try 474 { 475 XmlOptions options = new XmlOptions(); 476 options.put( XmlOptions.LOAD_LINE_NUMBERS ); 477 options.setEntityResolver(entityResolver); 478 479 XmlObject configdoc = loader.parse(configFiles[i], null, options); 480 if (!(configdoc instanceof ConfigDocument)) 481 StscState.addError(errorListener, "Document " + configFiles[i] + " is not an xsd config file", XmlErrorContext.CANNOT_LOAD_XSD_FILE, configdoc); 482 else 483 { 484 StscState.addInfo(errorListener, "Loading config file " + configFiles[i]); 485 if (configdoc.validate(new XmlOptions().setErrorListener(errorListener))) 486 cdoclist.add(((ConfigDocument)configdoc).getConfig()); 487 } 488 } 489 catch (XmlException e) 490 { 491 errorListener.add(e.getError()); 492 } 493 catch (Exception e) 494 { 495 StscState.addError(errorListener, "Cannot load xsd config file " + configFiles[i] + ": " + e, XmlErrorContext.CANNOT_LOAD_XSD_CONFIG_FILE, configFiles[i]); 496 } 497 } 498 } 499 ConfigDocument.Config[] cdocs = (ConfigDocument.Config[])cdoclist.toArray(new ConfigDocument.Config[cdoclist.size()]); 500 501 SchemaTypeLoader linkTo = SchemaTypeLoaderImpl.build(null, cpResourceLoader, null); 502 503 URI baseURI = null; 504 if (baseDir != null) 505 baseURI = baseDir.toURI(); 506 507 XmlOptions opts = new XmlOptions(); 508 if (download) 509 opts.setCompileDownloadUrls(); 510 if (noUpa) 511 opts.setCompileNoUpaRule(); 512 if (noPvr) 513 opts.setCompileNoPvrRule(); 514 if (mdefNamespaces != null) 515 opts.setCompileMdefNamespaces(mdefNamespaces); 516 opts.setCompileNoValidation(); opts.setEntityResolver(entityResolver); 518 519 SchemaTypeSystemCompiler.Parameters params = new SchemaTypeSystemCompiler.Parameters(); 521 params.setName(name); 522 params.setSchemas(sdocs); 523 params.setConfigs(cdocs); 524 params.setLinkTo(linkTo); 525 params.setOptions(opts); 526 params.setErrorListener(errorListener); 527 params.setJavaize(true); 528 params.setBaseURI(baseURI); 529 params.setSourcesToCopyMap(sourcesToCopyMap); 530 return SchemaTypeSystemCompiler.compile(params); 531 } 532 533 public static boolean compile(SchemaCompiler.Parameters params) 534 { 535 File baseDir = params.getBaseDir(); 536 File [] xsdFiles = params.getXsdFiles(); 537 File [] wsdlFiles = params.getWsdlFiles(); 538 File [] javaFiles = params.getJavaFiles(); 539 File [] configFiles = params.getConfigFiles(); 540 File [] classpath = params.getClasspath(); 541 File outputJar = params.getOutputJar(); 542 String name = params.getName(); 543 File srcDir = params.getSrcDir(); 544 File classesDir = params.getClassesDir(); 545 String compiler = params.getCompiler(); 546 String jar = params.getJar(); 547 String memoryInitialSize = params.getMemoryInitialSize(); 548 String memoryMaximumSize = params.getMemoryMaximumSize(); 549 boolean nojavac = params.isNojavac(); 550 boolean debug = params.isDebug(); 551 boolean verbose = params.isVerbose(); 552 boolean quiet = params.isQuiet(); 553 boolean download = params.isDownload(); 554 boolean noUpa = params.isNoUpa(); 555 boolean noPvr = params.isNoPvr(); 556 Collection outerErrorListener = params.getErrorListener(); 557 String repackage = params.getRepackage(); 558 List extensions = params.getExtensions(); 559 boolean jaxb = params.getJaxb(); 560 Set mdefNamespaces = params.getMdefNamespaces(); 561 EntityResolver entityResolver = params.getEntityResolver(); 562 563 if (srcDir == null || classesDir == null) 564 throw new IllegalArgumentException ("src and class gen directories may not be null."); 565 566 long start = System.currentTimeMillis(); 567 568 if (baseDir == null) 570 baseDir = new File (System.getProperty("user.dir")); 571 572 ResourceLoader cpResourceLoader = null; 573 574 Map sourcesToCopyMap = new HashMap (); 575 576 if (classpath != null) 577 cpResourceLoader = new PathResourceLoader(classpath); 578 579 boolean result = true; 580 581 XmlErrorWatcher errorListener = new XmlErrorWatcher(outerErrorListener); 583 SchemaTypeSystem system = loadTypeSystem(name, xsdFiles, wsdlFiles, configFiles, cpResourceLoader, download, noUpa, noPvr, mdefNamespaces, baseDir, sourcesToCopyMap, errorListener, entityResolver); 584 if (errorListener.hasError()) 585 result = false; 586 long finish = System.currentTimeMillis(); 587 if (!quiet) 588 System.out.println("Time to build schema type system: " + ((double)(finish - start) / 1000.0) + " seconds" ); 589 590 if (result && system != null) { 593 start = System.currentTimeMillis(); 594 595 List sourcefiles = new ArrayList (); 597 result &= SchemaCodeGenerator.compileTypeSystem(system, srcDir, javaFiles, sourcesToCopyMap, classpath, classesDir, outputJar, nojavac, jaxb, errorListener, repackage, verbose, sourcefiles); 598 result &= !errorListener.hasError(); 599 600 if (result) 601 { 602 finish = System.currentTimeMillis(); 603 if (!quiet) 604 System.out.println("Time to generate code: " + ((double)(finish - start) / 1000.0) + " seconds" ); 605 } 606 607 if (result && !nojavac) 609 { 610 start = System.currentTimeMillis(); 611 612 if (javaFiles != null) 613 sourcefiles.addAll(java.util.Arrays.asList(javaFiles)); 614 if (!CodeGenUtil.externalCompile(sourcefiles, classesDir, classpath, debug, compiler, memoryInitialSize, memoryMaximumSize, quiet, verbose)) 615 result = false; 616 617 finish = System.currentTimeMillis(); 618 if (result && !params.isQuiet()) 619 System.out.println("Time to compile code: " + ((double)(finish - start) / 1000.0) + " seconds" ); 620 621 if (result && outputJar != null) 623 { 624 if (!CodeGenUtil.externalJar(classesDir, outputJar, jar, quiet, verbose)) 625 result = false; 626 627 if (result && !params.isQuiet()) 628 System.out.println("Compiled types to: " + outputJar); 629 } 630 } 631 } 632 633 if (!result && !quiet) 634 { 635 System.out.println("BUILD FAILED"); 636 } 637 else { 638 runExtensions(extensions, system, classesDir); 640 } 641 642 if (cpResourceLoader != null) 643 cpResourceLoader.close(); 644 return result; 645 } 646 647 private static void runExtensions(List extensions, SchemaTypeSystem system, File classesDir) 648 { 649 if (extensions != null && extensions.size() > 0) 650 { 651 SchemaCompilerExtension sce = null; 652 Iterator i = extensions.iterator(); 653 Map extensionParms = null; 654 String classesDirName = null; 655 try 656 { 657 classesDirName = classesDir.getCanonicalPath(); 658 } 659 catch(java.io.IOException e) 660 { 661 System.out.println("WARNING: Unable to get the path for schema jar file"); 662 classesDirName = classesDir.getAbsolutePath(); 663 } 664 665 while (i.hasNext()) 666 { 667 Extension extension = (Extension) i.next(); 668 try 669 { 670 sce = (SchemaCompilerExtension) extension.getClassName().newInstance(); 671 } 672 catch (InstantiationException e) 673 { 674 System.out.println("UNABLE to instantiate schema compiler extension:" + extension.getClassName().getName()); 675 System.out.println("EXTENSION Class was not run"); 676 break; 677 } 678 catch (IllegalAccessException e) 679 { 680 System.out.println("ILLEGAL ACCESS Exception when attempting to instantiate schema compiler extension: " + extension.getClassName().getName()); 681 System.out.println("EXTENSION Class was not run"); 682 break; 683 } 684 685 System.out.println("Running Extension: " + sce.getExtensionName()); 686 extensionParms = new HashMap (); 687 Iterator parmsi = extension.getParams().iterator(); 688 while (parmsi.hasNext()) 689 { 690 Extension.Param p = (Extension.Param) parmsi.next(); 691 extensionParms.put(p.getName(), p.getValue()); 692 } 693 extensionParms.put("classesDir", classesDirName); 694 sce.schemaCompilerExtension(system, extensionParms); 695 } 696 } 697 } 698 699 700 private static boolean wsdlContainsEncoded(XmlObject wsdldoc) 701 { 702 XmlObject[] useAttrs = wsdldoc.selectPath( 704 "declare namespace soap='http://schemas.xmlsoap.org/wsdl/soap/' " + 705 ".//soap:body/@use|.//soap:header/@use|.//soap:fault/@use"); 706 for (int i = 0; i < useAttrs.length; i++) 707 { 708 if ("encoded".equals(((SimpleValue)useAttrs[i]).getStringValue())) 709 return true; 710 } 711 return false; 712 } 713 714 715 } 716 717 | Popular Tags |