Home » xmlbeans-2.5.0-src » org.apache » xmlbeans » maven » [javadoc | source]

    1   /**
    2    *
    3    * Copyright 2003-2005 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.xmlbeans.maven;
   19   
   20   import java.io.File;
   21   import java.io.IOException;
   22   import java.io.InputStream;
   23   import java.net.URI;
   24   import java.net.URISyntaxException;
   25   import java.net.URL;
   26   import java.net.URLClassLoader;
   27   import java.util.ArrayList;
   28   import java.util.Collection;
   29   import java.util.Iterator;
   30   import java.util.List;
   31   import java.util.StringTokenizer;
   32   
   33   import org.apache.maven.project.Resource;
   34   import org.apache.xml.resolver.CatalogManager;
   35   import org.apache.xml.resolver.tools.CatalogResolver;
   36   import org.apache.xmlbeans.impl.tool.SchemaCompiler;
   37   import org.xml.sax.EntityResolver;
   38   import org.xml.sax.InputSource;
   39   import org.xml.sax.SAXException;
   40   
   41   
   42   /**
   43    *
   44    *
   45    * @version $Rev: 46019 $ $Date: 2004-09-14 02:56:06 -0700 (Tue, 14 Sep 2004) $
   46    *
   47    * */
   48   public class SchemaCompilerWrapper {
   49   
   50       private String sourceDir;
   51       private String sourceSchemas;
   52       private String xmlConfigs;
   53       private String javaTargetDir;
   54       private String classTargetDir;
   55       private String catalogLocation;
   56       private String classPath;
   57       private List resources;
   58       private boolean buildSchemas;
   59       //this copy should not end in /
   60       private String baseSchemaLocation = "schemaorg_apache_xmlbeans/src";
   61   
   62       public String getSourceDir() {
   63           return sourceDir;
   64       }
   65   
   66       public void setSourceDir(String sourceDir) {
   67           this.sourceDir = sourceDir;
   68       }
   69   
   70       public String getSourceSchemas() {
   71           return sourceSchemas;
   72       }
   73   
   74       public void setSourceSchemas(String sourceSchemas) {
   75           this.sourceSchemas = sourceSchemas;
   76       }
   77   
   78       public String getXmlConfigs() {
   79           return xmlConfigs;
   80       }
   81   
   82       public void setXmlConfigs(String xmlConfigs) {
   83           this.xmlConfigs = xmlConfigs;
   84       }
   85   
   86       public String getJavaTargetDir() {
   87           return javaTargetDir;
   88       }
   89   
   90       public void setJavaTargetDir(String javaTargetDir) {
   91           this.javaTargetDir = javaTargetDir;
   92       }
   93   
   94       public String getClassTargetDir() {
   95           return classTargetDir;
   96       }
   97   
   98       public void setClassTargetDir(String classTargetDir) {
   99           this.classTargetDir = classTargetDir;
  100       }
  101   
  102       public String getCatalogLocation() {
  103           return catalogLocation;
  104       }
  105   
  106       public void setCatalogLocation(String catalogLocation) {
  107           this.catalogLocation = catalogLocation;
  108       }
  109   
  110       public String getClassPath() {
  111           return classPath;
  112       }
  113   
  114       public void setClassPath(String classPath) {
  115           this.classPath = classPath;
  116       }
  117   
  118       public List getResources() {
  119           return resources;
  120       }
  121   
  122       public void setResources(List resources) {
  123           this.resources = resources;
  124       }
  125   
  126       public boolean isBuildSchemas() {
  127           return buildSchemas;
  128       }
  129   
  130       public void setBuildSchemas(boolean buildSchemas) {
  131           this.buildSchemas = buildSchemas;
  132       }
  133   
  134       public String getBaseSchemaLocation() {
  135           return baseSchemaLocation;
  136       }
  137   
  138       public void setBaseSchemaLocation(String baseSchemaLocation) {
  139           if (baseSchemaLocation != null && !(baseSchemaLocation.length() == 0)) {
  140               this.baseSchemaLocation = baseSchemaLocation;
  141           }
  142       }
  143   
  144       public void compileSchemas() throws Exception {
  145           List schemas = new ArrayList();
  146           File base = new File(sourceDir);
  147           Resource resource = new Resource();
  148           resource.setDirectory(sourceDir);
  149           resource.setTargetPath(baseSchemaLocation);
  150           for (StringTokenizer st = new StringTokenizer(sourceSchemas, ","); st.hasMoreTokens();) {
  151               String schemaName = st.nextToken();
  152               schemas.add(new File(base, schemaName));
  153               resource.addInclude(schemaName);
  154           }
  155           resources.add(resource);
  156           if (buildSchemas) {
  157               List configs = new ArrayList();
  158   
  159               if (xmlConfigs != null) {
  160                   for (StringTokenizer st = new StringTokenizer(xmlConfigs, ","); st.hasMoreTokens();) {
  161                       String configName = st.nextToken();
  162                       configs.add(new File(configName));
  163                   }
  164               }
  165               List classPathList = new ArrayList();
  166               List urls = new ArrayList();
  167               if (classPath != null) {
  168                   for (StringTokenizer st = new StringTokenizer(classPath, ","); st.hasMoreTokens();) {
  169                       String classpathElement = st.nextToken();
  170                       File file = new File(classpathElement);
  171                       classPathList.add(file);
  172                       urls.add(file.toURL());
  173                       System.out.println("Adding to classpath: " + file);
  174                   }
  175               }
  176               ClassLoader cl = new URLClassLoader((URL[]) urls.toArray(new URL[] {}));
  177               EntityResolver entityResolver = null;
  178               if (catalogLocation != null) {
  179                   CatalogManager catalogManager = CatalogManager.getStaticManager();
  180                   catalogManager.setCatalogFiles(catalogLocation);
  181                   entityResolver = new CatalogResolver();
  182               }
  183               URI sourceDirURI = new File(sourceDir).toURI();
  184               entityResolver = new PassThroughResolver(cl, entityResolver, sourceDirURI, baseSchemaLocation);
  185   
  186               SchemaCompiler.Parameters params = new SchemaCompiler.Parameters();
  187               params.setBaseDir(null);
  188               params.setXsdFiles((File[])schemas.toArray(new File[] {}));
  189               params.setWsdlFiles(new File[] {});
  190               params.setJavaFiles(new File[] {});
  191               params.setConfigFiles((File[])configs.toArray(new File[] {}));
  192               params.setClasspath((File[])classPathList.toArray(new File[] {}));
  193               params.setOutputJar(null);
  194               params.setName(null);
  195               params.setSrcDir(new File(javaTargetDir));
  196               params.setClassesDir(new File(classTargetDir));
  197               params.setCompiler(null);
  198               params.setMemoryInitialSize(null);
  199               params.setMemoryMaximumSize(null);
  200               params.setNojavac(true);
  201               params.setQuiet(false);
  202               params.setVerbose(true);
  203               params.setDownload(false);
  204               params.setNoUpa(false);
  205               params.setNoPvr(false);
  206               params.setDebug(true);
  207               params.setErrorListener(new ArrayList());
  208               params.setRepackage(null);
  209               params.setExtensions(null);
  210               params.setMdefNamespaces(null);
  211               params.setEntityResolver(entityResolver);
  212   
  213               boolean result = SchemaCompiler.compile(params);
  214               if (!result) {
  215                   Collection errors = params.getErrorListener();
  216                   for (Iterator iterator = errors.iterator(); iterator.hasNext();) {
  217                       Object o = (Object) iterator.next();
  218                       System.out.println("xmlbeans error: " + o);
  219                   }
  220                   throw new Exception("Schema compilation failed");
  221               }
  222           }
  223   
  224       }
  225   
  226       private static class PassThroughResolver implements EntityResolver {
  227           private final ClassLoader cl;
  228           private final EntityResolver delegate;
  229           private final URI sourceDir;
  230           //this copy has an / appended
  231           private final String baseSchemaLocation;
  232   
  233           public PassThroughResolver(ClassLoader cl, EntityResolver delegate, URI sourceDir, String baseSchemaLocation) {
  234               this.cl = cl;
  235               this.delegate = delegate;
  236               this.sourceDir = sourceDir;
  237               this.baseSchemaLocation = baseSchemaLocation + "/";
  238           }
  239           public InputSource resolveEntity(String publicId,
  240                                            String systemId)
  241                   throws SAXException, IOException {
  242               if (delegate != null) {
  243                   InputSource is = delegate.resolveEntity(publicId, systemId);
  244                   if (is != null) {
  245                       return is;
  246                   }
  247               }
  248               System.out.println("Could not resolve publicId: " + publicId + ", systemId: " + systemId + " from catalog");
  249               String localSystemId;
  250               try {
  251                    localSystemId = sourceDir.relativize(new URI(systemId)).toString();
  252               } catch (URISyntaxException e) {
  253                   throw (IOException)new IOException("Could not relativeize systemId").initCause(e);
  254               }
  255               InputStream in = cl.getResourceAsStream(localSystemId);
  256               if (in != null) {
  257                   System.out.println("found in classpath at: " + localSystemId);
  258                   return new InputSource(in);
  259               }
  260               in = cl.getResourceAsStream(baseSchemaLocation + localSystemId);
  261               if (in != null) {
  262                   System.out.println("found in classpath at: META-INF/" + localSystemId);
  263                   return new InputSource(in);
  264               }
  265               System.out.println("Not found in classpath, looking in current directory: " + systemId);
  266               return new InputSource(systemId);
  267           }
  268   
  269       }
  270   }

Home » xmlbeans-2.5.0-src » org.apache » xmlbeans » maven » [javadoc | source]