001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.jci.examples.commandline;
019:
020: import java.io.File;
021: import java.net.URL;
022: import java.net.URLClassLoader;
023: import java.util.Iterator;
024:
025: import org.apache.commons.cli.CommandLine;
026: import org.apache.commons.cli.CommandLineParser;
027: import org.apache.commons.cli.GnuParser;
028: import org.apache.commons.cli.Option;
029: import org.apache.commons.cli.OptionBuilder;
030: import org.apache.commons.cli.Options;
031: import org.apache.commons.jci.compilers.CompilationResult;
032: import org.apache.commons.jci.compilers.JavaCompiler;
033: import org.apache.commons.jci.compilers.JavaCompilerFactory;
034: import org.apache.commons.jci.compilers.JavaCompilerSettings;
035: import org.apache.commons.jci.problems.CompilationProblem;
036: import org.apache.commons.jci.problems.CompilationProblemHandler;
037: import org.apache.commons.jci.readers.FileResourceReader;
038: import org.apache.commons.jci.readers.ResourceReader;
039: import org.apache.commons.jci.stores.FileResourceStore;
040: import org.apache.commons.jci.stores.ResourceStore;
041:
042: /**
043: * A simple front end to jci mimicking the javac command line
044: *
045: * @author tcurdt
046: */
047: public final class CommandlineCompiler {
048:
049: public static void main(String[] args) throws Exception {
050:
051: final Options options = new Options();
052:
053: options.addOption(OptionBuilder.withArgName("a.jar:b.jar")
054: .hasArg().withValueSeparator(':').withDescription(
055: "Specify where to find user class files")
056: .create("classpath"));
057:
058: options
059: .addOption(OptionBuilder
060: .withArgName("release")
061: .hasArg()
062: .withDescription(
063: "Provide source compatibility with specified release")
064: .create("source"));
065:
066: options.addOption(OptionBuilder.withArgName("release").hasArg()
067: .withDescription(
068: "Generate class files for specific VM version")
069: .create("target"));
070:
071: options.addOption(OptionBuilder.withArgName("path").hasArg()
072: .withDescription(
073: "Specify where to find input source files")
074: .create("sourcepath"));
075:
076: options.addOption(OptionBuilder.withArgName("directory")
077: .hasArg().withDescription(
078: "Specify where to place generated class files")
079: .create("d"));
080:
081: options
082: .addOption(OptionBuilder
083: .withArgName("num")
084: .hasArg()
085: .withDescription(
086: "Stop compilation after these number of errors")
087: .create("Xmaxerrs"));
088:
089: options
090: .addOption(OptionBuilder
091: .withArgName("num")
092: .hasArg()
093: .withDescription(
094: "Stop compilation after these number of warning")
095: .create("Xmaxwarns"));
096:
097: options.addOption(OptionBuilder.withDescription(
098: "Generate no warnings").create("nowarn"));
099:
100: // final HelpFormatter formatter = new HelpFormatter();
101: // formatter.printHelp("jci", options);
102:
103: final CommandLineParser parser = new GnuParser();
104: final CommandLine cmd = parser.parse(options, args, true);
105:
106: ClassLoader classloader = CommandlineCompiler.class
107: .getClassLoader();
108: File sourcepath = new File(".");
109: File targetpath = new File(".");
110: int maxerrs = 10;
111: int maxwarns = 10;
112: final boolean nowarn = cmd.hasOption("nowarn");
113:
114: final JavaCompiler compiler = new JavaCompilerFactory()
115: .createCompiler("eclipse");
116: final JavaCompilerSettings settings = compiler
117: .createDefaultSettings();
118:
119: for (Iterator it = cmd.iterator(); it.hasNext();) {
120: final Option option = (Option) it.next();
121:
122: if ("classpath".equals(option.getOpt())) {
123: final String[] values = option.getValues();
124: final URL[] urls = new URL[values.length];
125: for (int i = 0; i < urls.length; i++) {
126: urls[i] = new File(values[i]).toURL();
127: }
128: classloader = new URLClassLoader(urls);
129: } else if ("source".equals(option.getOpt())) {
130: settings.setSourceVersion(option.getValue());
131: } else if ("target".equals(option.getOpt())) {
132: settings.setTargetVersion(option.getValue());
133: } else if ("sourcepath".equals(option.getOpt())) {
134: sourcepath = new File(option.getValue());
135: } else if ("d".equals(option.getOpt())) {
136: targetpath = new File(option.getValue());
137: } else if ("Xmaxerrs".equals(option.getOpt())) {
138: maxerrs = Integer.parseInt(option.getValue());
139: } else if ("Xmaxwarns".equals(option.getOpt())) {
140: maxwarns = Integer.parseInt(option.getValue());
141: }
142: }
143:
144: final ResourceReader reader = new FileResourceReader(sourcepath);
145: final ResourceStore store = new FileResourceStore(targetpath);
146:
147: final int maxErrors = maxerrs;
148: final int maxWarnings = maxwarns;
149: compiler
150: .setCompilationProblemHandler(new CompilationProblemHandler() {
151: int errors = 0;
152: int warnings = 0;
153:
154: public boolean handle(
155: final CompilationProblem pProblem) {
156:
157: if (pProblem.isError()) {
158: System.err.println(pProblem);
159:
160: errors++;
161:
162: if (errors >= maxErrors) {
163: return false;
164: }
165: } else {
166: if (!nowarn) {
167: System.err.println(pProblem);
168: }
169:
170: warnings++;
171:
172: if (warnings >= maxWarnings) {
173: return false;
174: }
175: }
176:
177: return true;
178: }
179: });
180:
181: final String[] resource = cmd.getArgs();
182:
183: for (int i = 0; i < resource.length; i++) {
184: System.out.println("compiling " + resource[i]);
185: }
186:
187: final CompilationResult result = compiler.compile(resource,
188: reader, store, classloader);
189:
190: System.out.println(result.getErrors().length + " errors");
191: System.out.println(result.getWarnings().length + " warnings");
192:
193: }
194: }
|