package net.java.textilej.util.anttask;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import net.java.textilej.parser.TextileParser;
import net.java.textilej.parser.builder.HtmlDocumentBuilder;
import net.java.textilej.parser.tag.confluence.ConfluenceDialect;
import net.java.textilej.util.XmlStreamWriter;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.FileSet;
/**
* An Ant task for converting textile to HTML format.
*
* @author dgreen
*/
public class TextileToHtmlTask extends Task {
private List<FileSet> filesets = new ArrayList<FileSet>();
private String htmlFilenameFormat = "$1.html";
private boolean overwrite = true;
/**
* Adds a set of files to process.
*/
public void addFileset(FileSet set) {
filesets.add(set);
}
@Override
public void execute() throws BuildException {
if (filesets.isEmpty()) {
throw new BuildException("Please add one or more source filesets");
}
for (FileSet fileset: filesets) {
File filesetBaseDir = fileset.getDir(getProject());
DirectoryScanner ds = fileset.getDirectoryScanner(getProject());
String[] files = ds.getIncludedFiles();
if (files != null) {
File baseDir = ds.getBasedir();
for (String file: files) {
File inputFile = new File(baseDir,file);
try {
processFile(filesetBaseDir,inputFile);
} catch (BuildException e) {
throw e;
} catch (Exception e) {
throw new BuildException(String.format("Cannot process file '%s': %s",inputFile,e.getMessage()),e);
}
}
}
}
}
private void processFile(final File baseDir,final File source) throws BuildException {
log(String.format("Processing file '%s'",source),Project.MSG_VERBOSE);
String textile = null;
String name = source.getName();
if (name.lastIndexOf('.') != -1) {
name = name.substring(0,name.lastIndexOf('.'));
}
File htmlOutputFile = new File(source.getParentFile(),htmlFilenameFormat.replace("$1", name));
if (!htmlOutputFile.exists() || overwrite || htmlOutputFile.lastModified() < source.lastModified()) {
if (textile == null) {
textile = readFully(source);
}
Writer writer;
try {
writer = new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(htmlOutputFile)),"utf-8");
} catch (Exception e) {
throw new BuildException(String.format("Cannot write to file '%s': %s",htmlOutputFile,e.getMessage()),e);
}
try {
HtmlDocumentBuilder builder = new HtmlDocumentBuilder(writer) {
@Override
protected XmlStreamWriter createXmlStreamWriter(Writer out) {
return super.createFormattingXmlStreamWriter(out);
}
};
TextileParser parser = new TextileParser();
parser.setBuilder(builder);
parser.setDialect(new ConfluenceDialect());
builder.setEmitDtd(true);
parser.parse(textile);
} finally {
try {
writer.close();
} catch (Exception e) {
throw new BuildException(String.format("Cannot write to file '%s': %s",htmlOutputFile,e.getMessage()),e);
}
}
}
}
private String readFully(File inputFile) {
StringWriter w = new StringWriter();
try {
Reader r = new InputStreamReader(new BufferedInputStream(new FileInputStream(inputFile)));
try {
int i;
while ((i = r.read()) != -1) {
w.write((char)i);
}
} finally {
r.close();
}
} catch (IOException e) {
throw new BuildException(String.format("Cannot read file '%s': %s",inputFile,e.getMessage()),e);
}
return w.toString();
}
/**
* @see #setHtmlFilenameFormat(String)
*/
public String getHtmlFilenameFormat() {
return htmlFilenameFormat;
}
/**
* The format of the HTML output file. Consists of a pattern where the
* '$1' is replaced with the filename of the input file. Default value is
* <code>$1.html</code>
*
* @param htmlFilenameFormat
*/
public void setHtmlFilenameFormat(String htmlFilenameFormat) {
this.htmlFilenameFormat = htmlFilenameFormat;
}
}