//       __ __   ____ ___      JaCo -- extensible Java compiler
//    _ / //  \ / __// _ \     (c) 1998-2003, Matthias Zenger
//  / // //_/\ \\ \__\___/     
//  \___/     \_\\__/          A compiler adaptor for Jakarta Ant
//
//  [[id: JacoAntAdaptor.java (2184) 22-Feb-03 02:09]]

package org.zenger.jaco.adapt;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Execute;
import org.apache.tools.ant.taskdefs.Javac;
import org.apache.tools.ant.taskdefs.LogStreamHandler;
import org.apache.tools.ant.taskdefs.compilers.DefaultCompilerAdapter;
import org.apache.tools.ant.types.Commandline;
import org.apache.tools.ant.types.Path;
import java.io.IOException;


public class JacoAntAdaptor extends DefaultCompilerAdapter {
    
    private String source;
    private String target;

    
    public String compilerName() {
        return "JaCo";
    }
    
    public void setJavac(Javac attributes) {
        super.setJavac(attributes);
        JacoAntTask myattribs = (JacoAntTask)attributes;
        source = myattribs.getSource();
        target = myattribs.getTarget();
    }

    public boolean execute() throws BuildException {
        attributes.log(
            "Using " + compilerName() + " as Java compiler",
            Project.MSG_VERBOSE);
        return runCompiler(setupJacoCommand().getArguments());
    }
    
    public Commandline setupJacoCommand() {
        Commandline cmd = new Commandline();
        setupJavacCommandlineSwitches(cmd);
        setupJacoCommandlineSwitches(cmd);
        logAndAddFilesToCompile(cmd);
        return cmd;
    }
    
    public void setupJacoCommandlineSwitches(Commandline cmd) {
        if (source != null) {
            cmd.createArgument().setValue("-source");
            cmd.createArgument().setValue(source);
        }
        if (target != null) {
            cmd.createArgument().setValue("-target");
            cmd.createArgument().setValue(target);
        }
    }
    
    public boolean runCompiler(String[] args) {
        try {
            return new Jaco().run(args);
        } catch (Throwable e) {
            return false;
        }
    }
}
