hello,

this update now handles overwriting old files...

<target name="xmlc" depends="prepare">
<xmlc sourcedir="${source}">
<fileset dir="${presentation}">
<include name="*.html"/>
<exclude name="*.java" />
<exclude name="*.class" />
</fileset>
</xmlc>
</target>




-------------------------------------------------------

Stanislas Pinte
Software engineer - Trademine-europe
Tel: 00 32 486 67 78 86

-------------------------------------------------------
/**
 * @author [EMAIL PROTECTED]
 * @date 22-11-2000
 */
package test;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.DirectoryScanner;

import org.enhydra.xml.xmlc.commands.xmlc.XMLC;

import java.util.List;
import java.util.ArrayList;
import java.io.File;

/**
 * note: the current version of this task xmlcompiles the files in place,
 * and override them by default.
 */
public class xmlcTask extends Task
{
    private FileSet fileSet;
    private File sourceDir;
    private String message;
    
    /**
     * The method executing the task
     */
    public void execute() throws BuildException
    {
        //System.out.println("it works!");
        //String[] args = {"-class", "subscribe.presentation.SimpleHTML", 
"-keep",
        //  "-d" , "./subscribe/presentation/",  
"./subscribe/presentation/Simple.html"};
        //XMLC.main(args);
        
        //parse the fileSet, and call xmlc on each one.
        DirectoryScanner scanner = fileSet.getDirectoryScanner(getProject());
        String[] files = scanner.getIncludedFiles();
        //debug
        File dir = scanner.getBasedir();
        for (int i=0; i<files.length; i++)
        {
            //System.out.println("file[" + i + "] = " + dir.getAbsolutePath() + 
dir.separator + files[i]);
            String source = dir.getAbsolutePath() + dir.separator +  files[i];
            //System.out.println("source = " + source);
            String packageName = dir.getName() + ".";
            File parentDir = dir.getParentFile();
            packageName = parentDir.getName() + "." + packageName;
            //System.out.println("parentDir = " + parentDir);
            while (!parentDir.equals(sourceDir))
            {
                packageName = parentDir.getName() + "." + packageName;
                parentDir = parentDir.getParentFile();
            }
            //System.out.println(packageName);
            String destinationName = dir.getAbsolutePath() + dir.separator + 
removeExtension(files[i], ".html") + "HTML" + ".java";
            String destination = packageName + removeExtension(files[i], 
".html") + "HTML";
            //System.out.println("destination = " + destination);
            
            //check if source file newer than destination file. TODO.
            
            xmlc(source, destination, 
sourceDir.getParentFile().getAbsolutePath(), destinationName, true, true, 
false, true);
        }
    }
    
    /**
     * call xmlc on a source.
     * the default destinationPath is "."
     * the default is not to keep the generated source file.
     */
    private void xmlc(String source, String destination, String 
destinationDirectory, String destinationName,
                      boolean keepSource, boolean nocompile, boolean verbose, 
boolean overwrite)
    {
        
        //1: try to find the existing destination file
        System.out.println(destinationName);
        File destFile = new File(destinationName);
        if (destFile.exists() && overwrite)
        {
            destFile.delete();
            //System.out.println("deleting existing file");
        }
        
        
        List params = new ArrayList();
        
        params.add("-class");
        params.add(destination);
        
        if (keepSource)
        {
            params.add("-keep");
            params.add("-sourceout");
            params.add(destinationDirectory);
        }
        
        if(destinationDirectory != null)
        {
            params.add("-d");
            params.add(destinationDirectory);
        }
        
        if (nocompile)
        {
            params.add("-nocompile");
        }
        
        if (verbose)
        {
            params.add("-verbose");
        }
        
        //params.add("-validate");
        //params.add("no");
        
        params.add(source);
        
        String[] args = (String[])params.toArray(new String[0]);
        XMLC.main(args);
        
        //System.out.println("xmlcompiled a file");
    }
    
    /**
     * remove the extension from a file name.
     * I shoudl probably make that method fail-safe...
     */
    private String removeExtension(String source, String extension)
    {
        //the extension is always located at the end of the string,
        //--> it is just a question of removing the x last letters of the 
string.
        return source.substring(0, source.length() - extension.length());
    }
    
    public void addFileset(FileSet fileSet)
    {
        this.fileSet = fileSet;
    }
    
    /**
     * The setter for the "message" attribute
     */
    public void setMessage(String msg) {
        this.message = message;
    }
    
    public void setSourceDir(File sourceDir)
    {
        this.sourceDir = sourceDir;
    }
}

Reply via email to