KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.BufferedReader JavaDoc;
19 import java.io.ByteArrayInputStream JavaDoc;
20 import java.io.ByteArrayOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.PrintWriter JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.NoSuchElementException JavaDoc;
26 import java.util.StringTokenizer JavaDoc;
27
28 import org.apache.cocoon.components.language.programming.CompilerError;
29 import org.apache.commons.lang.SystemUtils;
30
31 /**
32  * This class wraps the Sun's Javac Compiler.
33  *
34  * @author <a HREF="mailto:stefano@apache.org">Stefano Mazzocchi</a>
35  * @version CVS $Id: Javac.java 156687 2005-03-09 20:55:09Z antonio $
36  * @since 2.0
37  */

38
39 public class Javac extends AbstractJavaCompiler {
40
41   public Javac() {
42       // EMPTY
43
}
44
45   /**
46    * Compile a source file yielding a loadable class file.
47    *
48    * <code>null</code> if it is the platform's default encoding
49    * @exception IOException If an error occurs during compilation
50    */

51   public boolean compile() throws IOException JavaDoc {
52       
53     ByteArrayOutputStream JavaDoc err = new ByteArrayOutputStream JavaDoc();
54     
55     boolean result;
56     if (!SystemUtils.IS_JAVA_1_3) { // For Java 1.4 and 1.5
57
PrintWriter JavaDoc pw = new PrintWriter JavaDoc(err);
58         result = com.sun.tools.javac.Main.compile(toStringArray(fillArguments(new ArrayList JavaDoc())), pw) == 0;
59     } else {
60         sun.tools.javac.Main compiler = new sun.tools.javac.Main(err, "javac");
61         result = compiler.compile(toStringArray(fillArguments(new ArrayList JavaDoc())));
62     }
63     this.errors = new ByteArrayInputStream JavaDoc(err.toByteArray());
64     return result;
65   }
66
67   /**
68    * Parse the compiler error stream to produce a list of
69    * <code>CompilerError</code>s
70    *
71    * @param input The error stream
72    * @return The list of compiler error messages
73    * @exception IOException If an error occurs during message collection
74    */

75   protected List JavaDoc parseStream(BufferedReader JavaDoc input) throws IOException JavaDoc {
76     List JavaDoc errors = new ArrayList JavaDoc();
77     String JavaDoc line = null;
78     StringBuffer JavaDoc buffer = null;
79
80     while (true) {
81       // cleanup the buffer
82
buffer = new StringBuffer JavaDoc(); // this is quicker than clearing it
83

84       // most errors terminate with the '^' char
85
do {
86         if ((line = input.readLine()) == null) {
87             if (buffer.length() > 0) {
88                 // There's an error which doesn't end with a '^'
89
errors.add(new CompilerError("\n" + buffer.toString()));
90             }
91             return errors;
92         }
93         buffer.append(line);
94         buffer.append('\n');
95       } while (!line.endsWith("^"));
96
97       // add the error bean
98
errors.add(parseModernError(buffer.toString()));
99     }
100   }
101
102   /**
103    * Parse an individual compiler error message with modern style.
104    *
105    * @param error The error text
106    * @return A messaged <code>CompilerError</code>
107    */

108   private CompilerError parseModernError(String JavaDoc error) {
109     StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(error, ":");
110     try {
111       String JavaDoc file = tokens.nextToken();
112       if (file.length() == 1) file = new StringBuffer JavaDoc(file).append(":").append(tokens.nextToken()).toString();
113       int line = Integer.parseInt(tokens.nextToken());
114
115       String JavaDoc message = tokens.nextToken("\n").substring(1);
116       String JavaDoc context = tokens.nextToken("\n");
117       String JavaDoc pointer = tokens.nextToken("\n");
118       int startcolumn = pointer.indexOf("^");
119       int endcolumn = context.indexOf(" ", startcolumn);
120       if (endcolumn == -1) {
121           endcolumn = context.length();
122       }
123       return new CompilerError(file, false, line, startcolumn, line, endcolumn, message);
124     } catch(NoSuchElementException JavaDoc nse) {
125       return new CompilerError("no more tokens - could not parse error message: " + error);
126     } catch(Exception JavaDoc nse) {
127       return new CompilerError("could not parse error message: " + error);
128     }
129   }
130
131   public String JavaDoc toString() {
132     return "Sun Javac Compiler";
133   }
134 }
135
Popular Tags