KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.CharCursor;
33 import com.caucho.util.StringCharCursor;
34 import com.caucho.vfs.ByteToChar;
35
36 import java.io.IOException JavaDoc;
37 import java.io.InputStream JavaDoc;
38 import java.io.UnsupportedEncodingException JavaDoc;
39
40 /**
41  * Reads javac error messages and parses them into a usable format.
42  */

43 class JavacErrorParser extends ErrorParser {
44   private CharBuffer _token = new CharBuffer();
45   private CharBuffer _buf = new CharBuffer();
46   private ByteToChar _lineBuf = ByteToChar.create();
47
48   public JavacErrorParser()
49     throws UnsupportedEncodingException JavaDoc
50   {
51     this(null);
52   }
53
54   public JavacErrorParser(String JavaDoc encoding)
55     throws UnsupportedEncodingException JavaDoc
56   {
57     if (encoding == null)
58       encoding = System.getProperty("file.encoding");
59   
60     _lineBuf.setEncoding(encoding);
61   }
62
63   String JavaDoc parseErrors(InputStream JavaDoc is, LineMap lineMap)
64     throws IOException JavaDoc
65   {
66     CharBuffer result = new CharBuffer();
67
68     int ch = is.read();
69     for (; ch >= 0; ch = is.read()) {
70       _lineBuf.clear();
71
72       for (; ch > 0 && ch != '\n'; ch = is.read())
73     _lineBuf.addByte((byte) ch);
74       _lineBuf.addChar('\n');
75
76       String JavaDoc lineString = _lineBuf.getConvertedString();
77
78       StringCharCursor cursor = new StringCharCursor(lineString);
79
80       String JavaDoc line = parseLine(cursor, lineMap);
81       if (line == null)
82     result.append(lineString);
83       else
84     result.append(line);
85
86     }
87
88     return result.toString();
89   }
90
91   /**
92    * Scans errors.
93    *
94    * <p>Javac errors look like "filename:line: message"
95    */

96   String JavaDoc parseLine(CharCursor is, LineMap lineMap)
97     throws IOException JavaDoc
98   {
99     int ch = is.read();
100
101     _buf.clear();
102
103     String JavaDoc filename = null;
104     int line = 0;
105
106     // Take 3: match /.*:\d+:/
107
_token.clear();
108
109   line:
110     for (; ch != is.DONE; ch = is.read()) {
111       while (ch == ':') {
112     line = 0;
113     for (ch = is.read(); ch >= '0' && ch <= '9'; ch = is.read())
114       line = 10 * line + ch - '0';
115     if (ch == ':' && line > 0) {
116       filename = _token.toString();
117       break line;
118     }
119     else {
120       _token.append(':');
121       if (line > 0)
122         _token.append(line);
123     }
124       }
125
126       if (ch != is.DONE)
127     _token.append((char) ch);
128     }
129
130     if (filename == null)
131       return null;
132
133     int column = 0;
134
135     // skip added junk like jikes extra "emacs" style columns
136
for (; ch != is.DONE && ch != ' '; ch = is.read()) {
137     }
138     
139     for (; ch == ' '; ch = is.read()) {
140     }
141
142     // now gather the message
143
_buf.clear();
144     for (; ch != is.DONE; ch = is.read())
145       _buf.append((char) ch);
146
147     String JavaDoc message = _buf.toString();
148
149     if (lineMap != null)
150       return lineMap.convertError(filename, line, 0, message);
151     else
152       return filename + ":" + line + ": " + message;
153   }
154 }
155
Popular Tags