KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > CompilationError


1 /*
2  * CompilationError.java
3  *
4  * Copyright (C) 2003 Peter Graves
5  * $Id: CompilationError.java,v 1.2 2003/06/05 17:26:07 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 public final class CompilationError
25 {
26     private final Line errorLine;
27     private final String JavaDoc fileName;
28     private final int lineNumber;
29     private final int offset;
30     private final String JavaDoc message;
31
32     private CompilationError(Line errorLine, String JavaDoc fileName, int lineNumber,
33         int offset, String JavaDoc message)
34     {
35         this.errorLine = errorLine;
36         this.fileName = fileName;
37         this.lineNumber = lineNumber;
38         this.offset = offset;
39         this.message = message;
40     }
41
42     public Line getErrorLine()
43     {
44         return errorLine;
45     }
46
47     public String JavaDoc getFileName()
48     {
49         return fileName;
50     }
51
52     public int getLineNumber()
53     {
54         return lineNumber;
55     }
56
57     public int getOffset()
58     {
59         return offset;
60     }
61
62     public String JavaDoc getMessage()
63     {
64         return message;
65     }
66
67     public static CompilationError parseLineAsErrorMessage(final Line line)
68     {
69         String JavaDoc text = line.trim();
70         if (text.startsWith("[javac]")) {
71             // Ant.
72
text = text.substring(7).trim();
73         }
74         String JavaDoc lookFor = ") : error ";
75         int index = text.indexOf(lookFor);
76         if (index < 0) {
77             lookFor = ") : warning ";
78             index = text.indexOf(lookFor);
79         }
80         if (index >= 0) {
81             // Microsoft C/C++.
82
int end = text.indexOf('(');
83             if (end >= 0) {
84                 String JavaDoc fileName = text.substring(0, end);
85                 String JavaDoc s = text.substring(end + 1, index);
86                 int lineNumber = 0;
87                 try {
88                     lineNumber = Integer.parseInt(s);
89                 }
90                 catch (NumberFormatException JavaDoc e) {
91                     return null;
92                 }
93                 if (lineNumber > 0) {
94                     // We have a winner. Look for error message on same line.
95
String JavaDoc remainder = text.substring(index + lookFor.length());
96                     String JavaDoc message;
97                     if ((index = remainder.indexOf(": ")) >= 0)
98                         message = remainder.substring(index + 2).trim();
99                     else
100                         message = remainder.trim();
101                     if (message.length() == 0)
102                         message = null;
103                     return new CompilationError(line, fileName, lineNumber, -1,
104                         message);
105                 }
106             }
107             return null;
108         }
109         index = text.indexOf(':');
110         if (Platform.isPlatformWindows() && index == 1) {
111             // The file name starts with a drive specifier ("C:").
112
// We want the next ':', not this one.
113
index = text.indexOf(':', 2);
114         }
115         if (index >= 0) {
116             String JavaDoc fileName = text.substring(0, index).trim();
117             String JavaDoc remainder = text.substring(index + 1);
118             index = remainder.indexOf(':');
119             if (index >= 0) {
120                 String JavaDoc s = remainder.substring(0, index);
121                 int lineNumber = 0;
122                 try {
123                     lineNumber = Integer.parseInt(s);
124                 }
125                 catch (NumberFormatException JavaDoc e) {
126                     return null;
127                 }
128                 if (lineNumber > 0) {
129                     // We have a winner. Maybe there's a column number too...
130
int offset = -1;
131                     remainder = remainder.substring(index + 1);
132                     index = remainder.indexOf(':');
133                     if (index >= 0) {
134                         // Found a colon.
135
s = remainder.substring(0, index);
136                         try {
137                             offset = Integer.parseInt(s) - 1;
138                         }
139                         catch (NumberFormatException JavaDoc e) {
140                             // No column number.
141
}
142                     }
143                     // Look for error message on same line.
144
String JavaDoc message;
145                     if ((index = remainder.indexOf(": ")) >= 0)
146                         message = remainder.substring(index + 2).trim();
147                     else
148                         message = remainder.trim();
149                     if (message.length() == 0)
150                         message = null;
151                     return new CompilationError(line, fileName, lineNumber,
152                         offset, message);
153                 }
154             }
155         }
156         return null;
157     }
158 }
159
Popular Tags