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 java.io.BufferedReader;
020: import java.io.ByteArrayInputStream;
021: import java.io.ByteArrayOutputStream;
022: import java.io.IOException;
023: import java.io.PrintWriter;
024: import java.util.ArrayList;
025: import java.util.List;
026: import java.util.NoSuchElementException;
027: import java.util.StringTokenizer;
028:
029: import org.apache.cocoon.components.language.programming.CompilerError;
030: import org.apache.commons.lang.SystemUtils;
031:
032: /**
033: * This class wraps the Sun's Javac Compiler.
034: *
035: * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
036: * @version CVS $Id: Javac.java 433543 2006-08-22 06:22:54Z crossley $
037: * @since 2.0
038: */
039:
040: public class Javac extends AbstractJavaCompiler {
041:
042: public Javac() {
043: // EMPTY
044: }
045:
046: /**
047: * Compile a source file yielding a loadable class file.
048: *
049: * <code>null</code> if it is the platform's default encoding
050: * @exception IOException If an error occurs during compilation
051: */
052: public boolean compile() throws IOException {
053:
054: ByteArrayOutputStream err = new ByteArrayOutputStream();
055:
056: boolean result;
057: if (!SystemUtils.IS_JAVA_1_3) { // For Java 1.4 and 1.5
058: PrintWriter pw = new PrintWriter(err);
059: result = com.sun.tools.javac.Main.compile(
060: toStringArray(fillArguments(new ArrayList())), pw) == 0;
061: } else {
062: sun.tools.javac.Main compiler = new sun.tools.javac.Main(
063: err, "javac");
064: result = compiler
065: .compile(toStringArray(fillArguments(new ArrayList())));
066: }
067: this .errors = new ByteArrayInputStream(err.toByteArray());
068: return result;
069: }
070:
071: /**
072: * Parse the compiler error stream to produce a list of
073: * <code>CompilerError</code>s
074: *
075: * @param input The error stream
076: * @return The list of compiler error messages
077: * @exception IOException If an error occurs during message collection
078: */
079: protected List parseStream(BufferedReader input) throws IOException {
080: List errors = new ArrayList();
081: String line = null;
082: StringBuffer buffer = null;
083:
084: while (true) {
085: // cleanup the buffer
086: buffer = new StringBuffer(); // this is quicker than clearing it
087:
088: // most errors terminate with the '^' char
089: do {
090: if ((line = input.readLine()) == null) {
091: if (buffer.length() > 0) {
092: // There's an error which doesn't end with a '^'
093: errors.add(new CompilerError("\n"
094: + buffer.toString()));
095: }
096: return errors;
097: }
098: buffer.append(line);
099: buffer.append('\n');
100: } while (!line.endsWith("^"));
101:
102: // add the error bean
103: errors.add(parseModernError(buffer.toString()));
104: }
105: }
106:
107: /**
108: * Parse an individual compiler error message with modern style.
109: *
110: * @param error The error text
111: * @return A messaged <code>CompilerError</code>
112: */
113: private CompilerError parseModernError(String error) {
114: StringTokenizer tokens = new StringTokenizer(error, ":");
115: try {
116: String file = tokens.nextToken();
117: if (file.length() == 1)
118: file = new StringBuffer(file).append(":").append(
119: tokens.nextToken()).toString();
120: int line = Integer.parseInt(tokens.nextToken());
121:
122: String message = tokens.nextToken("\n").substring(1);
123: String context = tokens.nextToken("\n");
124: String pointer = tokens.nextToken("\n");
125: int startcolumn = pointer.indexOf("^");
126: int endcolumn = context.indexOf(" ", startcolumn);
127: if (endcolumn == -1) {
128: endcolumn = context.length();
129: }
130: return new CompilerError(file, false, line, startcolumn,
131: line, endcolumn, message);
132: } catch (NoSuchElementException nse) {
133: return new CompilerError(
134: "no more tokens - could not parse error message: "
135: + error);
136: } catch (Exception nse) {
137: return new CompilerError("could not parse error message: "
138: + error);
139: }
140: }
141:
142: public String toString() {
143: return "Sun Javac Compiler";
144: }
145: }
|