KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > language > programming > java > AbstractJavaCompiler


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.language.programming.java;
17
18 import org.apache.avalon.framework.logger.AbstractLogEnabled;
19 import org.apache.avalon.excalibur.pool.Recyclable;
20 import org.apache.cocoon.components.language.programming.LanguageCompiler;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.InputStreamReader JavaDoc;
26 import java.util.List JavaDoc;
27
28 /**
29  * This class implements the functionality common to all Java compilers.
30  * @author <a HREF="mailto:stefano@apache.org">Stefano Mazzocchi</a>
31  * @version CVS $Id: AbstractJavaCompiler.java 156602 2005-03-09 03:55:53Z antonio $
32  * @since 2.0
33  */

34 public abstract class AbstractJavaCompiler extends AbstractLogEnabled implements LanguageCompiler, Recyclable {
35
36     /**
37      * The source program filename
38      */

39     protected String JavaDoc file;
40
41     /**
42      * The name of the directory containing the source program file
43      */

44     protected String JavaDoc srcDir;
45
46     /**
47      * The name of the directory to contain the resulting object program file
48      */

49     protected String JavaDoc destDir;
50
51     /**
52      * The classpath to be used for compilation
53      */

54     protected String JavaDoc classpath;
55
56     /**
57      * The encoding of the source program or <code>null</code> to use the
58      * platform's default encoding
59      */

60     protected String JavaDoc encoding = null;
61
62     /**
63      * The version of the JVM for wich the code was written.
64      * i.e: 130 = Java 1.3, 140 = Java 1.4 and 150 = Java 1.5
65      */

66     protected int compilerComplianceLevel;
67
68     /**
69      * The input stream to output compilation errors
70      */

71     protected InputStream JavaDoc errors;
72
73     /**
74      * Set the name of the file containing the source program
75      *
76      * @param file The name of the file containing the source program
77      */

78     public void setFile(String JavaDoc file) {
79         this.file = file;
80     }
81
82     /**
83      * Set the name of the directory containing the source program file
84      *
85      * @param srcDir The name of the directory containing the source program file
86      */

87     public void setSource(String JavaDoc srcDir) {
88         this.srcDir = srcDir;
89     }
90
91     /**
92      * Set the name of the directory to contain the resulting object program file
93      *
94      * @param destDir The name of the directory to contain the resulting object
95      * program file
96      */

97     public void setDestination(String JavaDoc destDir) {
98         this.destDir = destDir;
99     }
100
101     /**
102      * Set the classpath to be used for this compilation
103      *
104      * @param classpath The classpath to be used for this compilation
105      */

106     public void setClasspath(String JavaDoc classpath) {
107         this.classpath = classpath;
108     }
109
110     /**
111      * Set the encoding of the input source file or <code>null</code> to use the
112      * platform's default encoding
113      *
114      * @param encoding The encoding of the input source file or <code>null</code>
115      * to use the platform's default encoding
116      */

117     public void setEncoding(String JavaDoc encoding) {
118         this.encoding = encoding;
119     }
120
121     /**
122      * Set the version of the java source code to be compiled
123      *
124      * @param compilerComplianceLevel The version of the JVM for wich the code was written.
125      * i.e: 130 = Java 1.3, 140 = Java 1.4 and 150 = Java 1.5
126      *
127      * @since 2.1.7
128      */

129     public void setCompilerComplianceLevel(int compilerComplianceLevel) {
130         this.compilerComplianceLevel = compilerComplianceLevel;
131     }
132
133     /**
134      * Return the list of errors generated by this compilation
135      *
136      * @return The list of errors generated by this compilation
137      * @exception IOException If an error occurs during message collection
138      */

139     public List JavaDoc getErrors() throws IOException JavaDoc {
140         return parseStream(new BufferedReader JavaDoc(new InputStreamReader JavaDoc(errors)));
141     }
142
143     /**
144      * Parse the compiler error stream to produce a list of
145      * <code>CompilerError</code>s
146      *
147      * @param errors The error stream
148      * @return The list of compiler error messages
149      * @exception IOException If an error occurs during message collection
150      */

151     protected abstract List JavaDoc parseStream(BufferedReader JavaDoc errors)
152             throws IOException JavaDoc;
153
154     /**
155      * Fill the arguments taken by the Java compiler
156      *
157      * @param arguments The list of compilation arguments
158      * @return The prepared list of compilation arguments
159      */

160
161     protected List JavaDoc fillArguments(List JavaDoc arguments) {
162         // add compiler compliance level
163
/*arguments.add("-source");
164         switch (compilerComplianceLevel) {
165             case 150:
166                 arguments.add("5");
167                 break;
168             case 140:
169                 //arguments.add("-target");
170                 arguments.add("1.4");
171                 break;
172             default:
173                 //arguments.add("-target");
174                 arguments.add("1.3");
175         }*/

176         // destination directory
177
arguments.add("-d");
178         arguments.add(destDir);
179
180         // classpath
181
arguments.add("-classpath");
182         arguments.add(classpath);
183
184         // sourcepath
185
arguments.add("-sourcepath");
186         arguments.add(srcDir);
187
188         // add optimization (for what is worth)
189
arguments.add("-O");
190         
191         // add encoding if set
192
if (encoding != null) {
193             arguments.add("-encoding");
194             arguments.add(encoding);
195         }
196         return arguments;
197     }
198
199     /**
200      * Copy arguments to a string array
201      *
202      * @param arguments The compiler arguments
203      * @return A string array containing compilation arguments
204      */

205     protected String JavaDoc[] toStringArray(List JavaDoc arguments) {
206         int i;
207         String JavaDoc[] args = new String JavaDoc[arguments.size() + 1];
208
209         for (i = 0; i < arguments.size(); i++) {
210             args[i] = (String JavaDoc)arguments.get(i);
211         }
212
213         args[i] = file;
214
215         return args;
216     }
217
218     /** Reset all internal state.
219      * This method is called by the component manager before this
220      * component is return to its pool.
221      */

222     public void recycle() {
223         file = null;
224         srcDir = null;
225         destDir = null;
226         classpath = null;
227         encoding = null;
228         errors = null;
229     }
230 }
231
Popular Tags