/*
 * Created on 2004-3-14
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package workflow.script.compiler;
import java.io.PrintWriter;

import workflow.script.ScriptException;

import com.sun.tools.javac.v8.Main;

/**
 * @author zosatapo
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class JavaCompiler implements ScriptCompiler
{
	private String  __outputDir = null;
	private boolean __classDebugInfo = false;
	private String  __classPath = null;
	private String  __javaEncoding = null;
 
	private String __javaFilePath = null;

	private PrintWriter __out = null;

	public JavaCompiler()
	{
	}
	/**
	 *  Where should I keep generated code including java and class?
	 */
	public void setOutputDir(String outputDir)
	{
		this.__outputDir = outputDir;
	}
	/**
	 * Should we include debug information in compiled class?
	 */
	public void setClassDebugInfo(boolean classDebugInfo)
	{
		this.__classDebugInfo = classDebugInfo;

	}
	/**
	 * What classpath should I use while compiling the java source
	 * generated from script files?
	 */
	public void setClassPath(String classPath)
	{
		this.__classPath = classPath;
	}
	/**
	 * Java platform encoding to generate the script file class.
	 */
	public void setJavaEncoding(String javaEncoding)
	{
		this.__javaEncoding = javaEncoding;
	}
	/**
	 * Set where you want the compiler output (messages) to go 
	 */
	public void setMsgOutput(PrintWriter out)
	{
		this.__out = out;
	}

	/**
	 * Full path name of the Java source file to be compiled
	 */
	public void setJavaFilePath(String javaFilePath)
	{
		this.__javaFilePath = javaFilePath;
	}

	/**
	* Execute the compiler
	* @param source - file name of the source to be compiled
	*/
	public void compile() throws ScriptException
	{
		Main compiler = new Main("script->javac", __out);
		String[] args;
		if (__classDebugInfo)
		{
			args =
				new String[] {
					"-g",
					"-encoding",
					__javaEncoding,
					"-classpath",
					__classPath,
					"-d",
					__outputDir,
					__javaFilePath };
		}
		else
		{
			args =
				new String[] {
					"-encoding",
					__javaEncoding,
					"-classpath",
					__classPath,
					"-d",
					__outputDir,
					__javaFilePath };
		}
		if(compiler.compile(args)!=0)
	  {
	  	throw new ScriptException("--------compile falied---------");
	  }
	}
}
