KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > JikesOutputParser


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

18 package org.apache.tools.ant.taskdefs;
19
20 import java.io.BufferedReader JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.InputStreamReader JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import org.apache.tools.ant.Project;
26 import org.apache.tools.ant.Task;
27
28 /**
29  * Parses output from jikes and
30  * passes errors and warnings
31  * into the right logging channels of Project.
32  *
33  * <p><strong>As of Ant 1.2, this class is considered to be dead code
34  * by the Ant developers and is unmaintained. Don't use
35  * it.</strong></p>
36  *
37  * @deprecated since 1.2.
38  * Use Jikes' exit value to detect compilation failure.
39  */

40 public class JikesOutputParser implements ExecuteStreamHandler {
41     // CheckStyle:VisibilityModifier OFF - bc
42
protected Task task;
43     protected boolean errorFlag = false; // no errors so far
44
protected int errors;
45     protected int warnings;
46     protected boolean error = false;
47     protected boolean emacsMode;
48
49     protected BufferedReader JavaDoc br;
50     // CheckStyle:VisibilityModifier ON
51

52     /**
53      * Ignore.
54      * @param os ignored
55      */

56     public void setProcessInputStream(OutputStream JavaDoc os) {
57     }
58
59     /**
60      * Ignore.
61      * @param is ignored
62      */

63     public void setProcessErrorStream(InputStream JavaDoc is) {
64     }
65
66     /**
67      * Set the inputstream
68      * @param is the input stream
69      * @throws IOException on error
70      */

71     public void setProcessOutputStream(InputStream JavaDoc is) throws IOException JavaDoc {
72         br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is));
73     }
74
75     /**
76      * Invokes parseOutput.
77      * @throws IOException on error
78      */

79     public void start() throws IOException JavaDoc {
80         parseOutput(br);
81     }
82
83     /**
84      * Ignore.
85      */

86     public void stop() {
87     }
88
89     /**
90      * Construct a new Parser object
91      * @param task task in which context we are called
92      * @param emacsMode if true output in emacs mode
93      */

94     protected JikesOutputParser(Task task, boolean emacsMode) {
95         super();
96
97         System.err.println("As of Ant 1.2 released in October 2000, the "
98             + "JikesOutputParser class");
99         System.err.println("is considered to be dead code by the Ant "
100             + "developers and is unmaintained.");
101         System.err.println("Don\'t use it!");
102
103         this.task = task;
104         this.emacsMode = emacsMode;
105     }
106
107     /**
108      * Parse the output of a jikes compiler
109      * @param reader - Reader used to read jikes's output
110      * @throws IOException on error
111      */

112     protected void parseOutput(BufferedReader JavaDoc reader) throws IOException JavaDoc {
113        if (emacsMode) {
114            parseEmacsOutput(reader);
115        } else {
116            parseStandardOutput(reader);
117        }
118     }
119
120     private void parseStandardOutput(BufferedReader JavaDoc reader) throws IOException JavaDoc {
121         String JavaDoc line;
122         String JavaDoc lower;
123         // We assume, that every output, jikes does, stands for an error/warning
124
// XXX
125
// Is this correct?
126

127         // TODO:
128
// A warning line, that shows code, which contains a variable
129
// error will cause some trouble. The parser should definitely
130
// be much better.
131

132         while ((line = reader.readLine()) != null) {
133             lower = line.toLowerCase();
134             if (line.trim().equals("")) {
135                 continue;
136             }
137             if (lower.indexOf("error") != -1) {
138                 setError(true);
139             } else if (lower.indexOf("warning") != -1) {
140                 setError(false);
141                    } else {
142                 // If we don't know the type of the line
143
// and we are in emacs mode, it will be
144
// an error, because in this mode, jikes won't
145
// always print "error", but sometimes other
146
// keywords like "Syntax". We should look for
147
// all those keywords.
148
if (emacsMode) {
149                     setError(true);
150                 }
151             }
152             log(line);
153         }
154     }
155
156     private void parseEmacsOutput(BufferedReader JavaDoc reader) throws IOException JavaDoc {
157        // This may change, if we add advanced parsing capabilities.
158
parseStandardOutput(reader);
159     }
160
161     private void setError(boolean err) {
162         error = err;
163         if (error) {
164             errorFlag = true;
165         }
166     }
167
168     private void log(String JavaDoc line) {
169        if (!emacsMode) {
170            task.log("", (error ? Project.MSG_ERR : Project.MSG_WARN));
171        }
172        task.log(line, (error ? Project.MSG_ERR : Project.MSG_WARN));
173     }
174
175     /**
176      * Indicate if there were errors during the compile
177      * @return if errors occurred
178      */

179     protected boolean getErrorFlag() {
180         return errorFlag;
181     }
182 }
183
Popular Tags