xmlbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From eric...@apache.org
Subject cvs commit: xml-xmlbeans/v2/src/xmlpublic/org/apache/xmlbeans SchemaCodePrinter.java XmlOptions.java
Date Thu, 18 Mar 2004 23:28:00 GMT
ericvas     2004/03/18 15:28:00

  Modified:    v2/src/typeimpl/org/apache/xmlbeans/impl/schema
                        SchemaTypeCodePrinter.java
               v2/src/xmlcomp/org/apache/xmlbeans/impl/tool
                        SchemaCodeGenerator.java SchemaCompiler.java
               v2/src/xmlpublic/org/apache/xmlbeans XmlOptions.java
  Added:       v2/src/xmlpublic/org/apache/xmlbeans SchemaCodePrinter.java
  Log:
  Add plugability for code generation
  
  Revision  Changes    Path
  1.7       +46 -26    xml-xmlbeans/v2/src/typeimpl/org/apache/xmlbeans/impl/schema/SchemaTypeCodePrinter.java
  
  Index: SchemaTypeCodePrinter.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/typeimpl/org/apache/xmlbeans/impl/schema/SchemaTypeCodePrinter.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- SchemaTypeCodePrinter.java	12 Feb 2004 20:06:21 -0000	1.6
  +++ SchemaTypeCodePrinter.java	18 Mar 2004 23:27:59 -0000	1.7
  @@ -27,11 +27,13 @@
   import org.apache.xmlbeans.SchemaProperty;
   import org.apache.xmlbeans.SchemaStringEnumEntry;
   import org.apache.xmlbeans.XmlObject;
  +import org.apache.xmlbeans.XmlOptions;
  +import org.apache.xmlbeans.SchemaCodePrinter;
   
   /**
    * Prints the java code for a single schema type
    */
  -public final class SchemaTypeCodePrinter
  +public final class SchemaTypeCodePrinter implements SchemaCodePrinter
   {
       Writer _writer;
       int    _indent;
  @@ -46,9 +48,40 @@
   
       public static final String INDEX_CLASSNAME = "TypeSystemHolder";
   
  -    public SchemaTypeCodePrinter ( Writer writer )
  +    public static void printTypeImpl ( Writer writer, SchemaType sType,
  +                                       XmlOptions opt )
  +        throws IOException
  +    {
  +        getPrinter(opt).printTypeImpl( writer, sType );
  +    }
  +
  +    public static void printType ( Writer writer, SchemaType sType,
  +                                   XmlOptions opt )
  +        throws IOException
  +    {
  +        getPrinter(opt).printType( writer, sType );
  +    }
  +
  +    public static void printLoader ( Writer writer, SchemaTypeSystem system,
  +                                     XmlOptions opt )
  +        throws IOException
  +    {
  +        getPrinter(opt).printLoader( writer, system );
  +    }
  +
  +    private static SchemaCodePrinter getPrinter(XmlOptions opt)
  +    {
  +        Object printer = XmlOptions.safeGet
  +            (opt, XmlOptions.SCHEMA_CODE_PRINTER);
  +        if (printer == null || !(printer instanceof SchemaCodePrinter))
  +        {
  +            printer = new SchemaTypeCodePrinter();
  +        }
  +        return (SchemaCodePrinter) printer;
  +    }
  +
  +    public SchemaTypeCodePrinter ()
       {
  -        _writer = writer;
           _indent = 0;
       }
   
  @@ -124,38 +157,23 @@
           // System.out.println(s);
       }
   
  -    public static void printTypeImpl ( Writer writer, SchemaType sType )
  -        throws IOException
  -    {
  -        new SchemaTypeCodePrinter( writer ).printTypeImpl( sType, sType.getTypeSystem()
);
  -    }
  -
  -    public static void printType ( Writer writer, SchemaType sType )
  -        throws IOException
  -    {
  -        new SchemaTypeCodePrinter( writer ). printType( sType, sType.getTypeSystem() );
  -    }
  -
  -    public static void printLoader ( Writer writer, SchemaTypeSystem system )
  -        throws IOException
  -    {
  -        new SchemaTypeCodePrinter( writer ).printIndexType( system );
  -    }
  -
  -    void printType(SchemaType sType, SchemaTypeSystem system) throws IOException
  +    public void printType(Writer writer, SchemaType sType) throws IOException
       {
  +        _writer = writer;
           printTopComment(sType);
           printPackage(sType, true);
           emit("");
  -        printInnerType(sType, system);
  +        printInnerType(sType, sType.getTypeSystem());
           _writer.flush();
       }
   
  -    void printTypeImpl(SchemaType sType, SchemaTypeSystem system) throws IOException
  +    public void printTypeImpl(Writer writer, SchemaType sType) 
  +        throws IOException
       {
  +        _writer = writer;
           printTopComment(sType);
           printPackage(sType, false);
  -        printInnerTypeImpl(sType, system, false);
  +        printInnerTypeImpl(sType, sType.getTypeSystem(), false);
       }
   
       /**
  @@ -241,8 +259,10 @@
                   ((SchemaTypeSystemImpl)system).handleForType(sType) + "\");");
       }
   
  -    void printIndexType(SchemaTypeSystem system) throws IOException
  +    public void printLoader(Writer writer, SchemaTypeSystem system)
  +        throws IOException
       {
  +        _writer = writer;
           String shortName = shortIndexClassForSystem(system);
           emit("package " + system.getName() + ";");
           emit("");
  
  
  
  1.6       +16 -6     xml-xmlbeans/v2/src/xmlcomp/org/apache/xmlbeans/impl/tool/SchemaCodeGenerator.java
  
  Index: SchemaCodeGenerator.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/xmlcomp/org/apache/xmlbeans/impl/tool/SchemaCodeGenerator.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- SchemaCodeGenerator.java	3 Mar 2004 22:56:39 -0000	1.5
  +++ SchemaCodeGenerator.java	18 Mar 2004 23:28:00 -0000	1.6
  @@ -19,6 +19,8 @@
   import org.apache.xmlbeans.SchemaTypeSystem;
   import org.apache.xmlbeans.impl.common.XmlErrorWatcher;
   import org.apache.xmlbeans.impl.schema.SchemaTypeCodePrinter;
  +import org.apache.xmlbeans.SchemaCodePrinter;
  +import org.apache.xmlbeans.XmlOptions;
   import repackage.Repackager;
   
   import java.io.File;
  @@ -40,7 +42,7 @@
       // todo: output jar
       public static boolean compileTypeSystem(SchemaTypeSystem saver, File sourcedir, File[]
javasrc,
            Map sourcesToCopyMap, File[] classpath, File classesdir, File outputJar, boolean
nojavac,
  -         XmlErrorWatcher errors, String repackage, boolean verbose, List sourcefiles,
  +         XmlErrorWatcher errors, String repackage, SchemaCodePrinter codePrinter, boolean
verbose, List sourcefiles,
            File schemasDir)
       {
   
  @@ -87,6 +89,14 @@
   
           Repackager repackager = repackage == null ? null : new Repackager( repackage );
   
  +        // Create XmlOptions - currently just for SchemaCodePrinter,
  +        // but could be used more in future
  +        XmlOptions opts = new XmlOptions();
  +        if (codePrinter != null)
  +        {
  +            opts.setSchemaCodePrinter(codePrinter);
  +        }
  +
           try
           {
               String filename = SchemaTypeCodePrinter.indexClassForSystem(saver).replace('.',
File.separatorChar) + ".java";
  @@ -98,7 +108,7 @@
                       ? (Writer) new FileWriter( sourcefile )
                       : (Writer) new RepackagingWriter( sourcefile, repackager );
                               
  -            SchemaTypeCodePrinter.printLoader(writer, saver);
  +            SchemaTypeCodePrinter.printLoader(writer, saver, opts);
               
               writer.close();
               
  @@ -110,7 +120,7 @@
               failure = true;
           }
   
  -        failure &= genTypes(saver, sourcefiles, sourcedir, repackager, verbose);
  +        failure &= genTypes(saver, sourcefiles, sourcedir, repackager, verbose, opts);
   
           if (failure)
               return false;
  @@ -118,7 +128,7 @@
           return true;
       }
   
  -    private static boolean genTypes(SchemaTypeSystem saver, List sourcefiles, File sourcedir,
Repackager repackager, boolean verbose)
  +    private static boolean genTypes(SchemaTypeSystem saver, List sourcefiles, File sourcedir,
Repackager repackager, boolean verbose, XmlOptions opts)
       {
           boolean failure = false;
   
  @@ -162,7 +172,7 @@
                           : (Writer) new RepackagingWriter( sourcefile, repackager );
                   
   
  -                SchemaTypeCodePrinter.printType(writer, type);
  +                SchemaTypeCodePrinter.printType(writer, type, opts);
                   
                   writer.close();
                   
  @@ -192,7 +202,7 @@
                           ? (Writer) new FileWriter( implFile )
                           : (Writer) new RepackagingWriter( implFile, repackager );
                   
  -                SchemaTypeCodePrinter.printTypeImpl(writer, type);
  +                SchemaTypeCodePrinter.printTypeImpl(writer, type, opts);
                   
                   writer.close();
                   
  
  
  
  1.8       +33 -1     xml-xmlbeans/v2/src/xmlcomp/org/apache/xmlbeans/impl/tool/SchemaCompiler.java
  
  Index: SchemaCompiler.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/xmlcomp/org/apache/xmlbeans/impl/tool/SchemaCompiler.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- SchemaCompiler.java	6 Mar 2004 00:23:53 -0000	1.7
  +++ SchemaCompiler.java	18 Mar 2004 23:28:00 -0000	1.8
  @@ -28,6 +28,7 @@
   import org.apache.xmlbeans.impl.values.XmlListImpl;
   import org.apache.xmlbeans.SchemaTypeSystem;
   import org.apache.xmlbeans.SchemaTypeLoader;
  +import org.apache.xmlbeans.SchemaCodePrinter;
   import org.apache.xmlbeans.XmlBeans;
   import org.apache.xmlbeans.XmlOptions;
   import org.apache.xmlbeans.XmlObject;
  @@ -74,6 +75,7 @@
               System.out.println("    -repackage - repackage specification");
               System.out.println("    -extension - registers a schema compiler extension");
               System.out.println("    -extensionParms - specify parameters for the compiler
extension");
  +            System.out.println("    -schemaCodePrinter - specify SchemaCodePrinter class");
               */
               System.out.println();
               System.out.println("If you require a different java compiler, use the XMLBean
Ant task instead.");
  @@ -92,6 +94,7 @@
           opts.add("ms");
           opts.add("mx");
           opts.add("repackage");
  +        opts.add("schemaCodePrinter");
           opts.add("extension");
           opts.add("extensionParms");
           opts.add("allowmdef");
  @@ -115,6 +118,23 @@
   
           String repackage = cl.getOpt("repackage");
   
  +        String codePrinterClass = cl.getOpt("schemaCodePrinter");
  +        SchemaCodePrinter codePrinter = null;
  +        if (codePrinterClass != null)
  +        {
  +            try
  +            {
  +                codePrinter = (SchemaCodePrinter)
  +                    Class.forName(codePrinterClass).newInstance();
  +            }
  +            catch (Exception e)
  +            {
  +                System.err.println
  +                    ("Failed to load SchemaCodePrinter class " +
  +                     codePrinterClass + "; proceeding with default printer");
  +            }
  +        }
  +
           String name = cl.getOpt("name");
   
           boolean download = (cl.getOpt("dl") != null);
  @@ -269,6 +289,7 @@
           params.setExtensions(extensions);
           params.setMdefNamespaces(mdefNamespaces);
           params.setCatalogFile(catString);
  +        params.setSchemaCodePrinter(codePrinter);
   
           boolean result = compile(params);
   
  @@ -310,6 +331,7 @@
           private List extensions = Collections.EMPTY_LIST;
           private Set mdefNamespaces = Collections.EMPTY_SET;
           private String catalogFile;
  +        private SchemaCodePrinter schemaCodePrinter;
   
           public File getBaseDir()
           {
  @@ -579,6 +601,15 @@
               this.catalogFile = catalogPropFile;
           }
   
  +        public SchemaCodePrinter getSchemaCodePrinter()
  +        {
  +            return schemaCodePrinter;
  +        }
  +
  +        public void setSchemaCodePrinter(SchemaCodePrinter schemaCodePrinter)
  +        {
  +            this.schemaCodePrinter = schemaCodePrinter;
  +        }
       }
   
       private static SchemaTypeSystem loadTypeSystem(
  @@ -783,6 +814,7 @@
           boolean noAnn = params.isNoAnn();
           Collection outerErrorListener = params.getErrorListener();
           String repackage = params.getRepackage();
  +        SchemaCodePrinter codePrinter = params.getSchemaCodePrinter();
           List extensions = params.getExtensions();
           Set mdefNamespaces = params.getMdefNamespaces();
   
  @@ -827,7 +859,7 @@
               // generate source and .xsb
               List sourcefiles = new ArrayList();
               result &= SchemaCodeGenerator.compileTypeSystem(system, srcDir, javaFiles,
sourcesToCopyMap,
  -                classpath, classesDir, outputJar, nojavac, errorListener, repackage, verbose,
  +                classpath, classesDir, outputJar, nojavac, errorListener, repackage, codePrinter,
verbose,
                   sourcefiles, schemasDir);
               result &= !errorListener.hasError();
   
  
  
  
  1.5       +13 -2     xml-xmlbeans/v2/src/xmlpublic/org/apache/xmlbeans/XmlOptions.java
  
  Index: XmlOptions.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/xmlpublic/org/apache/xmlbeans/XmlOptions.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- XmlOptions.java	16 Mar 2004 00:55:01 -0000	1.4
  +++ XmlOptions.java	18 Mar 2004 23:28:00 -0000	1.5
  @@ -557,7 +557,17 @@
       public XmlOptions setEntityResolver(EntityResolver resolver) {
           return set( ENTITY_RESOLVER, resolver );
       }
  -
  +    
  +    /**
  +     * If this option is set when compiling a schema, then the given
  +     * SchemaTypeCodePrinter.Printer will be used to generate the
  +     * Java code.
  +     * 
  +     * @see XmlBeans#compileXsd
  +     */
  +    public XmlOptions setSchemaCodePrinter(SchemaCodePrinter printer) {
  +        return set( SCHEMA_CODE_PRINTER, printer );
  +    }
   
       //
       // Complete set of XmlOption's
  @@ -643,7 +653,8 @@
       public static final String VALIDATE_ON_SET                 =  "VALIDATE_ON_SET";
       /** @exclude */
       public static final String ENTITY_RESOLVER                 =  "ENTITY_RESOLVER";
  -    
  +    /** @exclude */
  +    public static final String SCHEMA_CODE_PRINTER             = "SCHEMA_CODE_PRINTER";
   
       private static final XmlOptions EMPTY_OPTIONS;
       static {
  
  
  
  1.1                  xml-xmlbeans/v2/src/xmlpublic/org/apache/xmlbeans/SchemaCodePrinter.java
  
  Index: SchemaCodePrinter.java
  ===================================================================
  /*   Copyright 2004 The Apache Software Foundation
   *
   *   Licensed under the Apache License, Version 2.0 (the "License");
   *   you may not use this file except in compliance with the License.
   *   You may obtain a copy of the License at
   *
   *       http://www.apache.org/licenses/LICENSE-2.0
   *
   *   Unless required by applicable law or agreed to in writing, software
   *   distributed under the License is distributed on an "AS IS" BASIS,
   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *   See the License for the specific language governing permissions and
   *  limitations under the License.
   */
  
  package org.apache.xmlbeans;
  
  import java.io.Writer;
  import java.io.IOException;
  
  /**
   * This class is used to provide alternate implementations of the
   * schema Java code generation.
   */
  
  public interface SchemaCodePrinter
  {
      public void printTypeImpl(Writer writer, SchemaType sType)
          throws IOException;
      
      public void printType(Writer writer, SchemaType sType)
          throws IOException;
      
      public void printLoader(Writer writer, SchemaTypeSystem system)
          throws IOException;
  }
  
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: xmlbeans-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xmlbeans-cvs-help@xml.apache.org


Mime
View raw message