/***********************************************************
 * $Id$
 * 
 * OOo templates for Tapestry of the clazzes.org project
 * http://www.clazzes.org
 *
 * Created: 29.07.2007
 *
 *  This library 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 2.1 of the License, or (at your option) any later version.
 *
 *  This library 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 this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 * 
 ***********************************************************/

package org.clazzes.tapestry.ooo.maven;

import java.io.File;
import java.io.IOException;

import javax.xml.transform.TransformerException;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;

/**
 * This mojo allows the extraction of internationalized text inside tapestry
 * pages.
 * 
 * @goal gentemplates
 * 
 * @author wglas
 */
public class GenTemplatesMojo extends AbstractMojo {

    /**
     * The directory to search for OOo file, which have to be translated.
     * 
     * @parameter default-value="${basedir}/src/main/webapp"
     */
    private File oooDirectory;

    /**
     * The directory where to put the compiled templates resp. the directory
     * where page files are located.
     * 
     * @parameter default-value="${basedir}/src/main/webapp"
     */
    private File pageDirectory;

    private void ooo2JWCFile(File from_file, File to_file) throws MojoExecutionException {
        
        this.getLog().debug(
                "ooo2JWCFile(File " + from_file.getPath()
                        + ", File " + to_file.getPath() + "): called");
        if (to_file.exists()
                && (to_file.lastModified() > from_file.lastModified())) {
            this.getLog().debug(
                    "ooo2JWCFile(File " + from_file.getPath()
                            + ", File " + to_file.getPath()
                            + "): target file is newer, skipping source file");
            return;
        }

        this.getLog().info("Converting " + from_file.getPath());
        try {
            OdtTemplate tmpl = new OdtTemplate(this.getLog());
            tmpl.readTemplate(from_file);
            tmpl.replaceHints();
            tmpl.writeTemplate(to_file);
        } catch (IOException e) {
            throw new MojoExecutionException("I/O error processing OOo file ["+ from_file +"]",e);
        } catch (TransformerException e) {
            throw new MojoExecutionException("XML transformer failed processing OOo file ["+ from_file +"]",e);
        } 
    }

    private void ooo2JWCFiles(File from_dir, File to_dir)
            throws MojoExecutionException {
        
        this.getLog().debug(
                "ooo2JWCFiles(File " + from_dir.getPath()
                        + ", File " + to_dir.getPath() + "): called");
        if (!to_dir.exists())
            to_dir.mkdirs();
        File[] direntries = from_dir.listFiles();
        for (File direntry : direntries) {
            
            if (direntry.getName().startsWith(".")) // catches ".", "..",
                // ".svn", ...
                continue;

            if (direntry.isDirectory()) {
                File to_subdir = new File(to_dir.getPath() + File.separator
                        + direntry.getName());
                this.ooo2JWCFiles(direntry, to_subdir);
                continue;
            }
            
            String lname = direntry.getName().toLowerCase();
            
            if (lname.endsWith(".odt") || lname.endsWith(".ods")) {
                int l = direntry.getName().length();
                File to_file = new File(to_dir.getPath() + File.separator
                        + direntry.getName().substring(0, l - 4) + ".xml");
                this.ooo2JWCFile(direntry, to_file);
                continue;
            }
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.apache.maven.plugin.Mojo#execute()
     */
    public void execute() throws MojoExecutionException, MojoFailureException {

        // start the recursion here.
        this.ooo2JWCFiles(this.oooDirectory,this.pageDirectory);
    }
}
