001: /*
002: * Janino - An embedded Java[TM] compiler
003: *
004: * Copyright (c) 2001-2007, Arno Unkrig
005: * All rights reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * 2. Redistributions in binary form must reproduce the above
014: * copyright notice, this list of conditions and the following
015: * disclaimer in the documentation and/or other materials
016: * provided with the distribution.
017: * 3. The name of the author may not be used to endorse or promote
018: * products derived from this software without specific prior
019: * written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
022: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
023: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
024: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
025: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
026: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
027: * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
028: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
029: * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
030: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
031: * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
032: */
033:
034: package org.codehaus.janino;
035:
036: import java.io.*;
037:
038: import org.apache.tools.ant.taskdefs.compilers.*;
039: import org.apache.tools.ant.types.Path;
040: import org.codehaus.janino.util.enumerator.*;
041:
042: /**
043: * A simple {@link org.apache.tools.ant.taskdefs.compilers.CompilerAdapter} for the "ant" tool
044: * that silently ignores most of the configuration parameters and attempts to compile all given
045: * source files into class files.
046: */
047: public class AntCompilerAdapter extends DefaultCompilerAdapter {
048:
049: /**
050: * Compile all source files in <code>DefaultCompilerAdapter.compileList</code> individually and
051: * write class files in directory <code>DefaultCompilerAdapter.destDir</code>.
052: * <p>
053: * The following fields of {@link DefaultCompilerAdapter} are honored:
054: * <ul>
055: * <li><code>DefaultCompilerAdapter.compileList</code> - the set of Java<sup>TM</sup> source files to compile
056: * <li><code>DefaultCompilerAdapter.destDir</code> - where to store the class files
057: * <li><code>DefaultCompilerAdapter.compileSourcepath</code> - where to look for more Java<sup>TM</sup> source files
058: * <li><code>DefaultCompilerAdapter.compileClasspath</code> - where to look for required classes
059: * <li><code>DefaultCompilerAdapter.extdirs</code>
060: * <li><code>DefaultCompilerAdapter.bootclasspath</code>
061: * <li><code>DefaultCompilerAdapter.encoding</code> - how the Java<sup>TM</sup> source files are encoded
062: * <li><code>DefaultCompilerAdapter.verbose</code>
063: * <li><code>DefaultCompilerAdapter.debug</code>
064: * <li><code>org.apache.tools.ant.taskdefs.Javac.getDebugLevel()</code>
065: * <li><code>DefaultCompilerAdapter.src</code>
066: * </ul>
067: * The following fields of {@link DefaultCompilerAdapter} are not honored at this time:
068: * <ul>
069: * <li><code>DefaultCompilerAdapter.depend</code>
070: * <li><code>DefaultCompilerAdapter.deprecation</code>
071: * <li><code>DefaultCompilerAdapter.includeAntRuntime</code>
072: * <li><code>DefaultCompilerAdapter.includeJavaRuntime</code>
073: * <li><code>DefaultCompilerAdapter.location</code>
074: * <li><code>DefaultCompilerAdapter.optimize</code>
075: * <li><code>DefaultCompilerAdapter.target</code>
076: * </ul>
077: *
078: * @return "true" on success
079: */
080: public boolean execute() {
081:
082: // Convert source files into source file names.
083: File[] sourceFiles = this .compileList;
084:
085: // Determine output directory.
086: File destinationDirectory = this .destDir == null ? Compiler.NO_DESTINATION_DIRECTORY
087: : this .destDir;
088:
089: // Determine the source path.
090: File[] optionalSourcePath = AntCompilerAdapter
091: .pathToFiles(this .compileSourcepath != null ? this .compileSourcepath
092: : this .src);
093:
094: // Determine the class path.
095: File[] classPath = AntCompilerAdapter.pathToFiles(
096: this .compileClasspath, new File[] { new File(".") });
097:
098: // Determine the ext dirs.
099: File[] optionalExtDirs = AntCompilerAdapter
100: .pathToFiles(this .extdirs);
101:
102: // Determine the boot class path
103: File[] optionalBootClassPath = AntCompilerAdapter
104: .pathToFiles(this .bootclasspath);
105:
106: // Determine the encoding.
107: String optionalCharacterEncoding = this .encoding;
108:
109: // Determine verbosity.
110: boolean verbose = this .verbose;
111:
112: // Determine debugging information.
113: EnumeratorSet debuggingInformation;
114: if (!this .debug) {
115: debuggingInformation = DebuggingInformation.NONE;
116: } else {
117: String debugLevel = this .attributes.getDebugLevel();
118: if (debugLevel == null) {
119: debuggingInformation = DebuggingInformation.DEFAULT_DEBUGGING_INFORMATION;
120: } else {
121: try {
122: debuggingInformation = new EnumeratorSet(
123: DebuggingInformation.class, debugLevel);
124: } catch (EnumeratorFormatException ex) {
125: debuggingInformation = DebuggingInformation.NONE;
126: }
127: }
128: }
129:
130: // Compile all source files.
131: try {
132: new Compiler(optionalSourcePath, classPath,
133: optionalExtDirs, optionalBootClassPath,
134: destinationDirectory, optionalCharacterEncoding,
135: verbose, debuggingInformation,
136: Compiler.DEFAULT_WARNING_HANDLE_PATTERNS, false // rebuild
137: ).compile(sourceFiles);
138: } catch (Scanner.ScanException e) {
139: System.out.println(e.getMessage());
140: return false;
141: } catch (Parser.ParseException e) {
142: System.out.println(e.getMessage());
143: return false;
144: } catch (CompileException e) {
145: System.out.println(e.getMessage());
146: return false;
147: } catch (IOException e) {
148: System.out.println(e.getMessage());
149: return false;
150: }
151: return true;
152: }
153:
154: /**
155: * Convert a {@link org.apache.tools.ant.types.Path} into an array of
156: * {@link File}.
157: * @param path
158: * @return The converted path, or <code>null</code> if <code>path</code> is <code>null</code>
159: */
160: private static File[] pathToFiles(Path path) {
161: if (path == null)
162: return null;
163:
164: String[] fileNames = path.list();
165: File[] files = new File[fileNames.length];
166: for (int i = 0; i < fileNames.length; ++i)
167: files[i] = new File(fileNames[i]);
168: return files;
169: }
170:
171: /**
172: * Convert a {@link org.apache.tools.ant.types.Path} into an array of
173: * {@link File}.
174: * @param path
175: * @param defaultValue
176: * @return The converted path, or, if <code>path</code> is <code>null</code>, the <code>defaultValue</code>
177: */
178: private static File[] pathToFiles(Path path, File[] defaultValue) {
179: if (path == null)
180: return defaultValue;
181: return AntCompilerAdapter.pathToFiles(path);
182: }
183: }
|