/* ======================================================================
The Bodington System Software License, Version 1.0
  
Copyright (c) 2001 The University of Leeds.  All rights reserved.
  
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

1.  Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

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 end-user documentation included with the redistribution, if any,
must include the following acknowledgement:  "This product includes
software developed by the University of Leeds
(http://www.bodington.org/)."  Alternately, this acknowledgement may
appear in the software itself, if and wherever such third-party
acknowledgements normally appear.

4.  The names "Bodington", "Nathan Bodington", "Bodington System",
"Bodington Open Source Project", and "The University of Leeds" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
d.gardner@leeds.ac.uk.

5.  The name "Bodington" may not appear in the name of products derived
from this software without prior written permission of the University of
Leeds.

THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO,  TITLE,  THE IMPLIED WARRANTIES 
OF QUALITY  AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO 
EVENT SHALL THE UNIVERSITY OF LEEDS 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.
=========================================================

This software was originally created by the University of Leeds and may contain voluntary 
contributions from others.  For more information on the Bodington Open Source Project, please 
see http://bodington.org/

====================================================================== */

package org.bodington.ant;

import java.io.File;
import java.util.Vector;
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}.
 * At this moment in time, it does not 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>xmlwarn</td>
 * <td>sets whether or not XML parse errors and warnings in the templates are 
 * reported (default: <code>true</code>).</td>
 * <td>No</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;
    private boolean xmlWarn = true;

    /**
     * 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();
    }
    
    /**
     * Set the value of the <code>xmlwarn</code> attribute. If 
     * <code>true</code>, errors and warnings in the XML templates will be 
     * reported (default: <code>true</code>). 
     * @param xmlWarn the value to be set.
     */
    public void setXmlWarn(boolean xmlWarn)
    {
        this.xmlWarn = xmlWarn;
    }

    /**
     * Execute the task.
     * @see org.apache.tools.ant.Task#execute()
     */
    public void execute()
    {
        Vector v = new Vector();
        v.add("-templateDir");
        v.add(templatedir);
        v.add("-classesDir");
        v.add(templateclasses);
        v.add("-classpath");
        v.add(buildpath);
        v.add("-bodHome");
        v.add(bodhome);
        if (xmlWarn)
            v.add("-xmlWarn");
        String[] args = (String[]) v.toArray(new String[v.size()]);
        TemplateBuilder.main(args);
    }
}
