KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > tools > xmlbeans > SchemaCompilerWrapper


1 /**
2  *
3  * Copyright 2003-2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.tools.xmlbeans;
19
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.StringTokenizer JavaDoc;
27
28 import org.xml.sax.EntityResolver JavaDoc;
29 import org.xml.sax.InputSource JavaDoc;
30 import org.xml.sax.SAXException JavaDoc;
31 import org.apache.xml.resolver.CatalogManager;
32 import org.apache.xml.resolver.tools.CatalogResolver;
33
34
35 /**
36  *
37  *
38  * @version $Rev: 46019 $ $Date: 2004-09-14 02:56:06 -0700 (Tue, 14 Sep 2004) $
39  *
40  * */

41 public class SchemaCompilerWrapper {
42
43     public static void CompileSchemas(String JavaDoc sourceDir, String JavaDoc sourceSchemas, String JavaDoc xmlConfigs, String JavaDoc targetDir, String JavaDoc catalogLocation, String JavaDoc classpath) throws Exception JavaDoc {
44         List JavaDoc schemas = new ArrayList JavaDoc();
45         File JavaDoc base = new File JavaDoc(sourceDir);
46         for (StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(sourceSchemas, ","); st.hasMoreTokens();) {
47             String JavaDoc schemaName = st.nextToken();
48             schemas.add(new File JavaDoc(base, schemaName));
49         }
50         List JavaDoc configs = new ArrayList JavaDoc();
51
52         if (xmlConfigs != null) {
53             for (StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(xmlConfigs, ","); st.hasMoreTokens();) {
54                 String JavaDoc configName = st.nextToken();
55                 configs.add(new File JavaDoc(configName));
56             }
57         }
58         EntityResolver JavaDoc entityResolver = null;
59         if (catalogLocation != null) {
60             CatalogManager catalogManager = CatalogManager.getStaticManager();
61             catalogManager.setCatalogFiles(catalogLocation);
62             entityResolver = new PassThroughResolver(new CatalogResolver());
63         }
64         List JavaDoc classPathList = new ArrayList JavaDoc();
65         if (classpath != null) {
66             for (StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(classpath, ","); st.hasMoreTokens();) {
67                 String JavaDoc classpathElement = st.nextToken();
68                 classPathList.add(new File JavaDoc(classpathElement));
69             }
70         }
71         SchemaCompiler.Parameters params = new SchemaCompiler.Parameters();
72         params.setBaseDir(null);
73         params.setXsdFiles((File JavaDoc[])schemas.toArray(new File JavaDoc[] {}));
74         params.setWsdlFiles(new File JavaDoc[] {});
75         params.setJavaFiles(new File JavaDoc[] {});
76         params.setConfigFiles((File JavaDoc[])configs.toArray(new File JavaDoc[] {}));
77         params.setClasspath((File JavaDoc[])classPathList.toArray(new File JavaDoc[] {}));
78         params.setOutputJar(null);
79         params.setName(null);
80         params.setSrcDir(new File JavaDoc(targetDir));
81         params.setClassesDir(new File JavaDoc(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 JavaDoc());
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 JavaDoc errors = params.getErrorListener();
103             for (Iterator JavaDoc iterator = errors.iterator(); iterator.hasNext();) {
104                 Object JavaDoc o = (Object JavaDoc) iterator.next();
105                 System.out.println("xmlbeans error: " + o);
106             }
107             throw new Exception JavaDoc("Schema compilation failed");
108         }
109
110     }
111
112     private static class PassThroughResolver implements EntityResolver JavaDoc {
113
114         private final EntityResolver JavaDoc delegate;
115
116         public PassThroughResolver(EntityResolver JavaDoc delegate) {
117             this.delegate = delegate;
118         }
119         public InputSource JavaDoc resolveEntity(String JavaDoc publicId,
120                                          String JavaDoc systemId)
121                 throws SAXException JavaDoc, IOException JavaDoc {
122             InputSource JavaDoc 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 JavaDoc(systemId);
128         }
129
130     }
131 }
132
Popular Tags