blob: ab9a2c3f5d9d7985d7f7e31aad2d21c02859bb33 [file] [log] [blame]
/*
$Id$
Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
Redistribution and use of this software and associated documentation
("Software"), with or without modification, are permitted provided
that the following conditions are met:
1. Redistributions of source code must retain copyright
statements and notices. Redistributions must also contain a
copy of this document.
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 name "groovy" must not be used to endorse or promote
products derived from this Software without prior written
permission of The Codehaus. For written permission,
please contact info@codehaus.org.
4. Products derived from this Software may not be called "groovy"
nor may "groovy" appear in their names without prior written
permission of The Codehaus. "groovy" is a registered
trademark of The Codehaus.
5. Due credit should be given to The Codehaus -
http://groovy.codehaus.org/
THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
``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 CODEHAUS 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.
*/
package org.codehaus.groovy.wiki;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
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.util.GlobPatternMapper;
import org.apache.tools.ant.util.SourceFileScanner;
import org.radeox.api.engine.RenderEngine;
import org.radeox.api.engine.context.RenderContext;
import org.radeox.engine.BaseRenderEngine;
import org.radeox.engine.context.BaseRenderContext;
/**
* Converts the Wiki markup into XML/HTML so that it can be styled
* by the Maven build
*
* @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
* @version $Revision$
*/
public class Wiki2Markup extends MatchingTask {
private Path src;
private File destDir;
protected boolean failOnError = true;
protected boolean listFiles = false;
protected File[] compileList = new File[0];
private GlobPatternMapper m = new GlobPatternMapper();
private RenderContext context;
private RenderEngine engine;
public static void main(String[] args) {
try {
Wiki2Markup engine = new Wiki2Markup();
engine.compileFiles(args);
}
catch (Exception e) {
System.out.println("Caught: " + e);
e.printStackTrace();
}
}
public Wiki2Markup() {
context = new BaseRenderContext();
engine = createRenderEngine();
m.setFrom("*.wiki");
m.setTo(getExtension());
}
/**
* Adds a path for source compilation.
*
* @return a nested src element.
*/
public Path createSrc() {
if (src == null) {
src = new Path(getProject());
}
return src.createPath();
}
/**
* Recreate src.
*
* @return a nested src element.
*/
protected Path recreateSrc() {
src = null;
return createSrc();
}
/**
* Set the source directories to find the source Java files.
* @param srcDir the source directories as a path
*/
public void setSrcdir(Path srcDir) {
if (src == null) {
src = srcDir;
}
else {
src.append(srcDir);
}
}
/**
* Gets the source dirs to find the source java files.
* @return the source directorys as a path
*/
public Path getSrcdir() {
return src;
}
/**
* Set the destination directory into which the Java source
* files should be compiled.
* @param destDir the destination director
*/
public void setDestdir(File destDir) {
this.destDir = destDir;
}
/**
* Gets the destination directory into which the java source files
* should be compiled.
* @return the destination directory
*/
public File getDestdir() {
return destDir;
}
/**
* If true, list the source files being handed off to the compiler.
* @param list if true list the source files
*/
public void setListfiles(boolean list) {
listFiles = list;
}
/**
* Get the listfiles flag.
* @return the listfiles flag
*/
public boolean getListfiles() {
return listFiles;
}
/**
* Indicates whether the build will continue
* even if there are compilation errors; defaults to true.
* @param fail if true halt the build on failure
*/
public void setFailonerror(boolean fail) {
failOnError = fail;
}
/**
* @ant.attribute ignore="true"
* @param proceed inverse of failoferror
*/
public void setProceed(boolean proceed) {
failOnError = !proceed;
}
/**
* Gets the failonerror flag.
* @return the failonerror flag
*/
public boolean getFailonerror() {
return failOnError;
}
/**
* Executes the task.
* @exception BuildException if an error occurs
*/
public void execute() throws BuildException {
checkParameters();
resetFileLists();
// scan source directories and dest directory to build up
// compile lists
String[] list = src.list();
for (int i = 0; i < list.length; i++) {
File srcDir = getProject().resolveFile(list[i]);
if (!srcDir.exists()) {
throw new BuildException("srcdir \"" + srcDir.getPath() + "\" does not exist!", getLocation());
}
DirectoryScanner ds = this.getDirectoryScanner(srcDir);
String[] files = ds.getIncludedFiles();
scanDir(srcDir, destDir != null ? destDir : srcDir, files);
}
compile();
}
/**
* Clear the list of files to be compiled and copied..
*/
protected void resetFileLists() {
compileList = new File[0];
}
/**
* Scans the directory looking for source files to be compiled.
* The results are returned in the class variable compileList
*
* @param srcDir The source directory
* @param destDir The destination directory
* @param files An array of filenames
*/
protected void scanDir(File srcDir, File destDir, String[] files) {
SourceFileScanner sfs = new SourceFileScanner(this);
File[] newFiles = sfs.restrictAsFiles(files, srcDir, destDir, m);
if (newFiles.length > 0) {
File[] newCompileList = new File[compileList.length + newFiles.length];
System.arraycopy(compileList, 0, newCompileList, 0, compileList.length);
System.arraycopy(newFiles, 0, newCompileList, compileList.length, newFiles.length);
compileList = newCompileList;
}
}
/**
* Gets the list of files to be compiled.
* @return the list of files as an array
*/
public File[] getFileList() {
return compileList;
}
protected void checkParameters() throws BuildException {
if (src == null) {
throw new BuildException("srcdir attribute must be set!", getLocation());
}
if (src.size() == 0) {
throw new BuildException("srcdir attribute must be set!", getLocation());
}
if (destDir != null && !destDir.isDirectory()) {
throw new BuildException(
"destination directory \"" + destDir + "\" does not exist " + "or is not a directory",
getLocation());
}
}
public void compileFiles(String[] args) throws IOException {
for (int i = 0; i < args.length; i++) {
File file = new File(args[i]);
compile(file, args[i]);
}
}
protected void compile() {
if (compileList.length > 0) {
log(
"Compiling "
+ compileList.length
+ " source file"
+ (compileList.length == 1 ? "" : "s")
+ (destDir != null ? " to " + destDir : ""));
try {
for (int i = 0; i < compileList.length; i++) {
String filename = compileList[i].getAbsolutePath();
if (listFiles) {
log(filename);
}
compile(compileList[i], compileList[i].getName());
}
}
catch (Exception e) {
String message = "Compile failed: " + e;
if (failOnError) {
throw new BuildException(message, e, getLocation());
}
else {
log(message, Project.MSG_ERR);
}
}
}
}
protected void compile(File file, String name) throws IOException {
String[] names = m.mapFileName(name);
String outputName = names[0];
context.set("name", name);
String text = readFile(file);
String result = engine.render(text, context);
File outputFile = new File(getDestdir(), outputName);
System.out.println("Creating file: " + outputFile);
FileWriter writer = new FileWriter(outputFile);
result = filter(result);
writer.write(result);
writer.close();
}
protected String filter(String result) {
return "<html><body>\n" + result + "\n<body><html>\n";
}
protected String readFile(File file) throws IOException {
StringBuffer buffer = new StringBuffer();
BufferedReader reader = new BufferedReader(new FileReader(file));
while (true) {
String line = reader.readLine();
if (line == null) {
break;
}
buffer.append(line);
buffer.append("\n");
}
return buffer.toString();
}
protected RenderEngine createRenderEngine() {
return new BaseRenderEngine();
}
protected String getExtension() {
return "*.html";
}
}