KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > java > JikesErrorParser


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.java;
30
31 import com.caucho.util.CharBuffer;
32 import com.caucho.vfs.ByteToChar;
33
34 import java.io.IOException JavaDoc;
35 import java.io.InputStream JavaDoc;
36
37 /**
38  * Reads jikes error messages and parses them into a usable format.
39  */

40 class JikesErrorParser extends ErrorParser {
41   private ByteToChar token = ByteToChar.create();
42   private ByteToChar buf = ByteToChar.create();
43   private String JavaDoc filename = "filename";
44
45   /**
46    * Parse all the errors into an array.
47    *
48    * @return null if no errors found.
49    */

50   String JavaDoc parseErrors(InputStream JavaDoc is, LineMap lineMap)
51     throws IOException JavaDoc
52   {
53     CharBuffer errors = new CharBuffer();
54
55     scanError(is, lineMap, errors);
56
57     return errors.toString();
58   }
59
60   /**
61    * Scans a single error.
62    *
63    * <p>Javac errors look like "filename:line: message"
64    */

65   private void scanError(InputStream JavaDoc is, LineMap lineMap, CharBuffer errors)
66     throws IOException JavaDoc
67   {
68     int ch = is.read();
69     ByteToChar sourceLine = ByteToChar.create();
70     sourceLine.setEncoding(System.getProperty("file.encoding"));
71     filename = "filename";
72
73     buf.clear();
74     while (ch >= 0) {
75       int colOffset = 0;
76       for (; ch >= 0 && (ch == ' ' || ch == '\t'); ch = is.read())
77     colOffset++;
78
79       int line = 0;
80       for (; ch >= 0 && ch >= '0' && ch <= '9'; ch = is.read()) {
81     line = 10 * line + ch - '0';
82     colOffset++;
83       }
84
85       if (ch < 0)
86     return;
87       else if (colOffset == 0 && ! Character.isWhitespace((char) ch)) {
88     ch = scanUnknownLine(is, ch, errors);
89       }
90       else if (ch != '.') {
91     ch = skipToNewline(is, ch);
92     continue;
93       }
94
95       sourceLine.clear();
96       for (ch = is.read(); ch >= 0 && ch != '\n'; ch = is.read())
97     sourceLine.addByte(ch);
98       sourceLine.addChar('\n');
99
100       int column = 0;
101       for (ch = is.read(); ch >= 0 && ch != '\n'; ch = is.read()) {
102     if (ch == '^')
103       break;
104     else if (ch == ' ')
105       column++;
106     else if (ch == '\t')
107       column = ((column + 8) / 8) * 8;
108       }
109       for (int i = colOffset + 1; i < column; i++)
110     sourceLine.addChar(' ');
111       sourceLine.addChar('^');
112       sourceLine.addChar('\n');
113
114       ch = skipToNewline(is, ch);
115       if (ch != '*')
116     continue;
117
118       buf.clear();
119       for (; ch >= 0 && ch != ':' && ch != '\n'; ch = is.read()) {
120       }
121
122       if (ch != ':') {
123     ch = skipToNewline(is, ch);
124     continue;
125       }
126
127       for (ch = is.read();
128        ch >= 0 && (ch == ' ' || ch == ' ');
129        ch = is.read()) {
130       }
131
132       for (; ch >= 0 && ch != '\n'; ch = is.read())
133     buf.addByte(ch);
134
135       String JavaDoc message = buf.getConvertedString();
136
137       if (lineMap != null)
138     errors.append(lineMap.convertError(filename, line, 0, message));
139       else
140     errors.append(filename + ":" + line + ": " + message);
141       errors.append('\n');
142       errors.append(sourceLine.getConvertedString());
143     }
144   }
145
146   private int scanUnknownLine(InputStream JavaDoc is, int ch, CharBuffer errors)
147     throws IOException JavaDoc
148   {
149     token.clear();
150     token.addByte(ch);
151     for (ch = is.read();
152      ch > 0 && ! Character.isWhitespace((char) ch);
153      ch = is.read()) {
154     }
155
156     if (token.equals("Found")) {
157       return scanFilename(is, ch);
158     }
159     else if (token.equals("***")) {
160       for (; ch == ' ' || ch == '\t'; ch = is.read()) {
161       }
162       token.clear();
163       for (; ch > 0 && ch != '\n' && ch != ':'; ch = is.read()) {
164     token.addByte(ch);
165       }
166       
167       if (token.equals("Warning"))
168     return skipToNewline(is, ch);
169
170       token.clear();
171       for (ch = is.read(); ch > 0 && ch != '\n'; ch = is.read())
172     token.addByte(ch);
173       token.addChar('\n');
174
175       errors.append(token.getConvertedString());
176
177       return is.read();
178     }
179     else
180       return skipToNewline(is, ch);
181   }
182
183   private int scanFilename(InputStream JavaDoc is, int ch)
184     throws IOException JavaDoc
185   {
186     for (; ch >= 0 && ch != '\n' && ch != '"'; ch = is.read()) {
187     }
188
189     if (ch != '"')
190       return skipToNewline(is, ch);
191
192     token.clear();
193     for (ch = is.read();
194      ch >= 0 && ch != '"' && ch != '\n';
195      ch = is.read()) {
196       token.addByte(ch);
197     }
198
199     if (ch != '"')
200       return skipToNewline(is, ch);
201
202     filename = token.getConvertedString();
203
204     return skipToNewline(is, ch);
205   }
206
207   private int skipToNewline(InputStream JavaDoc is, int ch)
208     throws IOException JavaDoc
209   {
210     for (; ch >= 0 && ch != '\n'; ch = is.read()) {
211     }
212
213     return is.read();
214   }
215 }
216
Popular Tags