
package org.apache.tools.ant.taskdefs;

import org.apache.tools.ant.*;
import org.apache.tools.ant.types.*;

import java.io.*;
import java.util.*;

/**
 * A ANSI C Precompiler task. Precompiles a file or directory to a new file.
 * Supports standard C #include #define #ifdef, ... statements which can be used
 * even for JAVA-Development or including common parts into a set of html-files.
 *
 * @author Andreas Granzer <a href="mailto:andreas@granzer.com">andreas@granzer.com</a>
 */
public class PreComp extends Task {
    protected File file = null;     // the source file
    protected File destFile = null; // the destination file
    protected File destDir = null;  // the destination directory
    protected Vector filesets = new Vector();

    protected boolean filtering = false;
    protected boolean forceOverwrite = false;
    protected boolean flatten = false;
    protected int verbosity = Project.MSG_VERBOSE;
    protected boolean includeEmpty = true;
		protected String newextension = "html";
		protected String newline = "lf";
		protected String commentStyle = "html";

    protected Hashtable fileCopyMap = new Hashtable();
    protected Hashtable dirCopyMap = new Hashtable();

    /**
     * Sets a single source file to copy.
     */
    public void setFile(File file) {
        this.file = file;
    }

    /**
     * Sets filtering.
     */
    public void setFiltering(boolean filtering) {
        this.filtering = filtering;
    }

    /**
     * Sets extension.
     */
    public void setNewextension(String extension) {
        this.newextension = extension;
    }

    /**
     * Sets newline mode. "lf" for Unix and "crlf" for Windows
     */
    public void setNewline(String newline) {
        this.newline = newline;
    }

    /**
     * Sets commentstyle mode. "C", "Java", "html", "xml", "unix", "none" are possible values.
     */
    public void setCommentstyle(String commentstyle) {
        this.commentStyle = commentStyle;
    }

    /**
     * Overwrite any existing destination file(s).
     */
    public void setOverwrite(boolean overwrite) {
        this.forceOverwrite = overwrite;
    }

    /**
     * When copying directory trees, the files can be "flattened"
     * into a single directory.  If there are multiple files with
     * the same name in the source directory tree, only the first
     * file will be copied into the "flattened" directory, unless
     * the forceoverwrite attribute is true.
     */
    public void setFlatten(boolean flatten) {
        this.flatten = flatten;
    }

    /**
     * Used to force listing of all names of copied files.
     */
    public void setVerbose(boolean verbose) {
        if (verbose) {
            this.verbosity = Project.MSG_INFO;
        } else {
            this.verbosity = Project.MSG_VERBOSE;
        }
    }

    /**
     * Adds a set of files (nested fileset attribute).
     */
    public void addFileset(FileSet set) {
        filesets.addElement(set);
    }

    /**
     * Performs the copy operation.
     */
    public void execute() throws BuildException {
        // make sure we don't have an illegal set of options
        validateAttributes();

        // deal with the single file
        if (file != null) {
            if (destFile == null) {
                destFile = new File(destDir, file.getName());
            }
						fileCopyMap.put(file.getAbsolutePath(), destFile.getAbsolutePath());
        }

        // deal with the filesets
        for (int i=0; i<filesets.size(); i++) {
            FileSet fs = (FileSet) filesets.elementAt(i);
            DirectoryScanner ds = fs.getDirectoryScanner(project);
            File fromDir = fs.getDir(project);

            String[] srcFiles = ds.getIncludedFiles();
            String[] srcDirs = ds.getIncludedDirectories();

            scan(fromDir, destDir, srcFiles, srcDirs);
        }

        // do all the copy operations now...
        doFileOperations();

        // clean up destDir again - so this instance can be used a second
        // time without throwing an exception
        if (destFile != null) {
            destDir = null;
        }
    }

//************************************************************************
//  protected and private methods
//************************************************************************

    /**
     * Ensure we have a consistent and legal set of attributes, and set
     * any internal flags necessary based on different combinations
     * of attributes.
     */
    protected void validateAttributes() throws BuildException {
        if (file == null && filesets.size() == 0) {
            throw new BuildException("Specify at least one source - a file or a fileset.");
        }

        if (file != null && file.exists() && file.isDirectory()) {
            throw new BuildException("Use a fileset to copy directories.");
        }

    }

    /**
     * Compares source files to destination files to see if they should be
     * copied.
     */
    protected void scan(File fromDir, File toDir, String[] files, String[] dirs) {
        for (int i = 0; i < files.length; i++) {
            String filename = files[i];
            File src = new File(fromDir, filename);
            File dest;
            if (flatten) {
                dest = new File(toDir, new File(filename).getName());
            } else {
                dest = new File(toDir, filename);
            }
						fileCopyMap.put(src.getAbsolutePath(),dest.getAbsolutePath());
        }
    }

    /**
     * Actually does the file (and possibly empty directory) copies.
     * This is a good method for subclasses to override.
     */
    protected void doFileOperations() {
        if (fileCopyMap.size() > 0) {
            log("Precomp " + fileCopyMap.size() + " files" );

            Enumeration e = fileCopyMap.keys();
            while (e.hasMoreElements()) {
                String fromFile = (String) e.nextElement();
                String toFile = (String) fileCopyMap.get(fromFile);

                try {
										log("Copying " + fromFile + " to " + toFile, verbosity);
									  Vector includedirs = new Vector();
			              includedirs.add(".");
										int pos = fromFile.lastIndexOf(File.separator);
										if ( pos > 1 ) {
										    String filedir = fromFile.substring(0,pos);
										    //System.out.println(filedir);
										    includedirs.add(filedir);
										}

										String newFile = fromFile.substring(0,fromFile.lastIndexOf('.')+1)+newextension;
										//System.out.println(newFile);
										OutputStream out = new FileOutputStream(newFile);
										JavaPreProcessor jpp = new JavaPreProcessor(fromFile,
										             out,
																 includedirs,
																 new Hashtable());
										jpp.setNewline(newline);
										jpp.setCommentStyle(commentStyle);
										jpp.preprocess();

                } catch (IOException ioe) {
                    String msg = "Failed to copy " + fromFile + " to " + toFile
                        + " due to " + ioe.getMessage();
                    throw new BuildException(msg, ioe, location);
                }
            }
        }

    }

}
