geirm       02/02/10 01:20:57

  Modified:    src/java/org/apache/tools/dvsl DVSL.java DVSLTask.java
  Log:
  These are Bill Burton's <[EMAIL PROTECTED]> patches applied (with some
  formatting help :)
  
  DVSL.java : added the notion of the externally settable classloader for
  use in instantiating toolbox classes
  
  DVSLTask.java : fundamental rewrite (or 'copious borrowing') - took the
  XSLT style task from Ant and adapted it for DVSL.  Brings in all sorts
  of expected features (change detection, auto dir making) and also add
  the classpath and alternate <tool> property support.
  
  Great stuff.
  
  Revision  Changes    Path
  1.6       +34 -4     jakarta-velocity-dvsl/src/java/org/apache/tools/dvsl/DVSL.java
  
  Index: DVSL.java
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity-dvsl/src/java/org/apache/tools/dvsl/DVSL.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- DVSL.java 6 Feb 2002 04:47:12 -0000       1.5
  +++ DVSL.java 10 Feb 2002 09:20:56 -0000      1.6
  @@ -1,7 +1,7 @@
   /*
    * The Apache Software License, Version 1.1
    *
  - * Copyright (c) 2000-2001 The Apache Software Foundation.  All rights
  + * Copyright (c) 2001-2002 The Apache Software Foundation.  All rights
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -102,6 +102,7 @@
       private Context styleContext;
       private DVSLContext baseContext = new DVSLContext();
       private Transformer transformer;
  +    private ClassLoader classLoader;
       private boolean ready = false;
   
       private Map velConfig = null;
  @@ -110,6 +111,7 @@
   
       public DVSL()
       {
  +        classLoader = DVSL.class.getClassLoader();
       }
   
       /**
  @@ -142,6 +144,26 @@
   
       /**
        *  <p>
  +     *  Specify a classloader for loading the Toolbox classes.  Setting to null
  +     *  resets to the default ClassLoader.
  +     *  </p>
  +     *
  +     *  @param classLoader ClassLoader or null for default ClassLoader
  +     */
  +    public void setClassLoader( ClassLoader classLoader )
  +    {
  +        if ( classLoader == null )
  +        {
  +            this.classLoader = this.getClass().getClassLoader();
  +        }
  +        else
  +        {
  +            this.classLoader = classLoader;
  +        }
  +    }
  +
  +    /**
  +     *  <p>
        *  Loads the toolbox from the input Properties.
        *  </p>
        *
  @@ -193,7 +215,7 @@
               {
                   String toolname = key.substring( TOOL_PROP_PREFIX.length());
   
  -                Object o = Class.forName( value ).newInstance();
  +                Object o = Class.forName( value, true, classLoader ).newInstance();
                   
                   toolbox.put( toolname, o );
               }
  @@ -235,6 +257,15 @@
       public void setStylesheet( String stylesheet )
           throws Exception
       {
  +        setStylesheet(new File(stylesheet));
  +    }
  +
  +    /**
  +     *  Convenience function.  See...
  +     */
  +    public void setStylesheet( File stylesheet )
  +        throws Exception
  +    {
           FileReader fr = null;
           
           try
  @@ -265,7 +296,7 @@
        *  to transform.  Just do it once, and transform away...
        *  </p>
        *
  -     *  @param stylesheet Reader with stylesheet char stream
  +     *  @param styleReader Reader with stylesheet char stream
        */
       public void setStylesheet( Reader styleReader )
           throws Exception
  @@ -448,7 +479,6 @@
   
           return transformer.transform( dom4jdoc, writer );
       }
  -
   
       public long transform( File f, Writer writer )
           throws Exception
  
  
  
  1.4       +679 -275  
