You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by bo...@apache.org on 2001/06/22 09:24:20 UTC

cvs commit: jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional TraXLiaison.java XslpLiaison.java

bodewig     01/06/22 00:24:20

  Modified:    src/main/org/apache/tools/ant/taskdefs XSLTProcess.java
               src/main/org/apache/tools/ant/taskdefs/optional
                        TraXLiaison.java XslpLiaison.java
  Log:
  Add force attribute to <style> task to allow it to run independent of
  file modification times.
  
  Submitted by:	Craeg K Strong <cs...@arielpartners.com>
  
  Allow classpath from which the processor will be loaded to be
  specified.
  
  PR:             2144
  
  Make sure Liaison classes close their output streams.
  
  PR:             1848
  
  Revision  Changes    Path
  1.22      +92 -21    jakarta-ant/src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java
  
  Index: XSLTProcess.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java,v
  retrieving revision 1.21
  retrieving revision 1.22
  diff -u -r1.21 -r1.22
  --- XSLTProcess.java	2001/05/08 09:35:30	1.21
  +++ XSLTProcess.java	2001/06/22 07:24:17	1.22
  @@ -61,6 +61,8 @@
   import java.util.StringTokenizer;
   import java.util.Vector;
   import org.apache.tools.ant.*;
  +import org.apache.tools.ant.types.Path;
  +import org.apache.tools.ant.types.Reference;
   
   
   /**
  @@ -85,7 +87,7 @@
    * @author <a href="mailto:rubys@us.ibm.com">Sam Ruby</a>
    * @author <a href="mailto:russgold@acm.org">Russell Gold</a>
    * @author <a href="stefan.bodewig@epost.de">Stefan Bodewig</a>
  - * @version $Revision: 1.21 $ $Date: 2001/05/08 09:35:30 $
  + * @version $Revision: 1.22 $ $Date: 2001/06/22 07:24:17 $
    */
   public class XSLTProcess extends MatchingTask {
   
  @@ -102,9 +104,13 @@
    
       private File outFile = null;
   
  +    private String processor;
  +    private Path classpath = null;
       private XSLTLiaison liaison;
       private boolean stylesheetLoaded = false;
   
  +    private boolean force = false;
  +
       /**
        * Creates a new XSLTProcess Task.
        **/
  @@ -179,6 +185,13 @@
       } //-- execute
   
       /**
  +     * Set whether to check dependencies, or always generate.
  +     **/
  +    public void setForce(boolean force) {
  +        this.force = force;
  +    } //-- setForce
  +
  +    /**
        * Set the base directory.
        **/
       public void setBasedir(File dir) {
  @@ -209,25 +222,73 @@
       public void setStyle(String xslFile) {
           this.xslFile = xslFile;
       }
  +
  +    /**
  +     * Set the classpath to load the Processor through (attribute).
  +     */
  +    public void setClasspath(Path classpath) {
  +        createClasspath().append(classpath);
  +    }
   
  -    public void setProcessor(String processor) throws Exception {
  +    /**
  +     * Set the classpath to load the Processor through (nested element).
  +     */
  +    public Path createClasspath() {
  +        if (classpath == null) {
  +            classpath = new Path(project);
  +        }
  +        return classpath.createPath();
  +    }
   
  -        if (processor.equals("trax")) {
  +    /**
  +     * Set the classpath to load the Processor through via reference
  +     * (attribute).
  +     */
  +    public void setClasspathRef(Reference r) {
  +        createClasspath().setRefid(r);
  +    }
  +
  +
  +    public void setProcessor(String processor) {
  +        this.processor = processor;
  +    }
  +
  +    /**
  +     * Load processor here instead of in setProcessor - this will be
  +     * called from within execute, so we have access to the latest
  +     * classpath.
  +     */
  +    private void resolveProcessor(String proc) throws Exception {
  +        if (proc.equals("trax")) {
               final Class clazz = 
  -                Class.forName("org.apache.tools.ant.taskdefs.optional.TraXLiaison");
  +                loadClass("org.apache.tools.ant.taskdefs.optional.TraXLiaison");
               liaison = (XSLTLiaison)clazz.newInstance();
  -        } else if (processor.equals("xslp")) {
  +        } else if (proc.equals("xslp")) {
               final Class clazz = 
  -                Class.forName("org.apache.tools.ant.taskdefs.optional.XslpLiaison");
  +                loadClass("org.apache.tools.ant.taskdefs.optional.XslpLiaison");
               liaison = (XSLTLiaison) clazz.newInstance();
  -        } else if (processor.equals("xalan")) {
  +        } else if (proc.equals("xalan")) {
               final Class clazz = 
  -                Class.forName("org.apache.tools.ant.taskdefs.optional.XalanLiaison");
  +                loadClass("org.apache.tools.ant.taskdefs.optional.XalanLiaison");
               liaison = (XSLTLiaison)clazz.newInstance();
           } else {
  -            liaison = (XSLTLiaison) Class.forName(processor).newInstance();
  +            liaison = (XSLTLiaison) loadClass(proc).newInstance();
           }
  +    }
   
  +    /**
  +     * Load named class either via the system classloader or a given
  +     * custom classloader.
  +     */
  +    private Class loadClass(String classname) throws Exception {
  +        if (classpath == null) {
  +            return Class.forName(classname);
  +        } else {
  +            AntClassLoader al = new AntClassLoader(project, classpath);
  +            Class c = al.loadClass(classname);
  +            AntClassLoader.initializeClass(c);
  +            return c;
  +        }
       }
   
       /**
  @@ -265,7 +326,8 @@
               }else{
                   outFile = new File(destDir,xmlFile+fileExt);
               }
  -            if (inFile.lastModified() > outFile.lastModified() ||
  +            if (force ||
  +                inFile.lastModified() > outFile.lastModified() ||
                   styleSheetLastModified > outFile.lastModified()) {
                   ensureDirectoryFor( outFile );
                   log("Transforming into "+destDir);
  @@ -293,7 +355,8 @@
               log("In file "+inFile+" time: " + inFile.lastModified() , Project.MSG_DEBUG);
               log("Out file "+outFile+" time: " + outFile.lastModified() , Project.MSG_DEBUG);
               log("Style file "+xslFile+" time: " + styleSheetLastModified , Project.MSG_DEBUG);
  -            if (inFile.lastModified() > outFile.lastModified() ||
  +            if (force ||
  +                inFile.lastModified() > outFile.lastModified() ||
                   styleSheetLastModified > outFile.lastModified()) {
                   ensureDirectoryFor( outFile );
                   log("Processing " + inFile + " to " + outFile, Project.MSG_INFO);
  @@ -321,18 +384,26 @@
           // if processor wasn't specified, see if TraX is available.  If not,
           // default it to xslp or xalan, depending on which is in the classpath
           if (liaison == null) {
  -            try {
  -                setProcessor("trax");
  -            } catch (Throwable e1) {
  +            if (processor != null) {
  +                try {
  +                    resolveProcessor(processor);
  +                } catch (Exception e) {
  +                    throw new BuildException(e);
  +                }
  +            } else {
                   try {
  -                    setProcessor("xslp");
  -                } catch (Throwable e2) {
  +                    resolveProcessor("trax");
  +                } catch (Throwable e1) {
                       try {
  -                        setProcessor("xalan");
  -                    } catch (Throwable e3) {
  -                        e2.printStackTrace();
  -                        e3.printStackTrace();
  -                        throw new BuildException(e1);
  +                        resolveProcessor("xslp");
  +                    } catch (Throwable e2) {
  +                        try {
  +                            resolveProcessor("xalan");
  +                        } catch (Throwable e3) {
  +                            e3.printStackTrace();
  +                            e2.printStackTrace();
  +                            throw new BuildException(e1);
  +                        }
                       }
                   }
               }
  
  
  
  1.2       +8 -2      jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/TraXLiaison.java
  
  Index: TraXLiaison.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/TraXLiaison.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TraXLiaison.java	2001/01/03 16:48:39	1.1
  +++ TraXLiaison.java	2001/06/22 07:24:18	1.2
  @@ -68,7 +68,7 @@
    *
    * @author <a href="mailto:rubys@us.ibm.com">Sam Ruby</a>
    * @author <a href="mailto:dims@yahoo.com">Davanum Srinivas</a>
  - * @version $Revision: 1.1 $ $Date: 2001/01/03 16:48:39 $
  + * @version $Revision: 1.2 $ $Date: 2001/06/22 07:24:18 $
    */
   public class TraXLiaison implements XSLTLiaison {
   
  @@ -93,7 +93,13 @@
       };
   
       public void transform(String infile, String outfile) throws Exception {
  -        transformer.transform(new StreamSource(normalize(infile)), new StreamResult(new FileOutputStream(outfile)));
  +        FileOutputStream out = new FileOutputStream(outfile);
  +        try {
  +            transformer.transform(new StreamSource(normalize(infile)), 
  +                                  new StreamResult(out));
  +        } finally {
  +            out.close();
  +        }
       }
   
       protected String normalize(String fileName) {
  
  
  
  1.5       +7 -2      jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/XslpLiaison.java
  
  Index: XslpLiaison.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/XslpLiaison.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- XslpLiaison.java	2001/01/03 14:18:36	1.4
  +++ XslpLiaison.java	2001/06/22 07:24:18	1.5
  @@ -65,7 +65,7 @@
   /**
    *
    * @author <a href="mailto:rubys@us.ibm.com">Sam Ruby</a>
  - * @version $Revision: 1.4 $ $Date: 2001/01/03 14:18:36 $
  + * @version $Revision: 1.5 $ $Date: 2001/06/22 07:24:18 $
    */
   public class XslpLiaison implements XSLTLiaison {
   
  @@ -82,7 +82,12 @@
       };
   
       public void transform(String infile, String outfile) throws Exception {
  -      processor.process(infile, xslSheet, new FileWriter(outfile));
  +        FileWriter out = new FileWriter(outfile);
  +        try {
  +            processor.process(infile, xslSheet, out);
  +        } finally {
  +            out.close();
  +        }
       }
   
       public void addParam(String name, String expression){