/*
 *  Copyright 2006-2009 Mikhail Kryshen
 *
 *  This file is part of Tema.
 *
 *  Tema is free software: you can redistribute it and/or modify it
 *  under the terms of the GNU Lesser General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  Tema is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the 
 *  GNU Lesser General Public License along with Tema.  
 *  If not, see <http://www.gnu.org/licenses/>.
 */

package kryshen.tema.ant;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import kryshen.tema.TemplateException;
import kryshen.tema.TemplateParser;
import kryshen.tema.io.NullWriter;
import kryshen.tema.io.TemplateReader;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

/**
 * Tema support for ant.
 *
 * @author Mikhail Kryshen
 */
public class TemaTask extends Task {
    
    public static class Variable {
        private String name;
        private String value;
        
        public void setName(String name) {
            this.name = name;
        }
        
        public void setValue(String value) {
            this.value = value;
        }
        
        public void setPath(File file) {
            this.value = file.getAbsolutePath();
        }
    }
    
    private File basedir = null;
    
    private File infile = null;
    private File outfile = null;
    
    private Collection<Variable> variables = new ArrayList<Variable>();
    
    @Override
    public void execute() throws BuildException {
        if (basedir == null) {
            basedir = getProject().getBaseDir();
        }
        
        if (infile == null) {
            throw new BuildException("no infile specified", getLocation());
        }
        
        try {
            executeTema();
        } catch (IOException e) {
            throw new BuildException(e);
        } catch (TemplateException e) {
            throw new BuildException(e);
        }
    }
    
    private void executeTema() throws IOException, TemplateException {
        TemplateParser parser = new TemplateParser(basedir);
        
        for (Variable var : variables) {
            if (var.name == null) {
                throw new BuildException(
                        "no name specified for variable", getLocation());
            }
            
            if (var.value == null) {
                throw new BuildException(
                        "no value specified for variable", getLocation());
            }
            
            parser.getContext().set(var.name, var.value);
        }
        
        TemplateReader in = new TemplateReader(
                new BufferedReader(new FileReader(infile)),
                infile.getName());
        try {
            Writer out;
            
            if (outfile != null) {
                out = new BufferedWriter(new FileWriter(outfile));
            } else {
                out = NullWriter.INSTANCE;
            }
            
            try {
                parser.parse(in, out);
            } finally {
                out.close();
            }
        } finally {
            in.close();
        }
    }
    
    public void setBasedir(File basedir) {
        this.basedir = basedir;
    }
    
    public void setInfile(File infile) {
        this.infile = infile;
    }
    
    public void setOutfile(File outfile) {
        this.outfile = outfile;
    }
    
    public void addVariable(Variable variable) {
        variables.add(variable);
    }
}