1 17 18 package org.apache.geronimo.tools.xmlbeans; 19 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.StringTokenizer ; 27 28 import org.xml.sax.EntityResolver ; 29 import org.xml.sax.InputSource ; 30 import org.xml.sax.SAXException ; 31 import org.apache.xml.resolver.CatalogManager; 32 import org.apache.xml.resolver.tools.CatalogResolver; 33 34 35 41 public class SchemaCompilerWrapper { 42 43 public static void CompileSchemas(String sourceDir, String sourceSchemas, String xmlConfigs, String targetDir, String catalogLocation, String classpath) throws Exception { 44 List schemas = new ArrayList (); 45 File base = new File (sourceDir); 46 for (StringTokenizer st = new StringTokenizer (sourceSchemas, ","); st.hasMoreTokens();) { 47 String schemaName = st.nextToken(); 48 schemas.add(new File (base, schemaName)); 49 } 50 List configs = new ArrayList (); 51 52 if (xmlConfigs != null) { 53 for (StringTokenizer st = new StringTokenizer (xmlConfigs, ","); st.hasMoreTokens();) { 54 String configName = st.nextToken(); 55 configs.add(new File (configName)); 56 } 57 } 58 EntityResolver entityResolver = null; 59 if (catalogLocation != null) { 60 CatalogManager catalogManager = CatalogManager.getStaticManager(); 61 catalogManager.setCatalogFiles(catalogLocation); 62 entityResolver = new PassThroughResolver(new CatalogResolver()); 63 } 64 List classPathList = new ArrayList (); 65 if (classpath != null) { 66 for (StringTokenizer st = new StringTokenizer (classpath, ","); st.hasMoreTokens();) { 67 String classpathElement = st.nextToken(); 68 classPathList.add(new File (classpathElement)); 69 } 70 } 71 SchemaCompiler.Parameters params = new SchemaCompiler.Parameters(); 72 params.setBaseDir(null); 73 params.setXsdFiles((File [])schemas.toArray(new File [] {})); 74 params.setWsdlFiles(new File [] {}); 75 params.setJavaFiles(new File [] {}); 76 params.setConfigFiles((File [])configs.toArray(new File [] {})); 77 params.setClasspath((File [])classPathList.toArray(new File [] {})); 78 params.setOutputJar(null); 79 params.setName(null); 80 params.setSrcDir(new File (targetDir)); 81 params.setClassesDir(new File (targetDir)); 82 params.setCompiler(null); 83 params.setJar(null); 84 params.setMemoryInitialSize(null); 85 params.setMemoryMaximumSize(null); 86 params.setNojavac(true); 87 params.setQuiet(false); 88 params.setVerbose(true); 89 params.setDownload(false); 90 params.setNoUpa(false); 91 params.setNoPvr(false); 92 params.setDebug(true); 93 params.setErrorListener(new ArrayList ()); 94 params.setRepackage(null); 95 params.setExtensions(null); 96 params.setJaxb(false); 97 params.setMdefNamespaces(null); 98 params.setEntityResolver(entityResolver); 99 100 boolean result = SchemaCompiler.compile(params); 101 if (!result) { 102 Collection errors = params.getErrorListener(); 103 for (Iterator iterator = errors.iterator(); iterator.hasNext();) { 104 Object o = (Object ) iterator.next(); 105 System.out.println("xmlbeans error: " + o); 106 } 107 throw new Exception ("Schema compilation failed"); 108 } 109 110 } 111 112 private static class PassThroughResolver implements EntityResolver { 113 114 private final EntityResolver delegate; 115 116 public PassThroughResolver(EntityResolver delegate) { 117 this.delegate = delegate; 118 } 119 public InputSource resolveEntity(String publicId, 120 String systemId) 121 throws SAXException , IOException { 122 InputSource is = delegate.resolveEntity(publicId, systemId); 123 if (is != null) { 124 return is; 125 } 126 System.out.println("Could not resolve publicId: " + publicId + ", systemId: " + systemId + " from catalog"); 127 return new InputSource (systemId); 128 } 129 130 } 131 } 132 | Popular Tags |