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.cocoon.components.language.programming.java;
018:
019: import org.apache.avalon.framework.logger.AbstractLogEnabled;
020: import org.apache.avalon.excalibur.pool.Recyclable;
021: import org.apache.cocoon.components.language.programming.LanguageCompiler;
022:
023: import java.io.BufferedReader;
024: import java.io.IOException;
025: import java.io.InputStream;
026: import java.io.InputStreamReader;
027: import java.util.List;
028:
029: /**
030: * This class implements the functionality common to all Java compilers.
031: * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
032: * @version CVS $Id: AbstractJavaCompiler.java 433543 2006-08-22 06:22:54Z crossley $
033: * @since 2.0
034: */
035: public abstract class AbstractJavaCompiler extends AbstractLogEnabled
036: implements LanguageCompiler, Recyclable {
037:
038: /**
039: * The source program filename
040: */
041: protected String file;
042:
043: /**
044: * The name of the directory containing the source program file
045: */
046: protected String srcDir;
047:
048: /**
049: * The name of the directory to contain the resulting object program file
050: */
051: protected String destDir;
052:
053: /**
054: * The classpath to be used for compilation
055: */
056: protected String classpath;
057:
058: /**
059: * The encoding of the source program or <code>null</code> to use the
060: * platform's default encoding
061: */
062: protected String encoding = null;
063:
064: /**
065: * The version of the JVM for wich the code was written.
066: * i.e: 130 = Java 1.3, 140 = Java 1.4 and 150 = Java 1.5
067: */
068: protected int compilerComplianceLevel;
069:
070: /**
071: * The input stream to output compilation errors
072: */
073: protected InputStream errors;
074:
075: /**
076: * Set the name of the file containing the source program
077: *
078: * @param file The name of the file containing the source program
079: */
080: public void setFile(String file) {
081: this .file = file;
082: }
083:
084: /**
085: * Set the name of the directory containing the source program file
086: *
087: * @param srcDir The name of the directory containing the source program file
088: */
089: public void setSource(String srcDir) {
090: this .srcDir = srcDir;
091: }
092:
093: /**
094: * Set the name of the directory to contain the resulting object program file
095: *
096: * @param destDir The name of the directory to contain the resulting object
097: * program file
098: */
099: public void setDestination(String destDir) {
100: this .destDir = destDir;
101: }
102:
103: /**
104: * Set the classpath to be used for this compilation
105: *
106: * @param classpath The classpath to be used for this compilation
107: */
108: public void setClasspath(String classpath) {
109: this .classpath = classpath;
110: }
111:
112: /**
113: * Set the encoding of the input source file or <code>null</code> to use the
114: * platform's default encoding
115: *
116: * @param encoding The encoding of the input source file or <code>null</code>
117: * to use the platform's default encoding
118: */
119: public void setEncoding(String encoding) {
120: this .encoding = encoding;
121: }
122:
123: /**
124: * Set the version of the java source code to be compiled
125: *
126: * @param compilerComplianceLevel The version of the JVM for wich the code was written.
127: * i.e: 130 = Java 1.3, 140 = Java 1.4 and 150 = Java 1.5
128: *
129: * @since 2.1.7
130: */
131: public void setCompilerComplianceLevel(int compilerComplianceLevel) {
132: this .compilerComplianceLevel = compilerComplianceLevel;
133: }
134:
135: /**
136: * Return the list of errors generated by this compilation
137: *
138: * @return The list of errors generated by this compilation
139: * @exception IOException If an error occurs during message collection
140: */
141: public List getErrors() throws IOException {
142: return parseStream(new BufferedReader(new InputStreamReader(
143: errors)));
144: }
145:
146: /**
147: * Parse the compiler error stream to produce a list of
148: * <code>CompilerError</code>s
149: *
150: * @param errors The error stream
151: * @return The list of compiler error messages
152: * @exception IOException If an error occurs during message collection
153: */
154: protected abstract List parseStream(BufferedReader errors)
155: throws IOException;
156:
157: /**
158: * Fill the arguments taken by the Java compiler
159: *
160: * @param arguments The list of compilation arguments
161: * @return The prepared list of compilation arguments
162: */
163:
164: protected List fillArguments(List arguments) {
165: // add compiler compliance level
166: /*arguments.add("-source");
167: switch (compilerComplianceLevel) {
168: case 150:
169: arguments.add("5");
170: break;
171: case 140:
172: //arguments.add("-target");
173: arguments.add("1.4");
174: break;
175: default:
176: //arguments.add("-target");
177: arguments.add("1.3");
178: }*/
179: // destination directory
180: arguments.add("-d");
181: arguments.add(destDir);
182:
183: // classpath
184: arguments.add("-classpath");
185: arguments.add(classpath);
186:
187: // sourcepath
188: arguments.add("-sourcepath");
189: arguments.add(srcDir);
190:
191: // add optimization (for what is worth)
192: arguments.add("-O");
193:
194: // add encoding if set
195: if (encoding != null) {
196: arguments.add("-encoding");
197: arguments.add(encoding);
198: }
199: return arguments;
200: }
201:
202: /**
203: * Copy arguments to a string array
204: *
205: * @param arguments The compiler arguments
206: * @return A string array containing compilation arguments
207: */
208: protected String[] toStringArray(List arguments) {
209: int i;
210: String[] args = new String[arguments.size() + 1];
211:
212: for (i = 0; i < arguments.size(); i++) {
213: args[i] = (String) arguments.get(i);
214: }
215:
216: args[i] = file;
217:
218: return args;
219: }
220:
221: /** Reset all internal state.
222: * This method is called by the component manager before this
223: * component is return to its pool.
224: */
225: public void recycle() {
226: file = null;
227: srcDir = null;
228: destDir = null;
229: classpath = null;
230: encoding = null;
231: errors = null;
232: }
233: }
|