001: package gnu.text;
002:
003: /** Represents an error message from processing a "source" file. */
004:
005: public class SourceError implements SourceLocator {
006: /** Used to chain to the "next" message. */
007: public SourceError next;
008:
009: /** The seriousness of the error - one of 'w' (for warning),
010: * 'e' (for error), or 'f' (for fatal error). */
011: public char severity;
012:
013: /** The name or URL of the file containing the error. */
014: public String filename;
015:
016: /** If non-null, an error code, as might be specified by a standard. */
017: public String code;
018:
019: /** The line number of the error, with 1 being the top line.
020: * The value 0 means unknown or not applicable (such as the entire file). */
021: /** The (1-origin) location of the error. */
022: public int line;
023:
024: /** The column number of the error, with 1 being the left-most column.
025: * The value 0 means unknown or not applicable (such as the entire line). */
026: public int column;
027:
028: /** The actual error message.
029: * This is post-localization and -formatting.
030: * It can contain multiple lines, separated by '\n'.*/
031: public String message;
032:
033: /** Provides optional stack trace.
034: * Filled when --debug-error-prints-stack-trace or
035: * --debug-warning-prints-stack-trace option is used.*/
036: public Throwable fakeException;
037:
038: public SourceError(char severity, String filename, int line,
039: int column, String message) {
040: this .severity = severity;
041: this .filename = filename;
042: this .line = line;
043: this .column = column;
044: this .message = message;
045: }
046:
047: public SourceError(char severity, SourceLocator location,
048: String message) {
049: this (severity, location.getFileName(),
050: location.getLineNumber(), location.getColumnNumber(),
051: message);
052: }
053:
054: /** Create a new SourceError using the current line/column from
055: * a <code>LineBufferedReader</code>. */
056: public SourceError(LineBufferedReader port, char severity,
057: String message) {
058: this (severity, port.getName(), port.getLineNumber() + 1, port
059: .getColumnNumber(), message);
060: if (column >= 0)
061: column++;
062: }
063:
064: /** Convert the error to a String.
065: * The String starts with filename, line and option column,
066: * followed by the message. Warning messages are indicated as such. */
067: public String toString() {
068: StringBuffer buffer = new StringBuffer();
069: buffer.append(filename == null ? "<unknown>" : filename);
070: if (line > 0 || column > 0) {
071: buffer.append(':');
072: buffer.append(line);
073: if (column > 0) {
074: buffer.append(':');
075: buffer.append(column);
076: }
077: }
078: buffer.append(": ");
079: if (severity == 'w')
080: buffer.append("warning - ");
081: buffer.append(message);
082: if (code != null) {
083: buffer.append(" [");
084: buffer.append(code);
085: buffer.append("]");
086: }
087: if (fakeException != null) {
088: // We assume getStackTrace is evailable if getCause is,
089: // rather than add a new PreProcess parameter.
090: /* #ifdef use:java.lang.Throwable.getCause */
091: StackTraceElement[] stackTrace = fakeException
092: .getStackTrace();
093: for (int i = 0; i < stackTrace.length; i++) {
094: buffer.append("\n");
095: buffer.append(" ");
096: buffer.append(stackTrace[i].toString());
097: }
098: /* #else */
099: // java.io.StringWriter writer = new java.io.StringWriter();
100: // java.io.PrintWriter pwriter = new java.io.PrintWriter(writer);
101: // fakeException.printStackTrace(pwriter);
102: // pwriter.close();
103: // buffer.append("\n");
104: // buffer.append(writer.toString());
105: /* #endif */
106: }
107: return buffer.toString();
108: }
109:
110: public void print(java.io.PrintWriter out) {
111: out.print(this );
112: }
113:
114: public void println(java.io.PrintWriter out) {
115: String line = toString();
116: for (;;) {
117: int nl = line.indexOf('\n');
118: if (nl < 0)
119: break;
120: out.println(line.substring(0, nl));
121: line = line.substring(nl + 1);
122: }
123: out.println(line);
124: }
125:
126: public void println(java.io.PrintStream out) {
127: String line = toString();
128: for (;;) {
129: int nl = line.indexOf('\n');
130: if (nl < 0)
131: break;
132: out.println(line.substring(0, nl));
133: line = line.substring(nl + 1);
134: }
135: out.println(line);
136: }
137:
138: public int getLineNumber() {
139: return line == 0 ? -1 : line;
140: }
141:
142: public int getColumnNumber() {
143: return column == 0 ? -1 : column;
144: }
145:
146: public String getPublicId() {
147: return null;
148: }
149:
150: public String getSystemId() {
151: return filename;
152: }
153:
154: public String getFileName() {
155: return filename;
156: }
157:
158: public boolean isStableSourceLocation() {
159: return true;
160: }
161: }
|