int maxerrs = 10;
int maxwarns = 10;
final boolean nowarn = cmd.hasOption("nowarn");
final JavaCompiler compiler = new JavaCompilerFactory().createCompiler("eclipse");
final JavaCompilerSettings settings = compiler.createDefaultSettings();
for (Iterator it = cmd.iterator(); it.hasNext();) {
final Option option = (Option) it.next();
if ("classpath".equals(option.getOpt())) {
final String[] values = option.getValues();
final URL[] urls = new URL[values.length];
for (int i = 0; i < urls.length; i++) {
urls[i] = new File(values[i]).toURL();
}
classloader = new URLClassLoader(urls);
} else if ("source".equals(option.getOpt())) {
settings.setSourceVersion(option.getValue());
} else if ("target".equals(option.getOpt())) {
settings.setTargetVersion(option.getValue());
} else if ("sourcepath".equals(option.getOpt())) {
sourcepath = new File(option.getValue());
} else if ("d".equals(option.getOpt())) {
targetpath = new File(option.getValue());
} else if ("Xmaxerrs".equals(option.getOpt())) {
maxerrs = Integer.parseInt(option.getValue());
} else if ("Xmaxwarns".equals(option.getOpt())) {
maxwarns = Integer.parseInt(option.getValue());
}
}
final ResourceReader reader = new FileResourceReader(sourcepath);
final ResourceStore store = new FileResourceStore(targetpath);
final int maxErrors = maxerrs;
final int maxWarnings = maxwarns;
compiler.setCompilationProblemHandler(new CompilationProblemHandler() {
int errors = 0;
int warnings = 0;
public boolean handle(final CompilationProblem pProblem) {
if (pProblem.isError()) {
System.err.println(pProblem);
errors++;
if (errors >= maxErrors) {
return false;
}
} else {
if (!nowarn) {
System.err.println(pProblem);
}
warnings++;
if (warnings >= maxWarnings) {
return false;
}
}
return true;
}
});
final String[] resource = cmd.getArgs();
for (int i = 0; i < resource.length; i++) {
System.out.println("compiling " + resource[i]);
}
final CompilationResult result = compiler.compile(resource, reader, store, classloader);
System.out.println( result.getErrors().length + " errors");
System.out.println( result.getWarnings().length + " warnings");
}