KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > text > SourceMessages


1 // Copyright (c) 1999, 2005 Per M.A. Bothner.
2
// This is free software; for terms and warranty disclaimer see ./COPYING.
3

4 package gnu.text;
5
6 /** A collection of (zero or more) SourceErrors.
7  * Has a "current line number" which clients can use as the default line
8  * number, or clients can explicitly provide a line number.
9  * Does not handle localization of messages.
10  */

11
12 public class SourceMessages implements SourceLocator
13 {
14   // Number of errors (not counting warnings). A value of 1000 is "fatal".
15
private int errorCount = 0;
16
17   /** The first error or warning in a linked list. */
18   SourceError firstError;
19   /** The last error or warning in a linked list. */
20   SourceError lastError;
21
22   public SourceError getErrors() { return firstError; }
23
24   SourceLocator locator;
25
26   String JavaDoc current_filename;
27   int current_line;
28   int current_column;
29
30   /** If true, print out stack trace with any warning. */
31   public static boolean debugStackTraceOnWarning = false;
32
33   /** If true, print out stack trace with any error. */
34   public static boolean debugStackTraceOnError = false;
35
36
37   /** Return true iff errors (not warnings) have been seen. */
38   public boolean seenErrors() { return errorCount > 0; }
39
40   public boolean seenErrorsOrWarnings() { return firstError != null; }
41
42   /** Get the number of errors (not counting warnings). */
43   public int getErrorCount() { return errorCount; }
44
45   /** Clear the error count (only). */
46   public void clearErrors() { errorCount = 0; }
47
48   /** Clear the contained errors and warnings. */
49   public void clear()
50   {
51     firstError = lastError = null;
52     errorCount = 0;
53   }
54
55   // The last SourceError with a *differnt* filename than prev has.
56
SourceError lastPrevFilename = null;
57
58   /** True if we should sort messages by line number. */
59   public boolean sortMessages;
60
61   /** Link in an error. */
62   public void error(SourceError error)
63   {
64     if (error.severity == 'f')
65       errorCount = 1000;
66     else if (error.severity != 'w')
67       errorCount++;
68     if ((SourceMessages.debugStackTraceOnError
69          && (error.severity == 'e' || error.severity == 'f'))
70         || SourceMessages.debugStackTraceOnWarning && error.severity == 'w')
71       {
72         error.fakeException = new Throwable JavaDoc();
73       }
74
75     // Insert the next error so that line numbers are increasing.
76
if (lastError != null && lastError.filename != null
77     && ! lastError.filename.equals(error.filename))
78       lastPrevFilename = lastError;
79     SourceError prev = lastPrevFilename;
80     if (! sortMessages || error.severity == 'f')
81       prev = lastError;
82     else
83       {
84         for (;;)
85           {
86             SourceError next;
87             if (prev == null)
88               next = firstError;
89             else
90               next = prev.next;
91             if (next == null)
92               break;
93             if (error.line != 0 && next.line != 0)
94               {
95                 if (error.line < next.line)
96                   break;
97                 if (error.line == next.line
98                     && error.column != 0 && next.column != 0)
99                   {
100                     if (error.column < next .column)
101                       break;
102                   }
103               }
104             prev = next;
105           }
106       }
107     if (prev == null)
108       {
109     error.next = firstError;
110     firstError = error;
111       }
112     else
113       {
114     error.next = prev.next;
115     prev.next = error;
116       }
117     if (prev == lastError)
118       lastError = error;
119   }
120
121   /** Record a new error.
122    * @param severity is the seriousness of the error
123    * - one of 'w' (for warning), 'e' (for error), or 'f' (for fatal error)
124    * @param filename the name or URL of the file containing the error
125    * @param line the (1-origin) line number or 0 if unknown
126    * @param column the (1-origin) column number or 0 if unknown
127    * @param message the error message
128    */

129   public void error(char severity, String JavaDoc filename, int line, int column,
130             String JavaDoc message)
131   {
132     error(new SourceError(severity, filename, line, column, message));
133   }
134
135   public void error(char severity, SourceLocator location, String JavaDoc message)
136   {
137     error(new SourceError(severity, location, message));
138   }
139
140   public void error(char severity, String JavaDoc filename, int line, int column,
141             String JavaDoc message, String JavaDoc code)
142   {
143     SourceError err = new SourceError(severity, filename, line, column,
144                                       message);
145     err.code = code;
146     error(err);
147   }
148
149   public void error(char severity, SourceLocator location,
150             String JavaDoc message, String JavaDoc code)
151   {
152     SourceError err = new SourceError(severity, location, message);
153     err.code = code;
154     error(err);
155   }
156
157   /** Record a new error at the current default source file location.
158    * @param severity is the seriousness of the error
159    * - one of 'w' (for warning), 'e' (for error), or 'f' (for fatal error)
160    * @param message the error message
161    */

162   public void error(char severity, String JavaDoc message)
163   {
164     error(new SourceError(severity, current_filename,
165               current_line, current_column, message));
166   }
167
168   public void error(char severity, String JavaDoc message, String JavaDoc code)
169   {
170     SourceError err = new SourceError(severity, current_filename,
171                                       current_line, current_column, message);
172     err.code = code;
173     error(err);
174   }
175
176   /** Print all the error messages to a PrintStream. */
177   public void printAll(java.io.PrintStream JavaDoc out, int max)
178   {
179     for (SourceError err = firstError;
180      err != null && --max >= 0; err = err.next)
181       {
182     err.println(out);
183       }
184   }
185
186   /** Print all the error messages to a PrintWriter. */
187   public void printAll(java.io.PrintWriter JavaDoc out, int max)
188   {
189     for (SourceError err = firstError;
190      err != null && --max >= 0; err = err.next)
191       {
192     err.println(out);
193       }
194   }
195
196   /** Convert this to a String containing the recorded errors.
197    * @param max the maximum number of error error to list
198    * @return a String with one '\n'-terminated line per recorded error
199    */

200   public String JavaDoc toString(int max)
201   {
202     if (firstError == null)
203       return null;
204     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc ();
205     for (SourceError err = firstError;
206      err != null && --max >= 0; err = err.next)
207       {
208     buffer.append(err);
209     buffer.append('\n');
210       }
211     return buffer.toString();
212   }
213
214   /** Checks if an error was seen; if so, prints and clears the messages.
215    * @param out where to write the error message to
216    * @param max maximum number of messages to print (can be 0)
217    */

218   public boolean checkErrors(java.io.PrintWriter JavaDoc out, int max)
219   {
220     if (firstError != null)
221       {
222     printAll(out, max);
223     firstError = lastError = null;
224         int saveCount = errorCount;
225     errorCount = 0;
226     return saveCount > 0;
227       }
228     return false;
229   }
230
231   /** Checks if an error was seen; if so, prints and clears the messages.
232    * @param out where to write the error message to
233    * @param max maximum number of messages to print (can be 0) */

234   public boolean checkErrors(java.io.PrintStream JavaDoc out, int max)
235   {
236     if (firstError != null)
237       {
238     printAll(out, max);
239     firstError = lastError = null;
240         int saveCount = errorCount;
241     errorCount = 0;
242     return saveCount > 0;
243       }
244     return false;
245   }
246
247   /** Links our location to the one give. */
248   public final void setSourceLocator (SourceLocator locator)
249   {
250     this.locator = locator == this ? null : locator;
251   }
252
253   /** Copies the current position of locator. */
254   public final void setLocation (SourceLocator locator)
255   {
256     this.locator = null;
257     current_line = locator.getLineNumber();
258     current_column = locator.getColumnNumber();
259     current_filename = locator.getFileName();
260   }
261
262   public String JavaDoc getPublicId ()
263   {
264     return locator == null ? null : locator.getPublicId();
265   }
266   public String JavaDoc getSystemId ()
267   {
268     return locator == null ? current_filename : locator.getSystemId();
269   }
270
271   public boolean isStableSourceLocation () { return false; }
272
273   /** The default filename to use for a new error. */
274   public final String JavaDoc getFileName() { return current_filename; }
275
276   /** The default line number to use for a new error. */
277   public final int getLineNumber ()
278   {
279     return locator == null ? current_line : locator.getLineNumber();
280   }
281
282   /** The default column number to use for a new error. */
283   public final int getColumnNumber ()
284   {
285     return locator == null ? current_column : locator.getColumnNumber();
286   }
287
288   /** Set the default filename to use for a new error. */
289   public void setFile(String JavaDoc filename) { current_filename = filename; }
290   /** Set the default line number to use for a new error. */
291   public void setLine(int line) { current_line = line; }
292   /** Set the default column number to use for a new error. */
293   public void setColumn(int column) { current_column = column; }
294
295   /** Set the default filename, line and column to use for a new error. */
296   public void setLine(String JavaDoc filename, int line, int column)
297   {
298     current_filename = filename;
299     current_line = line;
300     current_column = column;
301   }
302
303 }
304
Popular Tags