Date: Thu, 23 Nov 2000 17:21:35 +0100
To: [email protected]
From: Stanislas Pinte <[EMAIL PROTECTED]>
Subject: enhydra xmlc task update
hello,
a first patched version of my xmlc task, with a sample calling code.
<target name="xmlc" depends="prepare">
<xmlc sourcedir="${source}">
<fileset dir="${presentation}">
<include name="*.html"/>
<exclude name="*.java" />
<exclude name="*.class" />
</fileset>
</xmlc>
</target>
The sourcedir is the directory in which you can find your sources.
The fileset specifies wich files below the sourcedir will be taken into
account for xmlcompilation.
comments are welcomed.
Stan.
-------------------------------------------------------
Stanislas Pinte
Software engineer - Trademine-europe
Tel: 00 32 486 67 78 86
-------------------------------------------------------
-------------------------------------------------------
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 destination = packageName + removeExtension(files[i],
".html") + "HTML";
//System.out.println("destination = " + destination);
xmlc(source, destination,
sourceDir.getParentFile().getAbsolutePath(), true, true, 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,
boolean keepSource, boolean nocompile, boolean verbose)
{
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(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;
}
}