/**
* Squidy Interaction 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 3 of the License,
* or (at your option) any later version.
*
* Squidy Interaction 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 Squidy Interaction Library. If not, see
* <http://www.gnu.org/licenses/>.
*
* 2009 Human-Computer Interaction Group, University of Konstanz.
* <http://hci.uni-konstanz.de>
*
* Please contact info@squidy-lib.de or visit our website
* <http://www.squidy-lib.de> for further information.
*/
package org.squidy.common.dynamiccode;
import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
/**
* <code>Javac</code>.
*
* <pre>
* Date: Mar 30, 2009
* Time: 7:21:22 PM
* </pre>
*
*
* @author Roman Rädle <a
* href="mailto:Roman.Raedle@uni-konstanz.de">Roman
* .Raedle@uni-konstanz.de</a> Human-Computer Interaction Group
* University of Konstanz
*
* @version $Id: Javac.java 772 2011-09-16 15:39:44Z raedle $
* @since 1.0.0
*/
public final class Javac {
String classpath;
String outputdir;
String sourcepath;
String bootclasspath;
String extdirs;
String encoding;
String target;
public Javac(String classpath, String outputdir) {
this.classpath = classpath;
this.outputdir = outputdir;
}
/**
* Compile the given source files.
*
* @param srcFiles
* @return null if success; or compilation errors
*/
public String compile(String srcFiles[]) {
StringWriter err = new StringWriter();
PrintWriter errPrinter = new PrintWriter(err);
String args[] = buildJavacArgs(srcFiles);
int resultCode = com.sun.tools.javac.Main.compile(args, errPrinter);
errPrinter.close();
return (resultCode == 0) ? null : err.toString();
}
public String compile(File srcFiles[]) {
String paths[] = new String[srcFiles.length];
for (int i = 0; i < paths.length; i++) {
paths[i] = srcFiles[i].getAbsolutePath();
}
return compile(paths);
}
private String[] buildJavacArgs(String srcFiles[]) {
ArrayList args = new ArrayList();
if (classpath != null) {
args.add("-classpath");
args.add(classpath);
}
if (outputdir != null) {
args.add("-d");
args.add(outputdir);
}
if (sourcepath != null) {
args.add("-sourcepath");
args.add(sourcepath);
}
if (bootclasspath != null) {
args.add("-bootclasspath");
args.add(bootclasspath);
}
if (extdirs != null) {
args.add("-extdirs");
args.add(extdirs);
}
if (encoding != null) {
args.add("-encoding");
args.add(encoding);
}
if (target != null) {
args.add("-target");
args.add(target);
}
for (int i = 0; i < srcFiles.length; i++) {
args.add(srcFiles[i]);
}
return (String[]) args.toArray(new String[args.size()]);
}
public String getBootclasspath() {
return bootclasspath;
}
public void setBootclasspath(String bootclasspath) {
this.bootclasspath = bootclasspath;
}
public String getClasspath() {
return classpath;
}
public void setClasspath(String classpath) {
this.classpath = classpath;
}
public String getEncoding() {
return encoding;
}
public void setEncoding(String encoding) {
this.encoding = encoding;
}
public String getExtdirs() {
return extdirs;
}
public void setExtdirs(String extdirs) {
this.extdirs = extdirs;
}
public String getOutputdir() {
return outputdir;
}
public void setOutputdir(String outputdir) {
this.outputdir = outputdir;
}
public String getSourcepath() {
return sourcepath;
}
public void setSourcepath(String sourcepath) {
this.sourcepath = sourcepath;
}
public String getTarget() {
return target;
}
public void setTarget(String target) {
this.target = target;
}
}