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: package org.apache.commons.jci.compilers;
018:
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.io.OutputStream;
022: import java.io.Reader;
023: import java.io.Writer;
024: import java.net.URI;
025: import java.util.ArrayList;
026: import java.util.Collection;
027: import java.util.Iterator;
028: import java.util.List;
029: import java.util.Set;
030:
031: import javax.tools.Diagnostic;
032: import javax.tools.DiagnosticCollector;
033: import javax.tools.FileObject;
034: import javax.tools.JavaCompilerTool;
035: import javax.tools.JavaFileManager;
036: import javax.tools.JavaFileObject;
037: import javax.tools.SimpleJavaFileObject;
038: import javax.tools.ToolProvider;
039: import javax.tools.JavaFileObject.Kind;
040:
041: import org.apache.commons.jci.problems.CompilationProblem;
042: import org.apache.commons.jci.readers.ResourceReader;
043: import org.apache.commons.jci.stores.ResourceStore;
044: import org.apache.commons.lang.ArrayUtils;
045: import org.apache.commons.logging.Log;
046: import org.apache.commons.logging.LogFactory;
047:
048: public final class Jsr199JavaCompiler extends AbstractJavaCompiler {
049:
050: private final Log log = LogFactory.getLog(Jsr199JavaCompiler.class);
051:
052: private class CompilationUnit extends SimpleJavaFileObject {
053: final private ResourceReader reader;
054: final private String name;
055:
056: public CompilationUnit(final String pName,
057: final ResourceReader pReader) {
058: super (URI.create("reader:///" + pName), Kind.SOURCE);
059: reader = pReader;
060: name = pName;
061: }
062:
063: public boolean delete() {
064: log.debug("delete");
065: return super .delete();
066: }
067:
068: public CharSequence getCharContent(boolean arg0)
069: throws IOException {
070: log.debug("getCharContent of " + name);
071: byte[] content = reader.getBytes(name);
072: return new String(content);
073: }
074:
075: public Kind getKind() {
076: log.debug("getKind" + super .getKind());
077: return super .getKind();
078: }
079:
080: public long getLastModified() {
081: log.debug("getLastModified");
082: return super .getLastModified();
083: }
084:
085: public String getName() {
086: log.debug("getName " + super .getName());
087: return super .getName();
088: }
089:
090: public boolean isNameCompatible(String arg0, Kind arg1) {
091: log.debug("isNameCompatible " + arg0);
092: return super .isNameCompatible(arg0, arg1);
093: }
094:
095: public InputStream openInputStream() throws IOException {
096: log.debug("openInputStream");
097: return super .openInputStream();
098: }
099:
100: public OutputStream openOutputStream() throws IOException {
101: log.debug("openOutputStream");
102: return super .openOutputStream();
103: }
104:
105: public Reader openReader(boolean arg0) throws IOException {
106: log.debug("openReader");
107: return super .openReader(arg0);
108: }
109:
110: public Writer openWriter() throws IOException {
111: log.debug("openWriter");
112: return super .openWriter();
113: }
114:
115: public URI toUri() {
116: log.debug("toUri " + super .toUri());
117: return super .toUri();
118: }
119:
120: }
121:
122: private class JciJavaFileManager implements JavaFileManager {
123: private final ResourceStore store;
124: final Collection<JavaFileObject> units;
125:
126: public JciJavaFileManager(
127: final Collection<JavaFileObject> pUnits,
128: final ResourceStore pStore) {
129: store = pStore;
130: units = pUnits;
131: }
132:
133: public void close() {
134: log.debug("close");
135: }
136:
137: public void flush() {
138: log.debug("flush");
139: }
140:
141: public ClassLoader getClassLoader(
142: JavaFileManager.Location location) {
143: log.debug("getClassLoader");
144: return null;
145: }
146:
147: public FileObject getFileForInput(
148: JavaFileManager.Location location, String packageName,
149: String relativeName) {
150: log.debug("getFileForInput");
151: return null;
152: }
153:
154: public FileObject getFileForOutput(
155: JavaFileManager.Location location, String packageName,
156: String relativeName, FileObject sibling) {
157: log.debug("getFileForOutput");
158: return null;
159: }
160:
161: public JavaFileObject getJavaFileForInput(
162: JavaFileManager.Location location, String className,
163: JavaFileObject.Kind kind) {
164: log.debug("getJavaFileForInput");
165: return null;
166: }
167:
168: public JavaFileObject getJavaFileForOutput(
169: JavaFileManager.Location location, String className,
170: JavaFileObject.Kind kind, FileObject sibling) {
171: log.debug("getJavaFileForOutput");
172: return null;
173: }
174:
175: public int isSupportedOption(String option) {
176: log.debug("isSupportedOption");
177: return 0;
178: }
179:
180: public boolean handleOption(String current,
181: Iterator<String> remaining) {
182: log.debug("handleOption");
183: return false;
184: }
185:
186: public boolean hasLocation(JavaFileManager.Location location) {
187: log.debug("hasLocation");
188: return false;
189: }
190:
191: public String inferBinaryName(
192: JavaFileManager.Location location, JavaFileObject file) {
193: log.debug("inferBinaryName " + file.getName());
194: return file.getName().replaceFirst(".java", ".class");
195: }
196:
197: public Iterable<JavaFileObject> list(
198: JavaFileManager.Location location, String packageName,
199: Set<JavaFileObject.Kind> kinds, boolean recurse) {
200: log.debug("list " + location + packageName + kinds
201: + recurse);
202: return units;
203: }
204: }
205:
206: public CompilationResult compile(final String[] pResourcePaths,
207: final ResourceReader pReader, final ResourceStore pStore,
208: final ClassLoader classLoader) {
209:
210: final Collection<JavaFileObject> units = new ArrayList<JavaFileObject>();
211: for (int i = 0; i < pResourcePaths.length; i++) {
212: final String sourcePath = pResourcePaths[i];
213: log.debug("compiling " + sourcePath);
214: units.add(new CompilationUnit(sourcePath, pReader));
215: }
216:
217: final JavaCompilerTool compiler = ToolProvider
218: .getSystemJavaCompilerTool();
219: // final JavaFileManager fileManager = compiler.getStandardFileManager(diagnostics);
220: final JavaFileManager fileManager = new JciJavaFileManager(
221: units, pStore);
222: final DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
223:
224: compiler.getTask(null, fileManager, diagnostics, null, null,
225: units).run();
226:
227: try {
228: fileManager.close();
229: } catch (IOException e) {
230: // TODO Auto-generated catch block
231: e.printStackTrace();
232: }
233:
234: final List<Diagnostic<? extends JavaFileObject>> jsrProblems = diagnostics
235: .getDiagnostics();
236: final CompilationProblem[] problems = new CompilationProblem[jsrProblems
237: .size()];
238: int i = 0;
239: for (final Diagnostic<? extends JavaFileObject> jsrProblem : jsrProblems) {
240: problems[i++] = new Jsr199CompilationProblem(jsrProblem);
241: }
242:
243: return new CompilationResult(problems);
244: }
245:
246: }
|