You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xmlbeans-cvs@xml.apache.org by da...@apache.org on 2004/06/03 23:28:36 UTC

cvs commit: xml-xmlbeans/v2/src/xmlcomp/org/apache/xmlbeans/impl/tool SchemaCodeGenerator.java SchemaCompiler.java XMLBean.java

daveremy    2004/06/03 14:28:36

  Modified:    v2/src/xmlcomp/org/apache/xmlbeans/impl/tool
                        SchemaCodeGenerator.java SchemaCompiler.java
                        XMLBean.java
  Log:
  Added new 'noSrcRegen' flag to the <scomp> ant task.
  This allows one to take advantage of the <javac> task's ability to only recompile source XmlBeans that are newer than their target class.
  Contributed by Radu Preotiuc.
  
  Revision  Changes    Path
  1.8       +82 -7     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.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- SchemaCodeGenerator.java	10 Apr 2004 02:50:01 -0000	1.7
  +++ SchemaCodeGenerator.java	3 Jun 2004 21:28:36 -0000	1.8
  @@ -28,6 +28,7 @@
   import java.io.FileWriter;
   import java.io.IOException;
   import java.io.StringWriter;
  +import java.io.Reader;
   import java.io.Writer;
   import java.util.ArrayList;
   import java.util.Arrays;
  @@ -44,7 +45,7 @@
       public static boolean compileTypeSystem(SchemaTypeSystem saver, File sourcedir, File[] javasrc,
            Map sourcesToCopyMap, File[] classpath, File classesdir, File outputJar, boolean nojavac,
            XmlErrorWatcher errors, String repackage, SchemaCodePrinter codePrinter, boolean verbose, List sourcefiles,
  -         File schemasDir)
  +        File schemasDir, boolean incrSrcGen)
       {
   
           if (sourcedir == null || classesdir == null)
  @@ -112,7 +113,7 @@
               failure = true;
           }
   
  -        failure &= genTypes(saver, sourcefiles, sourcedir, repackager, verbose, opts);
  +        failure &= genTypes(saver, sourcefiles, sourcedir, repackager, verbose, opts, incrSrcGen);
   
           if (failure)
               return false;
  @@ -171,7 +172,7 @@
           }
       }
   
  -    private static boolean genTypes(SchemaTypeSystem saver, List sourcefiles, File sourcedir, Repackager repackager, boolean verbose, XmlOptions opts)
  +    private static boolean genTypes(SchemaTypeSystem saver, List sourcefiles, File sourcedir, Repackager repackager, boolean verbose, XmlOptions opts, boolean incrSrcGen)
       {
           boolean failure = false;
   
  @@ -180,6 +181,15 @@
           types.addAll(Arrays.asList(saver.documentTypes()));
           types.addAll(Arrays.asList(saver.attributeTypes()));
   
  +        Set seenFiles = null;
  +        if (incrSrcGen)
  +        {
  +            seenFiles = new HashSet();
  +            if (sourcefiles != null)
  +                for (int i = 0; i < sourcefiles.size(); i++)
  +                    seenFiles.add(sourcefiles.get(i));
  +        }
  +
           for (Iterator i = types.iterator(); i.hasNext(); )
           {
               SchemaType type = (SchemaType)i.next();
  @@ -202,6 +212,8 @@
               String filename = fjn.replace('.', File.separatorChar) + ".java";
               
               Writer writer = null;
  +            Reader reader = null;
  +            boolean changed = true;
               
               try
               {
  @@ -209,6 +221,41 @@
                   sourcefile.getParentFile().mkdirs();
                   if (verbose)
                       System.err.println("created " + sourcefile.getAbsolutePath());
  +                if (incrSrcGen)
  +                    seenFiles.add(sourcefile);
  +                if (incrSrcGen && sourcefile.exists())
  +                {
  +                    // Generate the file in a buffer and then compare it to the
  +                    // file already on disk
  +                    // Generation
  +                    StringWriter sw = new StringWriter();
  +                    SchemaTypeCodePrinter.printType(sw, type, opts);
  +                    StringBuffer buffer = sw.getBuffer();
  +                    if (repackager != null)
  +                        buffer = repackager.repackage(buffer);
  +                    // Comparison
  +                    List diffs = new ArrayList();
  +                    reader = new java.io.FileReader(sourcefile);
  +                    String str = buffer.toString();
  +                    Diff.readersAsText(new java.io.StringReader(str), "<generated>",
  +                            reader, sourcefile.getName(), diffs);
  +                    reader.close();
  +                    // Check the list of differences
  +                    changed = (diffs.size() > 0);
  +                    if (changed)
  +                    {
  +                        // Diffs encountered, replace the file with the text from
  +                        // the buffer
  +                        writer = new FileWriter( sourcefile );
  +                        writer.write(str);
  +                        writer.close();
  +                        sourcefiles.add(sourcefile);
  +                    }
  +                    else
  +                        ; // No diffs, don't do anything
  +                }
  +                else
  +                {
                   writer =
                       repackager == null
                           ? (Writer) new FileWriter( sourcefile )
  @@ -218,8 +265,9 @@
                   SchemaTypeCodePrinter.printType(writer, type, opts);
                   
                   writer.close();
  -                
  +
                   sourcefiles.add(sourcefile);
  +                }
               }
               catch (IOException e)
               {
  @@ -227,7 +275,10 @@
                   failure = true;
               }
               finally {
  -                try { if (writer != null) writer.close(); } catch (IOException e) {}
  +                try {
  +                    if (writer != null) writer.close();
  +                    if (reader != null) reader.close();
  +                } catch (IOException e) {}
               }
   
               try
  @@ -239,7 +290,11 @@
                       System.err.println("created " + implFile.getAbsolutePath());
                   implFile.getParentFile().mkdirs();
   
  -                
  +                if (incrSrcGen)
  +                    seenFiles.add(implFile);
  +                // If the interface did not change, the implementation shouldn't either
  +                if (changed)
  +                {
                   writer =
                       repackager == null
                           ? (Writer) new FileWriter( implFile )
  @@ -248,8 +303,9 @@
                   SchemaTypeCodePrinter.printTypeImpl(writer, type, opts);
                   
                   writer.close();
  -                
  +
                   sourcefiles.add(implFile);
  +                }
               }
               catch (IOException e)
               {
  @@ -261,7 +317,26 @@
               }
           }
   
  +        if (incrSrcGen)
  +            deleteObsoleteFiles(sourcedir, seenFiles);
  +
           return failure;
  +    }
  +
  +    private static void deleteObsoleteFiles(File srcDir, Set seenFiles)
  +    {
  +        // Go recursively starting with srcDir and delete all files that are
  +        // not in the given Set
  +        File[] files = srcDir.listFiles();
  +        for (int i = 0; i < files.length; i++)
  +        {
  +            if (files[i].isDirectory())
  +                deleteObsoleteFiles(files[i], seenFiles);
  +            else if (seenFiles.contains(files[i]))
  +                ;
  +            else
  +                files[i].delete();
  +        }
       }
   
       protected static File createTempDir() throws IOException
  
  
  
  1.10      +13 -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.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- SchemaCompiler.java	12 May 2004 18:44:53 -0000	1.9
  +++ SchemaCompiler.java	3 Jun 2004 21:28:36 -0000	1.10
  @@ -327,6 +327,7 @@
           private boolean noPvr;
           private boolean noAnn;
           private boolean debug;
  +        private boolean incrementalSrcGen;
           private String repackage;
           private List extensions = Collections.EMPTY_LIST;
           private Set mdefNamespaces = Collections.EMPTY_SET;
  @@ -503,6 +504,16 @@
               this.noAnn = noAnn;
           }
   
  +        public boolean isIncrementalSrcGen()
  +        {
  +            return incrementalSrcGen;
  +        }
  +
  +        public void setIncrementalSrcGen(boolean incrSrcGen)
  +        {
  +            this.incrementalSrcGen = incrSrcGen;
  +        }
  +
           public boolean isDebug()
           {
               return debug;
  @@ -814,6 +825,7 @@
           boolean noUpa = params.isNoUpa();
           boolean noPvr = params.isNoPvr();
           boolean noAnn = params.isNoAnn();
  +        boolean incrSrcGen = params.isIncrementalSrcGen();
           Collection outerErrorListener = params.getErrorListener();
           String repackage = params.getRepackage();
           SchemaCodePrinter codePrinter = params.getSchemaCodePrinter();
  @@ -862,7 +874,7 @@
               List sourcefiles = new ArrayList();
               result &= SchemaCodeGenerator.compileTypeSystem(system, srcDir, javaFiles, sourcesToCopyMap,
                   classpath, classesDir, outputJar, nojavac, errorListener, repackage, codePrinter, verbose,
  -                sourcefiles, schemasDir);
  +                sourcefiles, schemasDir, incrSrcGen);
               result &= !errorListener.hasError();
   
               if (result)
  
  
  
  1.6       +12 -0     xml-xmlbeans/v2/src/xmlcomp/org/apache/xmlbeans/impl/tool/XMLBean.java
  
  Index: XMLBean.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/xmlcomp/org/apache/xmlbeans/impl/tool/XMLBean.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- XMLBean.java	14 May 2004 20:36:41 -0000	1.5
  +++ XMLBean.java	3 Jun 2004 21:28:36 -0000	1.6
  @@ -68,6 +68,7 @@
                           failonerror = true,
                           fork = true,
                           includeAntRuntime = true,
  +                        noSrcRegen,
                           includeJavaRuntime = false;
   
       private String      typesystemname,
  @@ -223,6 +224,7 @@
               params.setExtensions(extensions);
               params.setErrorListener(err);
               params.setCatalogFile(catalog);
  +            params.setIncrementalSrcGen(noSrcRegen);
               params.setMdefNamespaces(mdefnamespaces);
               success = SchemaCompiler.compile(params);
   
  @@ -645,6 +647,16 @@
       public void setIncludeJavaRuntime(boolean includeJavaRuntime)
       {
           this.includeJavaRuntime = includeJavaRuntime;
  +    }
  +
  +    public boolean isIncrementalSrcGen()
  +    {
  +        return noSrcRegen;
  +    }
  +
  +    public void setNoSrcRegen(boolean noSrcRegen)
  +    {
  +        this.noSrcRegen = noSrcRegen;
       }
   
       /**
  
  
  

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