KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > components > compiler > AbstractCompiler


1 /*
2  * Copyright 2001-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
17 package org.apache.axis.components.compiler;
18
19 import java.io.BufferedReader JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.InputStreamReader JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25
26 /**
27  * This class implements the functionality common to all Java compilers.
28  * @author <a HREF="mailto:dims@yahoo.com">Davanum Srinivas</a>
29  * @author <a HREF="mailto:stefano@apache.org">Stefano Mazzocchi</a>
30  * @since 2.0
31  */

32 public abstract class AbstractCompiler implements Compiler JavaDoc {
33
34   /**
35    * The source program filenames
36    */

37   protected ArrayList JavaDoc fileList = new ArrayList JavaDoc();
38
39   /**
40    * The name of the directory containing the source program file
41    */

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

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

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

58   protected String JavaDoc encoding = null;
59
60   /**
61    * The input stream to output compilation errors
62    */

63   protected InputStream JavaDoc errors;
64
65   /**
66    * Add the name of the file containing the source program to the file list
67    *
68    * @param file The name of the file containing the source program
69    */

70   public void addFile(String JavaDoc file) {
71     this.fileList.add(file);
72   }
73
74   /**
75    * Set the name of the directory containing the source program file
76    *
77    * @param srcDir The name of the directory containing the source program file
78    */

79   public void setSource(String JavaDoc srcDir) {
80     this.srcDir = srcDir;
81   }
82
83   /**
84    * Set the name of the directory to contain the resulting object program file
85    *
86    * @param destDir The name of the directory to contain the resulting object
87    * program file
88    */

89   public void setDestination(String JavaDoc destDir) {
90       this.destDir = destDir;
91   }
92
93   /**
94    * Set the classpath to be used for this compilation
95    *
96    * @param classpath The classpath to be used for this compilation
97    */

98   public void setClasspath(String JavaDoc classpath) {
99     this.classpath = classpath;
100   }
101
102   /**
103    * Set the encoding of the input source file or <code>null</code> to use the
104    * platform's default encoding
105    *
106    * @param encoding The encoding of the input source file or <code>null</code>
107    * to use the platform's default encoding
108    */

109   public void setEncoding(String JavaDoc encoding) {
110     this.encoding = encoding;
111   }
112
113   /**
114    * Return the list of errors generated by this compilation
115    *
116    * @return The list of errors generated by this compilation
117    * @exception IOException If an error occurs during message collection
118    */

119   public List JavaDoc getErrors() throws IOException JavaDoc {
120     return parseStream(new BufferedReader JavaDoc(new InputStreamReader JavaDoc(errors)));
121   }
122
123   /**
124    * Parse the compiler error stream to produce a list of
125    * <code>CompilerError</code>s
126    *
127    * @param errors The error stream
128    * @return The list of compiler error messages
129    * @exception IOException If an error occurs during message collection
130    */

131   protected abstract List JavaDoc parseStream(BufferedReader JavaDoc errors)
132       throws IOException JavaDoc;
133
134   /**
135    * Fill the arguments taken by the Java compiler
136    *
137    * @param arguments The list of compilation arguments
138    * @return The prepared list of compilation arguments
139    */

140   protected List JavaDoc fillArguments(List JavaDoc arguments) {
141     // destination directory
142
arguments.add("-d");
143     arguments.add(destDir);
144
145     // classpath
146
arguments.add("-classpath");
147     arguments.add(classpath);
148
149     // sourcepath
150
if(srcDir != null) {
151         arguments.add("-sourcepath");
152         arguments.add(srcDir);
153     }
154
155     // add optimization (for what is worth)
156
arguments.add("-O");
157
158     // add debug option
159
arguments.add("-g");
160
161     // add encoding if set
162
if (encoding != null) {
163       arguments.add("-encoding");
164       arguments.add(encoding);
165     }
166
167     return arguments;
168   }
169
170   /**
171    * Copy arguments to a string array
172    *
173    * @param arguments The compiler arguments
174    * @return A string array containing compilation arguments
175    */

176   protected String JavaDoc[] toStringArray(List JavaDoc arguments) {
177     int i;
178     String JavaDoc[] args = new String JavaDoc[arguments.size() + fileList.size()];
179
180     for (i = 0; i < arguments.size(); i++) {
181       args[i] = (String JavaDoc) arguments.get(i);
182     }
183
184     for (int j=0; j < fileList.size(); i++,j++) {
185         args[i] = (String JavaDoc)fileList.get(j);
186     }
187     return args;
188   }
189 }
190
Popular Tags