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.util.CharCursor;
033: import com.caucho.util.StringCharCursor;
034: import com.caucho.vfs.ByteToChar;
035:
036: import java.io.IOException;
037: import java.io.InputStream;
038: import java.io.UnsupportedEncodingException;
039:
040: /**
041: * Reads javac error messages and parses them into a usable format.
042: */
043: class JavacErrorParser extends ErrorParser {
044: private CharBuffer _token = new CharBuffer();
045: private CharBuffer _buf = new CharBuffer();
046: private ByteToChar _lineBuf = ByteToChar.create();
047:
048: public JavacErrorParser() throws UnsupportedEncodingException {
049: this (null);
050: }
051:
052: public JavacErrorParser(String encoding)
053: throws UnsupportedEncodingException {
054: if (encoding == null)
055: encoding = System.getProperty("file.encoding");
056:
057: _lineBuf.setEncoding(encoding);
058: }
059:
060: String parseErrors(InputStream is, LineMap lineMap)
061: throws IOException {
062: CharBuffer result = new CharBuffer();
063:
064: int ch = is.read();
065: for (; ch >= 0; ch = is.read()) {
066: _lineBuf.clear();
067:
068: for (; ch > 0 && ch != '\n'; ch = is.read())
069: _lineBuf.addByte((byte) ch);
070: _lineBuf.addChar('\n');
071:
072: String lineString = _lineBuf.getConvertedString();
073:
074: StringCharCursor cursor = new StringCharCursor(lineString);
075:
076: String line = parseLine(cursor, lineMap);
077: if (line == null)
078: result.append(lineString);
079: else
080: result.append(line);
081:
082: }
083:
084: return result.toString();
085: }
086:
087: /**
088: * Scans errors.
089: *
090: * <p>Javac errors look like "filename:line: message"
091: */
092: String parseLine(CharCursor is, LineMap lineMap) throws IOException {
093: int ch = is.read();
094:
095: _buf.clear();
096:
097: String filename = null;
098: int line = 0;
099:
100: // Take 3: match /.*:\d+:/
101: _token.clear();
102:
103: line: for (; ch != is.DONE; ch = is.read()) {
104: while (ch == ':') {
105: line = 0;
106: for (ch = is.read(); ch >= '0' && ch <= '9'; ch = is
107: .read())
108: line = 10 * line + ch - '0';
109: if (ch == ':' && line > 0) {
110: filename = _token.toString();
111: break line;
112: } else {
113: _token.append(':');
114: if (line > 0)
115: _token.append(line);
116: }
117: }
118:
119: if (ch != is.DONE)
120: _token.append((char) ch);
121: }
122:
123: if (filename == null)
124: return null;
125:
126: int column = 0;
127:
128: // skip added junk like jikes extra "emacs" style columns
129: for (; ch != is.DONE && ch != ' '; ch = is.read()) {
130: }
131:
132: for (; ch == ' '; ch = is.read()) {
133: }
134:
135: // now gather the message
136: _buf.clear();
137: for (; ch != is.DONE; ch = is.read())
138: _buf.append((char) ch);
139:
140: String message = _buf.toString();
141:
142: if (lineMap != null)
143: return lineMap.convertError(filename, line, 0, message);
144: else
145: return filename + ":" + line + ": " + message;
146: }
147: }
|