
package org.bodington.ant;

import java.io.File;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.Reference;

import org.bodington.installation.TemplateBuilder;

/**
 * An ANT task that wraps {@link org.bodington.installation.TemplateBuilder}.
 * All the attributes for this class are mandatory. For the sake of simplicity
 * it was not written to accept any nested elements.
 * <h3>Use</h3>
 * <p>
 * As this task is not a core ANT task, you will need to declare it in your
 * build file. You will need to add something such as the following (the nested
 * <code>classpath</code> element tells ANT where to find the task).
 * </p>
 * 
 * <pre>
 * 
 *  &lt;taskdef name=&quot;template-compiler&quot; classname=&quot;org.bodington.ant.TemplateBuilderTask&quot;&gt;
 *    &lt;classpath&gt;
 *      &lt;pathelement location=&quot;${build}/bodserver.jar&quot;/&gt;
 *    &lt;/classpath&gt;
 *  &lt;/taskdef&gt;
 *  
 * </pre>
 * 
 * <h3>Parameters</h3>
 * <table cellspacing="0" border="1"> <thead>
 * <tr>
 * <th>Attribute</th>
 * <th>Description</th>
 * <th>Required</th>
 * </tr>
 * </thead> <tbody>
 * <tr>
 * <td>templatedir</td>
 * <td>the source directory of the (HTML) templates.</td>
 * <td>Yes</td>
 * </tr>
 * <tr>
 * <td>templateclasses</td>
 * <td>the output directory for the template class files.</td>
 * <td>Yes</td>
 * </tr>
 * <tr>
 * <td>bodhome</td>
 * <td>sets the value of the system property <code>bodington.home</code>.
 * See the example below.</td>
 * <td>Yes</td>
 * </tr>
 * <tr>
 * <td>buildpathref</td>
 * <td>a reference to the CLASSPATH required by the underlying task. See the
 * example below. <br>
 * <B>NOTE: </B> having the <code>buildpathref</code> as a reference to what
 * ANT terms a <i>path-like structure </i> allows for the CLASSPATH to be
 * expressed in a cross-platform way (even a mixture of OS platform
 * conventions!).</td>
 * <td>Yes</td>
 * </tr>
 * </tbody> </table>
 * <h3>Examples</h3>
 * After having declared the task with a <task>taskdef</code>, you can declare
 * the <i>path-like structure </i> to which your <code>buildpathref</code>
 * refers in the following way.
 * 
 * <pre>
 * 
 *  &lt;path id=&quot;template.build.path&quot;&gt;
 *  &lt;pathelement location=&quot;${java.home}/lib/tools.jar&quot;/&gt;
 *  &lt;pathelement location=&quot;${build.bodington.classes}&quot;/&gt;
 *  &lt;pathelement location=&quot;${tomcat-4}/common/lib/servlet.jar&quot;/&gt;
 *  &lt;/path&gt; 
 *  
 * </pre>
 * 
 * You can then call the task (within a target) using the following entry:
 * 
 * <pre>
 * 
 *  &lt;template-compiler templatedir=&quot;${build.bodington}/templates&quot;
 *  templateclasses=&quot;${build.bodington}/WEB-INF/template_classes&quot;
 *  bodhome=&quot;${build.bodington}/WEB-INF&quot;
 *  buildpathref=&quot;template.build.path&quot;/&gt;
 *  
 * </pre>
 * 
 * @author Alexis O'Connor
 * @see <a href="http://ant.apache.org/">Apache ANT </a>
 */
public class TemplateBuilderTask extends Task
{
    private String buildpath;
    private String templatedir;
    private String templateclasses;
    private String bodhome;

    /**
     * Set the value of the <code>buildpathref</code> attribute.
     * @param buildpathref the value to be set.
     */
    public void setBuildpathRef(Reference buildpathref)
    {
        Path path = new Path(getProject());
        path.setRefid(buildpathref);
        this.buildpath = path.toString();
    }

    /**
     * Set the value of the <code>templatedir</code> attribute.
     * @param templatedir the value to be set.
     */
    public void setTemplatedir(File templatedir)
    {
        this.templatedir = templatedir.getAbsolutePath();
    }

    /**
     * Set the value of the <code>templateclasses</code> attribute.
     * @param templateclasses the value to be set.
     */
    public void setTemplateclasses(File templateclasses)
    {
        this.templateclasses = templateclasses.getAbsolutePath();
    }

    /**
     * Set the value of the <code>bodhome</code> attribute.
     * @param bodhome the value to be set.
     */
    public void setBodHome(File bodhome)
    {
        this.bodhome = bodhome.getAbsolutePath();
    }

    /**
     * Execute the task.
     * @see org.apache.tools.ant.Task#execute()
     */
    public void execute()
    {
        String[] args = new String[]{templatedir, templateclasses, buildpath};
        System.setProperty("bodington.home", bodhome);
        TemplateBuilder.main(args);
    }
}
