001: // Copyright (c) 1999 Per M.A. Bothner.
002: // This is free software; for terms and warranty disclaimer see ./COPYING.
003:
004: package gnu.text;
005:
006: /** A collection of (zero or more) SourceErrors. */
007:
008: public class SourceMessages {
009: // Number of errors (not counting warnings). A value of 1000 is "fatal".
010: private int errorCount = 0;
011:
012: SourceError firstError;
013: SourceError lastError;
014:
015: public SourceError getErrors() {
016: return firstError;
017: }
018:
019: /** Return true iff errors (not warnings) have been seen. */
020: public boolean seenErrors() {
021: return errorCount > 0;
022: }
023:
024: /** Get the number of errors (not counting warnings). */
025: public int getErrorCount() {
026: return errorCount;
027: }
028:
029: public void clearErrors() {
030: errorCount = 0;
031: }
032:
033: public void clear() {
034: firstError = lastError = null;
035: errorCount = 0;
036: }
037:
038: // The last SourceError with a *differnt* filename than prev has.
039: SourceError lastPrevFilename = null;
040:
041: public void error(SourceError error) {
042: if (error.severity == 'f')
043: errorCount = 1000;
044: else if (error.severity != 'w')
045: errorCount++;
046:
047: // Insert the next error so that line numbers are increasing.
048: if (lastError != null && lastError.filename != null
049: && !lastError.filename.equals(error.filename))
050: lastPrevFilename = lastError;
051: SourceError prev = lastPrevFilename;
052: for (;;) {
053: SourceError next;
054: if (prev == null)
055: next = firstError;
056: else
057: next = prev.next;
058: if (next == null)
059: break;
060: if (error.line != 0 && next.line != 0) {
061: if (error.line < next.line)
062: break;
063: if (error.line == next.line && error.column != 0
064: && next.column != 0) {
065: if (error.column < next.column)
066: break;
067: }
068: }
069: prev = next;
070: }
071: if (prev == null) {
072: error.next = firstError;
073: firstError = error;
074: } else {
075: error.next = prev.next;
076: prev.next = error;
077: }
078: if (prev == lastError)
079: lastError = error;
080: }
081:
082: public void error(char severity, String filename, int line,
083: int column, String message) {
084: error(new SourceError(severity, filename, line, column, message));
085: }
086:
087: public void printAll(java.io.PrintStream out, int max) {
088: for (SourceError err = firstError; err != null && --max >= 0; err = err.next) {
089: out.println(err);
090: }
091: }
092:
093: public void printAll(java.io.PrintWriter out, int max) {
094: for (SourceError err = firstError; err != null && --max >= 0; err = err.next) {
095: out.println(err);
096: }
097: }
098:
099: public String toString(int max) {
100: if (firstError == null)
101: return null;
102: StringBuffer buffer = new StringBuffer();
103: for (SourceError err = firstError; err != null && --max >= 0; err = err.next) {
104: buffer.append(err);
105: buffer.append('\n');
106: }
107: return buffer.toString();
108: }
109:
110: /** Returns true if an error was seen. Prints and clears the messages.
111: * @param out where to write the error message to
112: * @param max maximum number of messages to print (can be 0) */
113: public boolean checkErrors(java.io.PrintWriter out, int max) {
114: if (firstError != null) {
115: printAll(out, max);
116: firstError = lastError = null;
117: int saveCount = errorCount;
118: errorCount = 0;
119: return saveCount > 0;
120: }
121: return false;
122: }
123:
124: /** Returns true if an error was seen. Prints and clears the messages
125: * @param out where to write the error message to
126: * @param max maximum number of messages to print (can be 0) */
127: public boolean checkErrors(java.io.PrintStream out, int max) {
128: if (firstError != null) {
129: printAll(out, max);
130: firstError = lastError = null;
131: int saveCount = errorCount;
132: errorCount = 0;
133: return saveCount > 0;
134: }
135: return false;
136: }
137:
138: }
|