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: */
018: package org.apache.tools.ant.taskdefs;
019:
020: import java.io.BufferedReader;
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.io.InputStreamReader;
024: import java.io.OutputStream;
025: import org.apache.tools.ant.Project;
026: import org.apache.tools.ant.Task;
027:
028: /**
029: * Parses output from jikes and
030: * passes errors and warnings
031: * into the right logging channels of Project.
032: *
033: * <p><strong>As of Ant 1.2, this class is considered to be dead code
034: * by the Ant developers and is unmaintained. Don't use
035: * it.</strong></p>
036: *
037: * @deprecated since 1.2.
038: * Use Jikes' exit value to detect compilation failure.
039: */
040: public class JikesOutputParser implements ExecuteStreamHandler {
041: // CheckStyle:VisibilityModifier OFF - bc
042: protected Task task;
043: protected boolean errorFlag = false; // no errors so far
044: protected int errors;
045: protected int warnings;
046: protected boolean error = false;
047: protected boolean emacsMode;
048:
049: protected BufferedReader br;
050:
051: // CheckStyle:VisibilityModifier ON
052:
053: /**
054: * Ignore.
055: * @param os ignored
056: */
057: public void setProcessInputStream(OutputStream os) {
058: }
059:
060: /**
061: * Ignore.
062: * @param is ignored
063: */
064: public void setProcessErrorStream(InputStream is) {
065: }
066:
067: /**
068: * Set the inputstream
069: * @param is the input stream
070: * @throws IOException on error
071: */
072: public void setProcessOutputStream(InputStream is)
073: throws IOException {
074: br = new BufferedReader(new InputStreamReader(is));
075: }
076:
077: /**
078: * Invokes parseOutput.
079: * @throws IOException on error
080: */
081: public void start() throws IOException {
082: parseOutput(br);
083: }
084:
085: /**
086: * Ignore.
087: */
088: public void stop() {
089: }
090:
091: /**
092: * Construct a new Parser object
093: * @param task task in which context we are called
094: * @param emacsMode if true output in emacs mode
095: */
096: protected JikesOutputParser(Task task, boolean emacsMode) {
097: super ();
098:
099: System.err
100: .println("As of Ant 1.2 released in October 2000, the "
101: + "JikesOutputParser class");
102: System.err.println("is considered to be dead code by the Ant "
103: + "developers and is unmaintained.");
104: System.err.println("Don\'t use it!");
105:
106: this .task = task;
107: this .emacsMode = emacsMode;
108: }
109:
110: /**
111: * Parse the output of a jikes compiler
112: * @param reader - Reader used to read jikes's output
113: * @throws IOException on error
114: */
115: protected void parseOutput(BufferedReader reader)
116: throws IOException {
117: if (emacsMode) {
118: parseEmacsOutput(reader);
119: } else {
120: parseStandardOutput(reader);
121: }
122: }
123:
124: private void parseStandardOutput(BufferedReader reader)
125: throws IOException {
126: String line;
127: String lower;
128: // We assume, that every output, jikes does, stands for an error/warning
129: // XXX
130: // Is this correct?
131:
132: // TODO:
133: // A warning line, that shows code, which contains a variable
134: // error will cause some trouble. The parser should definitely
135: // be much better.
136:
137: while ((line = reader.readLine()) != null) {
138: lower = line.toLowerCase();
139: if (line.trim().equals("")) {
140: continue;
141: }
142: if (lower.indexOf("error") != -1) {
143: setError(true);
144: } else if (lower.indexOf("warning") != -1) {
145: setError(false);
146: } else {
147: // If we don't know the type of the line
148: // and we are in emacs mode, it will be
149: // an error, because in this mode, jikes won't
150: // always print "error", but sometimes other
151: // keywords like "Syntax". We should look for
152: // all those keywords.
153: if (emacsMode) {
154: setError(true);
155: }
156: }
157: log(line);
158: }
159: }
160:
161: private void parseEmacsOutput(BufferedReader reader)
162: throws IOException {
163: // This may change, if we add advanced parsing capabilities.
164: parseStandardOutput(reader);
165: }
166:
167: private void setError(boolean err) {
168: error = err;
169: if (error) {
170: errorFlag = true;
171: }
172: }
173:
174: private void log(String line) {
175: if (!emacsMode) {
176: task.log("", (error ? Project.MSG_ERR : Project.MSG_WARN));
177: }
178: task.log(line, (error ? Project.MSG_ERR : Project.MSG_WARN));
179: }
180:
181: /**
182: * Indicate if there were errors during the compile
183: * @return if errors occurred
184: */
185: protected boolean getErrorFlag() {
186: return errorFlag;
187: }
188: }
|