jakarta-velocity-dvsl/src/java/org/apache/tools/dvsl/DVSLTask.java
  
  Index: DVSLTask.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-velocity-dvsl/src/java/org/apache/tools/dvsl/DVSLTask.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- DVSLTask.java     7 Feb 2002 05:40:30 -0000       1.3
  +++ DVSLTask.java     10 Feb 2002 09:20:56 -0000      1.4
  @@ -1,301 +1,705 @@
  -/*
  - * The Apache Software License, Version 1.1
  - *
  - * Copyright (c) 2000-2001 The Apache Software Foundation.  All rights
  - * reserved.
  - *
  - * Redistribution and use in source and binary forms, with or without
  - * modification, are permitted provided that the following conditions
  - * are met:
  - *
  - * 1. Redistributions of source code must retain the above copyright
  - *    notice, this list of conditions and the following disclaimer.
  - *
  - * 2. Redistributions in binary form must reproduce the above copyright
  - *    notice, this list of conditions and the following disclaimer in
  - *    the documentation and/or other materials provided with the
  - *    distribution.
  - *
  - * 3. The end-user documentation included with the redistribution, if
  - *    any, must include the following acknowlegement:
  - *       "This product includes software developed by the
  - *        Apache Software Foundation (http://www.apache.org/)."
  - *    Alternately, this acknowlegement may appear in the software itself,
  - *    if and wherever such third-party acknowlegements normally appear.
  - *
  - * 4. The names "The Jakarta Project", "Velocity","DVSL" and "Apache Software
  - *    Foundation" must not be used to endorse or promote products derived
  - *    from this software without prior written permission. For written
  - *    permission, please contact [EMAIL PROTECTED]
  - *
  - * 5. Products derived from this software may not be called "Apache"
  - *    nor may "Apache" appear in their names without prior written
  - *    permission of the Apache Group.
  - *
  - * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  - * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  - * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  - * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  - * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  - * SUCH DAMAGE.
  - * ====================================================================
  - *
  - * This software consists of voluntary contributions made by many
  - * individuals on behalf of the Apache Software Foundation.  For more
  - * information on the Apache Software Foundation, please see
  - * <http://www.apache.org/>.
  - */
  -package org.apache.tools.dvsl;
  +/*
  + * The Apache Software License, Version 1.1
  + *
  + * Copyright (c) 2001-2002 The Apache Software Foundation.  All rights
  + * reserved.
  + *
  + * Redistribution and use in source and binary forms, with or without
  + * modification, are permitted provided that the following conditions
  + * are met:
  + *
  + * 1. Redistributions of source code must retain the above copyright
  + *    notice, this list of conditions and the following disclaimer.
  + *
  + * 2. Redistributions in binary form must reproduce the above copyright
  + *    notice, this list of conditions and the following disclaimer in
  + *    the documentation and/or other materials provided with the
  + *    distribution.
  + *
  + * 3. The end-user documentation included with the redistribution, if
  + *    any, must include the following acknowlegement:
  + *       "This product includes software developed by the
  + *        Apache Software Foundation (http://www.apache.org/)."
  + *    Alternately, this acknowlegement may appear in the software itself,
  + *    if and wherever such third-party acknowlegements normally appear.
  + *
  + * 4. The names "The Jakarta Project", "Velocity", "DVSL" and "Apache Software
  + *    Foundation" must not be used to endorse or promote products derived
  + *    from this software without prior written permission. For written
  + *    permission, please contact [EMAIL PROTECTED]
  + *
  + * 5. Products derived from this software may not be called "Apache"
  + *    nor may "Apache" appear in their names without prior written
  + *    permission of the Apache Group.
  + *
  + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  + * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  + * SUCH DAMAGE.
  + * ====================================================================
  + *
  + * This software consists of voluntary contributions made by many
  + * individuals on behalf of the Apache Software Foundation.  For more
  + * information on the Apache Software Foundation, please see
  + * <http://www.apache.org/>.
  + */
  +
  +package org.apache.tools.dvsl;
   
  -import java.util.Properties;
   import java.util.Map;
  +import java.util.Properties;
  +import java.util.Vector;
  +import java.util.HashMap;
  +import java.util.Enumeration;
   
  -import java.io.Writer;
  -import java.io.FileWriter;
   import java.io.File;
  -import java.io.BufferedWriter;
   import java.io.FileInputStream;
  +import java.io.BufferedWriter;
  +import java.io.OutputStreamWriter;
  +import java.io.FileOutputStream;
   
  -import org.apache.tools.ant.BuildException;
  -import org.apache.tools.ant.DirectoryScanner;
  -import org.apache.tools.ant.Project;
  -import org.apache.tools.ant.taskdefs.MatchingTask;
  -
  -import org.apache.velocity.VelocityContext;
  -
  -
  -/**
  - *  Simple Ant task to mimic the behavior of the
  - *  included Ant Style XSL task.
  - *
  - *  Still needs :
  - *      <style> only-on-change (also like Anakia)
  - *      auto-output directory creation 
  - *
  - *  @author <a href="mailto:[EMAIL PROTECTED]";>Geir Magnusson Jr.</a>
  - */
  +import org.apache.tools.ant.AntClassLoader;
  +import org.apache.tools.ant.BuildException;
  +import org.apache.tools.ant.DirectoryScanner;
  +import org.apache.tools.ant.Project;
  +import org.apache.tools.ant.taskdefs.MatchingTask;
  +import org.apache.tools.ant.types.Path;
  +import org.apache.tools.ant.types.Reference;
  +import org.apache.tools.ant.util.FileUtils;
  +import org.apache.velocity.VelocityContext;
  +
  +
  +/**
  + * A Task to process via DVSL a set of XML documents. This is
  + * useful for building views of XML based documentation.
  + * arguments:  (XXX: NEEDS UPDATING)
  + * <ul>
  + * <li>basedir
  + * <li>destdir
  + * <li>style
  + * <li>in
  + * <li>out
  + * <li>includes
  + * <li>excludes
  + * <li>force
  + * <li>extension
  + * <li>outputencoding
  + * <li>classpath
  + * <li>classpathref
  + * <li>toolboxfile
  + * <li>velocityconfigclass
  + * <li>
  + * </ul>
  + * <p>Of these arguments, the <b>sourcedir</b> and <b>destdir</b> are required.</p>
  + * <p>Following are the supported nested elements:</p>
  + * <ul>
  + * <li>&lt;include&gt;
  + * <li>&lt;exclude&gt;
  + * <li>&lt;classpath&gt;
  + * <li>&lt;tool name="toolbox-property" value="value-or-object" /&gt;
  + * <li>&lt;velconfig name="velocity-config-name" value="config-value" /&gt;
  + * </ul>
  + * <p>This task will recursively scan the sourcedir and destdir
  + * looking for XML documents to process via DVSL. Any other files,
  + * such as images, or html files in the source directory will be
  + * copied into the destination directory.  XXX: is that true???</p>
  + *
  + * <p>This task was adapted from Ant's <style> task (XSLTProcess class).</p>
  + *
  + * @author <a href="mailto:[EMAIL PROTECTED]";>Keith Visco</a>
  + * @author <a href="mailto:[EMAIL PROTECTED]";>Sam Ruby</a>
  + * @author <a href="mailto:[EMAIL PROTECTED]";>Russell Gold</a>
  + * @author <a href="mailto:[EMAIL PROTECTED]";>Stefan Bodewig</a>
  + * @author <a href="mailto:[EMAIL PROTECTED]";>Geir Magnusson Jr.</a>
  + * @author <a href="mailto:[EMAIL PROTECTED]";>Bill Burton.</a>
  + */
  +
   public class DVSLTask extends MatchingTask
  -{
  -    protected File basedir;
  -    protected File destdir;
  -    protected String stylesheet;
  -    protected String toolboxProps;
  -    protected String extension = ".html";
  -    protected String velPropClass = null;
  -
  -    /*
  -     * I wish they called this sourcedir in Style.  grrr...
  -     */
  -    public void setBasedir( File bd )
  -    {
  -        basedir = bd;
  -    }
  -
  -    public void setDestdir( File dd )
  -    {
  -        destdir = dd;
  -    }
  -
  -    public void setStyle( String style )
  -    {
  -        stylesheet = style;
  -    }
  -
  -    public void setExtension( String ext )
  -    {
  -        extension = ext;
  -    }
  -
  -    public void setToolbox( String toolbox )
  -    {
  -        toolboxProps = toolbox;
  -    }
  -
  -    /**
  -     *  <p>
  -     *  Allows the user to specify a class that implements
  -     *  java.util.Properties that will have user properties
  -     *  to be used when setting up DVSL
  -     *  </p>
  -     */
  -    public void setVelocityConfigClass( String classname )
  -    {
  -        velPropClass = classname;
  -    }
  -
  -    public void execute () throws BuildException
  -    {
  -        /*
  -         *  use the current project directory if no source dir set
  -         */
  -        if (basedir == null)
  -        {
  -            basedir = project.resolveFile(".");
  -        }
  -
  -        /*
  -         *  use the current directory as well.  Will check to prevent that we don't
  -         *  overwrite the source
  -         */
  -        if (destdir == null )
  -        {
  -            destdir = project.resolveFile(".");
  -        }
  -
  -        /*
  -         *  must have a stylesheet
  -         */
  -        if (stylesheet == null) 
  -        {
  -            throw new BuildException("You must specify a stylesheet (.vsl file) 
with the style parameter");
  -        }
  -
  -        /*
  -         *  now just do it
  -         */
  -        process();
  +{
  +    private DVSL dvsl;
  +
  +    private File destDir = null;
  +    private File baseDir = null;
  +    private File stylesheet = null;
  +    private File inFile = null;
  +    private File outFile = null;
  +
  +    private String targetExtension = ".html";
  +    private String outputEncoding = "UTF-8";
  +
  +    private Path classpath = null;
  +    private ClassLoader classLoader = null;
  +
  +    private boolean force = false;
  +
  +    private Vector toolAttr = new Vector();
  +    private File toolboxFile = null;
  +    private Properties toolboxProps = null;
  +
  +    private String velConfigClass = null;
  +    private Map velConfigMap = null;
  +    private Vector velConfigAttr = new Vector();
  +
  +    //private String outputtype = null;  // later when output type is supported
  +
  +    /**
  +     * Creates a new DVSLTask2 Task.
  +     **/
  +    public DVSLTask()
  +    {
  +        classLoader = DVSLTask.class.getClassLoader();
       }
  +
  +    /**
  +     * Executes the task.
  +     */
  +    public void execute()
  +            throws BuildException
  +    {
  +        DirectoryScanner scanner;
  +        String[]         list;
  +        String[]         dirs;
  +
  +        if (stylesheet == null)
  +        {
  +            throw new BuildException("no stylesheet specified", location);
  +        }
  +
  +        if (baseDir == null)
  +        {
  +            baseDir = project.resolveFile(".");
  +        }
  +
  +        /*
  +         * make a DVSL
  +         */
  +        dvsl = new DVSL();
  +
  +        /*
  +         *  Create a new Classloader for loading the Toolbox and the Velocity
  +         *  properties class.
  +         */
  +        if ( classpath != null )
  +        {
  +            classLoader = new AntClassLoader( project, classpath );
  +            dvsl.setClassLoader( classLoader );
  +        }
  +
  +        /*
  +         * If the user gave us a velPropClass, we create an instance
  +         * of that to use to config the VelocityEngine inside te DVSL
  +         */
  +        Object velConfigObj = null;
   
  -
  -    /**
  -     *  does the real work.  Gets the list of files to work on,
  -     *  and transforms each one
  -     */
  -    protected void process()
  -        throws BuildException
  -    {
  -        /*
  -         *  make a DVSL
  -         */
  -     
  -        DVSL dvsl = new DVSL();
  -
  -        /*
  -         *  now, if the user gave us a velPropClass, we create an instance
  -         *  of that to use to config the VelocityEngine inside te DVSL
  -         */
  -
  -        if( velPropClass != null )
  -        {
  -            Object ob = null;
  -
  +        if ( velConfigClass != null )
  +        {
               try
  -            {
  -                 ob = Class.forName( velPropClass).newInstance();
  -            }
  -            catch( Exception ex )
  -            {
  -                throw new BuildException("Error instantiating VelocityPropClass : " 
+ ex );
  -            }
  +            {
  +                velConfigObj = Class.forName( velConfigClass, true, classLoader 
).newInstance();
   
  -            if ( ob instanceof Map )
  +                if ( velConfigObj instanceof Map  )
  +                {
  +                    velConfigMap = (Map) velConfigObj;
  +                }
  +                else
  +                {
  +                    throw new BuildException("VelocityPropClass is not instanceof 
java.util.Map" );
  +                }
  +            }
  +            catch( Exception ex )
  +            {
  +                throw new BuildException("Error instantiating VelocityPropClass : "
  +                        + ex );
  +            }
  +        }
  +
  +        /*
  +         * If any nested Velocity Config elements have been specified, overlay any 
settings
  +         * in the Velocity Config object.
  +         */
  +        if ( !velConfigAttr.isEmpty() )
  +        {
  +            if ( velConfigMap == null )
               {
  -                dvsl.setVelocityConfig( (Map) ob );
  +               velConfigMap = new HashMap();
               }
  -        }
  -
  -        /*
  -         * now the stylesheet
  -         */
  +
  +            /*
  +             * Now copy velocity config attributes into the Map
  +             */
  +            for ( Enumeration e = velConfigAttr.elements(); e.hasMoreElements(); )
  +            {
  +                VelocityConfig p = (VelocityConfig)e.nextElement();
  +                velConfigMap.put( p.getName(), p.getValue() );
  +            }
  +        }
  +
  +        /*
  +         * Finally, set the Velocity Config object in DVSL if it's valid.
  +         */
  +        if ( velConfigMap != null)
  +        {
  +            dvsl.setVelocityConfig( velConfigMap );
  +        }
  +
  +        /*
  +         * now the stylesheet
  +         */
           try
  -        {
  -            dvsl.setStylesheet( stylesheet );
  -        }
  -        catch( Exception e )
  -        {
  -            throw new BuildException("Problem setting stylesheet : " + e );
  +        {
  +            log( "Loading stylesheet " + stylesheet, Project.MSG_INFO);
  +            dvsl.setStylesheet( stylesheet );
           }
  +        catch (Exception ex)
  +        {
  +            log("Failed to read stylesheet " + stylesheet, Project.MSG_INFO);
  +            throw new BuildException(ex);
  +        }
  +
  +        /*
  +         *  now, if we were given a toolbox, set that up too
  +         */
  +        toolboxProps = new Properties();
   
  -        /*
  -         *  now, if we were given a toolbox, set that up too
  -         */
  -
  -        if ( toolboxProps != null)
  -        {   
  -            try
  -            {
  -                Properties p = new Properties();
  -                
  -                p.load( new FileInputStream( project.resolveFile( toolboxProps ) ) 
);
  -                
  -                dvsl.setToolbox( p );
  +        try
  +        {
  +            if ( toolboxFile != null)
  +            {
  +                toolboxProps.load( new FileInputStream( toolboxFile ) );
               }
  -            catch( Exception ee )
  -            {
  -                throw new BuildException( "Error loading the toolbox : " + ee );
  +
  +            /*
  +             *  Overlay any parameters
  +             */
  +            for ( Enumeration e = toolAttr.elements(); e.hasMoreElements(); )
  +            {
  +                Tool p = (Tool)e.nextElement();
  +                toolboxProps.setProperty( p.getName(), p.getValue() );
               }
  -        }
  -
  +
  +            dvsl.setToolbox( toolboxProps );
  +        }
  +        catch( Exception ee )
  +        {
  +            throw new BuildException( "Error loading the toolbox : " + ee );
  +        }
  +
           /*
  -         *  get a list of the input files and process each one
  +         * if we have an in file and out then process them
            */
  -        
  -        String[] infiles = getDirectoryScanner( basedir ).getIncludedFiles();
  -
  -        for (int i = 0;i < infiles.length; ++i)
  -        {
  +
  +        if (inFile != null && outFile != null)
  +        {
  +            process(inFile, outFile, stylesheet);
  +            return;
  +        }
  +
  +        /*
  +         * if we get here, in and out have not been specified, we are
  +         * in batch processing mode.
  +         */
  +
  +        /*
  +         *   make sure Source directory exists...
  +         */
  +        if (destDir == null )
  +        {
  +            String msg = "destdir attributes must be set!";
  +            throw new BuildException(msg);
  +        }
  +
  +        scanner = getDirectoryScanner(baseDir);
  +        log("Transforming into "+destDir, Project.MSG_INFO);
  +
  +        /*
  +         *  Process all the files marked for styling
  +         */
  +        list = scanner.getIncludedFiles();
  +
  +        for (int i = 0;i < list.length; ++i)
  +        {
  +            process( baseDir, list[i], destDir, stylesheet );
  +        }
  +
  +        /*
  +         *  Process all the directoried marked for styling
  +         */
  +        dirs = scanner.getIncludedDirectories();
  +
  +        for (int j = 0;j < dirs.length;++j)
  +        {
  +            list=new File(baseDir,dirs[j]).list();
  +
  +            for (int i = 0;i < list.length;++i)
  +            {
  +                process( baseDir, list[i], destDir, stylesheet );
  +            }
  +        }
  +    } //-- 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)
  +    {
  +        baseDir = dir;
  +    } //-- setSourceDir
  +
  +    /**
  +     * Set the destination directory into which the XSL result
  +     * files should be copied to
  +     * @param dir name of the destination directory
  +     **/
  +    public void setDestdir(File dir)
  +    {
  +        destDir = dir;
  +    } //-- setDestDir
  +
  +    /**
  +     * Set the desired file extension to be used for the target
  +     * @param name the extension to use
  +     **/
  +    public void setExtension(String name)
  +    {
  +        targetExtension = name;
  +    } //-- setDestDir
  +
  +    /**
  +     * Sets the file to use for styling relative to the base directory
  +     * of this task.
  +     */
  +    //public void setStyle(String dvslFile) {  // XXX:
  +    public void setStyle(File dvslFile)
  +    {
  +        this.stylesheet = dvslFile;
  +    }
  +
  +    /**
  +     * Sets the Toolbox properties file to use.
  +     * @param toolboxFile properties file of tools
  +     * @deprecated use setToolboxFile instead
  +     */
  +    public void setToolbox(String toolboxFile)
  +    {
  +        log("DEPRECATED - use the toolboxfile attribute instead");
  +        this.toolboxFile = new File(toolboxFile);
  +    }
  +
  +    /**
  +     * Sets the Toolbox properties file to use.
  +     * @param toolboxFile properties file of tools
  +     */
  +    public void setToolboxFile(File toolboxFile)
  +    {
  +        this.toolboxFile = toolboxFile;
  +    }
  +
  +    /**
  +     * Allows the user to specify a class that implements
  +     * {@link java.util.Properties} that will have user properties
  +     * to be used when setting up DVSL.
  +     * @param classname Velocity configuration class to load
  +     */
  +    public void setVelocityConfigClass(String classname)
  +    {
  +        velConfigClass = classname;
  +    }
  +
  +    /**
  +     * Sets an output file
  +     * @param outFile output file
  +     */
  +    public void setOut(File outFile)
  +    {
  +        this.outFile = outFile;
  +    }
  +
  +    /**
  +     * Sets an input xml file to be styled
  +     * @param inFile input file
  +     */
  +    public void setIn(File inFile)
  +    {
  +        this.inFile = inFile;
  +    }
  +
  +    /**
  +     * Sets the character encoding for the output file.
  +     * @param encoding Output encoding
  +     */
  +    public void setOutputEncoding( String encoding )
  +    {
  +        if ( encoding != null )
  +            this.outputEncoding = encoding;
  +    }
  +
  +    /**
  +     * Set the classpath to load the Processor through (attribute).
  +     * @param classpath classpath to set
  +     */
  +    public void setClasspath(Path classpath)
  +    {
  +        createClasspath().append(classpath);
  +    }
  +
  +    /**
  +     * Set the classpath to load the Processor through (nested element).
  +     */
  +    public Path createClasspath()
  +    {
  +        if (classpath == null)
  +        {
  +            classpath = new Path(project);
  +        }
  +        return classpath.createPath();
  +    }
  +
  +    /**
  +     * Set the classpath to load the Processor through via reference
  +     * (attribute).
  +     * @param r reference to classpath
  +     */
  +    public void setClasspathRef(Reference r)
  +    {
  +        createClasspath().setRefid(r);
  +    }
  +
  +    /**
  +     * Processes the given input XML file and stores the result
  +     * in the given resultFile.
  +     **/
  +    private void process(File baseDir, String xmlFile, File destDir,
  +                         File stylesheet)
  +        throws BuildException
  +    {
  +
  +        String fileExt=targetExtension;
  +        File   outFile=null;
  +        File   inFile=null;
  +
  +        try
  +        {
  +            long styleSheetLastModified = stylesheet.lastModified();
  +            inFile = new File(baseDir,xmlFile);
  +            int dotPos = xmlFile.lastIndexOf('.');
  +
  +            if(dotPos>0)
  +            {
  +                outFile = new 
File(destDir,xmlFile.substring(0,xmlFile.lastIndexOf('.'))+fileExt);
  +            }
  +            else
  +            {
  +                outFile = new File(destDir,xmlFile+fileExt);
  +            }
  +
  +            if (force ||
  +                inFile.lastModified() > outFile.lastModified() ||
  +                styleSheetLastModified > outFile.lastModified())
  +            {
  +                ensureDirectoryFor( outFile );
  +                log("Processing "+inFile+" to "+outFile);
  +                transform(inFile, outFile);
  +            }
  +        }
  +        catch (Exception ex)
  +        {
               /*
  -             *  get the infile and create the outfilename
  +             * If failed to process document, must delete target document,
  +             * or it will not attempt to process it the second time
                */
  -            String infilename = infiles[i];
  -            String root = infilename.substring( 0, infilename.lastIndexOf('.'));
  -            String outfilename = root + extension;
  -
  -            Writer writer = null;
  -
  -            try
  -            {
  -                File infile = new File( basedir, infilename);
  -                File outfile = new File( destdir, outfilename);
  -                
  -                writer = new BufferedWriter( new FileWriter( outfile ) );
  -
  -                /*
  -                 *  add a user context so we can store the current input
  -                 *  and output filename, which must be interesting to know
  -                 */
  -
  -                VelocityContext userContext = new VelocityContext();
  -
  -                userContext.put("inputfilename", infilename);
  -                userContext.put("outputfilename", outfilename);
  -
  -                dvsl.setUserContext( userContext );
  -
  -                /*
  -                 *  do the transformation
  -                 */
  -                
  -                long time = dvsl.transform( infile, writer );
  +
  +            log("Failed to process " + inFile, Project.MSG_INFO);
   
  -                log("Processed " + infilename + " to " + outfilename + " in " + 
time + " msec.");
  -            }
  -            catch( Exception e )
  -            {
  -                throw new BuildException( "Exception while procesing " + infilename 
+ " : " + e );
  -            }
  -            finally
  -            {
  -                if (writer != null)
  -                {
  -                    try
  -                    {
  -                        writer.flush();
  -                        writer.close();
  -                    }
  -                    catch( Exception ee )
  -                    {
  -                    }
  -                }
  -            }   
  +            if (outFile != null)
  +            {
  +                outFile.delete();
  +            }
  +
  +            throw new BuildException(ex);
  +        }
  +    } //-- processXML
  +
  +    private void process(File inFile, File outFile, File stylesheet)
  +            throws BuildException
  +    {
  +        try
  +        {
  +            long styleSheetLastModified = stylesheet.lastModified();
  +            log("In file "+inFile+" time: " + inFile.lastModified() , 
Project.MSG_DEBUG);
  +            log("Out file "+outFile+" time: " + outFile.lastModified() , 
Project.MSG_DEBUG);
  +            log("Style file "+stylesheet+" time: " + styleSheetLastModified , 
Project.MSG_DEBUG);
  +
  +            if (force ||
  +                inFile.lastModified() > outFile.lastModified() ||
  +                styleSheetLastModified > outFile.lastModified())
  +            {
  +                ensureDirectoryFor( outFile );
  +                log("Processing " + inFile + " to " + outFile, Project.MSG_INFO);
  +                transform(inFile, outFile);
  +            }
           }
  +        catch (Exception ex)
  +        {
  +            log("Failed to process " + inFile, Project.MSG_INFO);
  +
  +            if(outFile!=null)
  +            {
  +                outFile.delete();
  +            }
  +            throw new BuildException(ex);
  +        }
  +    }
   
  -        log("Processing complete.");
  -    }
  +    /**
  +     * <p>
  +     * Does the actual transform
  +     * </p>
  +     *
  +     * @param inFile  XML document source
  +     * @param outFile File for transformed input
  +     *
  +     */
  +    private void transform( File inFile, File outFile )
  +        throws Exception
  +    {
  +        BufferedWriter writer =
  +                new BufferedWriter(new OutputStreamWriter(
  +                                       new FileOutputStream(outFile),
  +                                           outputEncoding));
  +
  +        dvsl.transform( inFile, writer );
  +        writer.close();
  +    }
  +
  +    private void ensureDirectoryFor( File targetFile ) throws BuildException
  +    {
  +        File directory = new File( targetFile.getParent() );
  +
  +        if (!directory.exists())
  +        {
  +            if (!directory.mkdirs())
  +            {
  +                throw new BuildException("Unable to create directory: "
  +                                         + directory.getAbsolutePath() );
  +            }
  +        }
  +    }
  +
  +    /**
  +     * support for <tool> nested element
  +     */
  +    public Tool createTool()
  +    {
  +        Tool p = new Tool();
  +        toolAttr.addElement(p);
  +        return p;
  +    }
  +
  +    public class Tool
  +    {
  +        private String name = null;
  +        private String value = null;
  +
  +        public void setName(String name)
  +        {
  +            this.name = name;
  +        }
  +
  +        public String getName() throws BuildException
  +        {
  +            if (name == null)
  +            {
  +                throw new BuildException("Name attribute is missing.");
  +            }
  +            return name;
  +        }
  +
  +        public void setValue(String value)
  +        {
  +            this.value = value;
  +        }
  +
  +        public String getValue() throws BuildException
  +        {
  +            if (value == null)
  +            {
  +                throw new BuildException("value attribute is missing.");
  +            }
  +            return value;
  +        }
  +    }
  +
  +    /**
  +     *  support for <velconfig> nested element
  +     */
  +    public VelocityConfig createVelConfig()
  +    {
  +        VelocityConfig p = new VelocityConfig();
  +        velConfigAttr.addElement(p);
  +        return p;
  +    }
  +
  +    public class VelocityConfig
  +    {
  +        private String name = null;
  +        private String value = null;
  +
  +        public void setName(String name)
  +        {
  +            this.name = name;
  +        }
  +
  +        public String getName() throws BuildException
  +        {
  +            if (name == null)
  +            {
  +                throw new BuildException("Name attribute is missing.");
  +            }
  +            return name;
  +        }
  +
  +        public void setValue(String value)
  +        {
  +            this.value = value;
  +        }
  +
  +        public String getValue() throws BuildException
  +        {
  +            if (value == null)
  +            {
  +                throw new BuildException("Value attribute is missing.");
  +            }
  +            return value;
  +        }
  +    }
  +
  +    /**
  +     * Set the output type to use for the transformation.  Only "xml" (the
  +     * default) is guaranteed to work for all parsers.  Xalan2 also
  +     * supports "html" and "text".<br>
  +     * <i>Not currently implemented.</i>
  +     * @param type the output method to use
  +     */
  +    /*
  +    public void setOutputtype(String type) {
  +        this.outputtype = type;
  +    }
  +    */
  +
   }
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to