001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.java;
030:
031: import com.caucho.util.CharBuffer;
032: import com.caucho.vfs.ByteToChar;
033:
034: import java.io.IOException;
035: import java.io.InputStream;
036:
037: /**
038: * Reads jikes error messages and parses them into a usable format.
039: */
040: class JikesErrorParser extends ErrorParser {
041: private ByteToChar token = ByteToChar.create();
042: private ByteToChar buf = ByteToChar.create();
043: private String filename = "filename";
044:
045: /**
046: * Parse all the errors into an array.
047: *
048: * @return null if no errors found.
049: */
050: String parseErrors(InputStream is, LineMap lineMap)
051: throws IOException {
052: CharBuffer errors = new CharBuffer();
053:
054: scanError(is, lineMap, errors);
055:
056: return errors.toString();
057: }
058:
059: /**
060: * Scans a single error.
061: *
062: * <p>Javac errors look like "filename:line: message"
063: */
064: private void scanError(InputStream is, LineMap lineMap,
065: CharBuffer errors) throws IOException {
066: int ch = is.read();
067: ByteToChar sourceLine = ByteToChar.create();
068: sourceLine.setEncoding(System.getProperty("file.encoding"));
069: filename = "filename";
070:
071: buf.clear();
072: while (ch >= 0) {
073: int colOffset = 0;
074: for (; ch >= 0 && (ch == ' ' || ch == '\t'); ch = is.read())
075: colOffset++;
076:
077: int line = 0;
078: for (; ch >= 0 && ch >= '0' && ch <= '9'; ch = is.read()) {
079: line = 10 * line + ch - '0';
080: colOffset++;
081: }
082:
083: if (ch < 0)
084: return;
085: else if (colOffset == 0
086: && !Character.isWhitespace((char) ch)) {
087: ch = scanUnknownLine(is, ch, errors);
088: } else if (ch != '.') {
089: ch = skipToNewline(is, ch);
090: continue;
091: }
092:
093: sourceLine.clear();
094: for (ch = is.read(); ch >= 0 && ch != '\n'; ch = is.read())
095: sourceLine.addByte(ch);
096: sourceLine.addChar('\n');
097:
098: int column = 0;
099: for (ch = is.read(); ch >= 0 && ch != '\n'; ch = is.read()) {
100: if (ch == '^')
101: break;
102: else if (ch == ' ')
103: column++;
104: else if (ch == '\t')
105: column = ((column + 8) / 8) * 8;
106: }
107: for (int i = colOffset + 1; i < column; i++)
108: sourceLine.addChar(' ');
109: sourceLine.addChar('^');
110: sourceLine.addChar('\n');
111:
112: ch = skipToNewline(is, ch);
113: if (ch != '*')
114: continue;
115:
116: buf.clear();
117: for (; ch >= 0 && ch != ':' && ch != '\n'; ch = is.read()) {
118: }
119:
120: if (ch != ':') {
121: ch = skipToNewline(is, ch);
122: continue;
123: }
124:
125: for (ch = is.read(); ch >= 0 && (ch == ' ' || ch == ' '); ch = is
126: .read()) {
127: }
128:
129: for (; ch >= 0 && ch != '\n'; ch = is.read())
130: buf.addByte(ch);
131:
132: String message = buf.getConvertedString();
133:
134: if (lineMap != null)
135: errors.append(lineMap.convertError(filename, line, 0,
136: message));
137: else
138: errors.append(filename + ":" + line + ": " + message);
139: errors.append('\n');
140: errors.append(sourceLine.getConvertedString());
141: }
142: }
143:
144: private int scanUnknownLine(InputStream is, int ch,
145: CharBuffer errors) throws IOException {
146: token.clear();
147: token.addByte(ch);
148: for (ch = is.read(); ch > 0
149: && !Character.isWhitespace((char) ch); ch = is.read()) {
150: }
151:
152: if (token.equals("Found")) {
153: return scanFilename(is, ch);
154: } else if (token.equals("***")) {
155: for (; ch == ' ' || ch == '\t'; ch = is.read()) {
156: }
157: token.clear();
158: for (; ch > 0 && ch != '\n' && ch != ':'; ch = is.read()) {
159: token.addByte(ch);
160: }
161:
162: if (token.equals("Warning"))
163: return skipToNewline(is, ch);
164:
165: token.clear();
166: for (ch = is.read(); ch > 0 && ch != '\n'; ch = is.read())
167: token.addByte(ch);
168: token.addChar('\n');
169:
170: errors.append(token.getConvertedString());
171:
172: return is.read();
173: } else
174: return skipToNewline(is, ch);
175: }
176:
177: private int scanFilename(InputStream is, int ch) throws IOException {
178: for (; ch >= 0 && ch != '\n' && ch != '"'; ch = is.read()) {
179: }
180:
181: if (ch != '"')
182: return skipToNewline(is, ch);
183:
184: token.clear();
185: for (ch = is.read(); ch >= 0 && ch != '"' && ch != '\n'; ch = is
186: .read()) {
187: token.addByte(ch);
188: }
189:
190: if (ch != '"')
191: return skipToNewline(is, ch);
192:
193: filename = token.getConvertedString();
194:
195: return skipToNewline(is, ch);
196: }
197:
198: private int skipToNewline(InputStream is, int ch)
199: throws IOException {
200: for (; ch >= 0 && ch != '\n'; ch = is.read()) {
201: }
202:
203: return is.read();
204: }
205: }
|