KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > text > SourceError


1 package gnu.text;
2
3 /** Represents an error message from processing a "source" file. */
4
5 public class SourceError
6   implements SourceLocator
7 {
8   /** Used to chain to the "next" message. */
9   public SourceError next;
10
11   /** The seriousness of the error - one of 'w' (for warning),
12    * 'e' (for error), or 'f' (for fatal error). */

13   public char severity;
14
15   /** The name or URL of the file containing the error. */
16   public String JavaDoc filename;
17
18   /** If non-null, an error code, as might be specified by a standard. */
19   public String JavaDoc code;
20
21   /** The line number of the error, with 1 being the top line.
22    * The value 0 means unknown or not applicable (such as the entire file). */

23   /** The (1-origin) location of the error. */
24   public int line;
25
26   /** The column number of the error, with 1 being the left-most column.
27    * The value 0 means unknown or not applicable (such as the entire line). */

28   public int column;
29
30   /** The actual error message.
31    * This is post-localization and -formatting.
32    * It can contain multiple lines, separated by '\n'.*/

33   public String JavaDoc message;
34
35   /** Provides optional stack trace.
36    * Filled when --debug-error-prints-stack-trace or
37    * --debug-warning-prints-stack-trace option is used.*/

38   public Throwable JavaDoc fakeException;
39
40   public SourceError(char severity, String JavaDoc filename, int line, int column,
41              String JavaDoc message)
42   {
43     this.severity = severity;
44     this.filename = filename;
45     this.line = line;
46     this.column = column;
47     this.message = message;
48   }
49
50   public SourceError(char severity, SourceLocator location, String JavaDoc message)
51   {
52     this(severity, location.getFileName(), location.getLineNumber(),
53          location.getColumnNumber(), message);
54   }
55
56   /** Create a new SourceError using the current line/column from
57    * a <code>LineBufferedReader</code>. */

58   public SourceError(LineBufferedReader port, char severity, String JavaDoc message)
59   {
60     this(severity, port.getName(),
61      port.getLineNumber() + 1, port.getColumnNumber(),
62      message);
63     if (column >= 0)
64       column++;
65   }
66
67   /** Convert the error to a String.
68    * The String starts with filename, line and option column,
69    * followed by the message. Warning messages are indicated as such. */

70   public String JavaDoc toString()
71   {
72     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc ();
73     buffer.append (filename == null ? "<unknown>" : filename);
74     if (line > 0 || column > 0)
75       {
76     buffer.append (':');
77     buffer.append (line);
78     if (column > 0)
79       {
80         buffer.append (':');
81         buffer.append (column);
82       }
83       }
84     buffer.append (": ");
85     if (severity == 'w')
86       buffer.append("warning - ");
87     buffer.append (message);
88     if (code != null)
89       {
90         buffer.append(" [");
91         buffer.append(code);
92         buffer.append("]");
93       }
94     if (fakeException != null)
95       {
96         // We assume getStackTrace is evailable if getCause is,
97
// rather than add a new PreProcess parameter.
98
/* #ifdef use:java.lang.Throwable.getCause */
99         StackTraceElement JavaDoc[] stackTrace = fakeException.getStackTrace();
100         for (int i = 0; i < stackTrace.length; i++)
101           {
102             buffer.append("\n");
103             buffer.append(" ");
104             buffer.append(stackTrace[i].toString());
105           }
106         /* #else */
107         // java.io.StringWriter writer = new java.io.StringWriter();
108
// java.io.PrintWriter pwriter = new java.io.PrintWriter(writer);
109
// fakeException.printStackTrace(pwriter);
110
// pwriter.close();
111
// buffer.append("\n");
112
// buffer.append(writer.toString());
113
/* #endif */
114       }
115     return buffer.toString ();
116   }
117
118   public void print(java.io.PrintWriter JavaDoc out)
119   {
120     out.print(this);
121   }
122
123   public void println(java.io.PrintWriter JavaDoc out)
124   {
125     String JavaDoc line = toString();
126     for (;;)
127       {
128         int nl = line.indexOf('\n');
129         if (nl < 0)
130           break;
131         out.println(line.substring(0, nl));
132         line = line.substring(nl+1);
133       }
134     out.println(line);
135   }
136
137   public void println(java.io.PrintStream JavaDoc out)
138   {
139     String JavaDoc line = toString();
140     for (;;)
141       {
142         int nl = line.indexOf('\n');
143         if (nl < 0)
144           break;
145         out.println(line.substring(0, nl));
146         line = line.substring(nl+1);
147       }
148     out.println(line);
149   }
150
151   public int getLineNumber () { return line == 0 ? -1 : line; }
152   public int getColumnNumber () { return column == 0 ? -1 : column; }
153   public String JavaDoc getPublicId() { return null; }
154   public String JavaDoc getSystemId() { return filename; }
155   public String JavaDoc getFileName() { return filename; }
156   public boolean isStableSourceLocation() { return true; }
157 }
158
Popular Tags