001: /*
002: * CompilationError.java
003: *
004: * Copyright (C) 2003 Peter Graves
005: * $Id: CompilationError.java,v 1.2 2003/06/05 17:26:07 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: public final class CompilationError {
025: private final Line errorLine;
026: private final String fileName;
027: private final int lineNumber;
028: private final int offset;
029: private final String message;
030:
031: private CompilationError(Line errorLine, String fileName,
032: int lineNumber, int offset, String message) {
033: this .errorLine = errorLine;
034: this .fileName = fileName;
035: this .lineNumber = lineNumber;
036: this .offset = offset;
037: this .message = message;
038: }
039:
040: public Line getErrorLine() {
041: return errorLine;
042: }
043:
044: public String getFileName() {
045: return fileName;
046: }
047:
048: public int getLineNumber() {
049: return lineNumber;
050: }
051:
052: public int getOffset() {
053: return offset;
054: }
055:
056: public String getMessage() {
057: return message;
058: }
059:
060: public static CompilationError parseLineAsErrorMessage(
061: final Line line) {
062: String text = line.trim();
063: if (text.startsWith("[javac]")) {
064: // Ant.
065: text = text.substring(7).trim();
066: }
067: String lookFor = ") : error ";
068: int index = text.indexOf(lookFor);
069: if (index < 0) {
070: lookFor = ") : warning ";
071: index = text.indexOf(lookFor);
072: }
073: if (index >= 0) {
074: // Microsoft C/C++.
075: int end = text.indexOf('(');
076: if (end >= 0) {
077: String fileName = text.substring(0, end);
078: String s = text.substring(end + 1, index);
079: int lineNumber = 0;
080: try {
081: lineNumber = Integer.parseInt(s);
082: } catch (NumberFormatException e) {
083: return null;
084: }
085: if (lineNumber > 0) {
086: // We have a winner. Look for error message on same line.
087: String remainder = text.substring(index
088: + lookFor.length());
089: String message;
090: if ((index = remainder.indexOf(": ")) >= 0)
091: message = remainder.substring(index + 2).trim();
092: else
093: message = remainder.trim();
094: if (message.length() == 0)
095: message = null;
096: return new CompilationError(line, fileName,
097: lineNumber, -1, message);
098: }
099: }
100: return null;
101: }
102: index = text.indexOf(':');
103: if (Platform.isPlatformWindows() && index == 1) {
104: // The file name starts with a drive specifier ("C:").
105: // We want the next ':', not this one.
106: index = text.indexOf(':', 2);
107: }
108: if (index >= 0) {
109: String fileName = text.substring(0, index).trim();
110: String remainder = text.substring(index + 1);
111: index = remainder.indexOf(':');
112: if (index >= 0) {
113: String s = remainder.substring(0, index);
114: int lineNumber = 0;
115: try {
116: lineNumber = Integer.parseInt(s);
117: } catch (NumberFormatException e) {
118: return null;
119: }
120: if (lineNumber > 0) {
121: // We have a winner. Maybe there's a column number too...
122: int offset = -1;
123: remainder = remainder.substring(index + 1);
124: index = remainder.indexOf(':');
125: if (index >= 0) {
126: // Found a colon.
127: s = remainder.substring(0, index);
128: try {
129: offset = Integer.parseInt(s) - 1;
130: } catch (NumberFormatException e) {
131: // No column number.
132: }
133: }
134: // Look for error message on same line.
135: String message;
136: if ((index = remainder.indexOf(": ")) >= 0)
137: message = remainder.substring(index + 2).trim();
138: else
139: message = remainder.trim();
140: if (message.length() == 0)
141: message = null;
142: return new CompilationError(line, fileName,
143: lineNumber, offset, message);
144: }
145: }
146: }
147: return null;
148: }
149: }
|