//----------------------------BEGIN LICENSE----------------------------
/* 
 * Willow : the Open Source WorkFlow Project
 * Distributable under GNU LGPL license by gun.org
 *
 * Copyright (C) 2004-2010 huihoo.org 
 * Copyright (C) 2004-2010  ZosaTapo <dertyang@hotmail.com> 
 * 
 * ====================================================================
 * Project Homepage : http://www.huihoo.org/willow
 * Source Forge     : http://sourceforge.net/projects/huihoo
 * Mailing list     : willow@lists.sourceforge.net 
 */
//----------------------------END  LICENSE-----------------------------
package org.huihoo.workflow.script.compiler;
import java.io.OutputStream;

import sun.tools.javac.Main;

public class SunJavaCompiler implements JavaCompiler
{
	private String __outputDir = null;
	private boolean __classDebugInfo = false;
	private String __classPath = null;
	private String __javaEncoding = null;
	private String __javaFilePath = null;
	private OutputStream __out = null;
	public SunJavaCompiler()
	{
	}
	/**
	 *  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(OutputStream 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 boolean compile()
	{
		Main compiler = new Main(__out, "script->javac");
		String[] args;
		if (__classDebugInfo)
		{
			args =
				new String[] {
					"-g",
					"-deprecation",
					"-encoding",
					__javaEncoding,
					"-classpath",
					__classPath,
					"-d",
					__outputDir,
					__javaFilePath };
		}
		else
		{
			args =
				new String[] {
					"-deprecation",
					"-encoding",
					__javaEncoding,
					"-classpath",
					__classPath,
					"-d",
					__outputDir,
					__javaFilePath };
		}
		return compiler.compile(args);
	}
}
