| 1 4 package gnu.text; 5 6 11 12 public class SourceMessages implements SourceLocator 13 { 14 private int errorCount = 0; 16 17 18 SourceError firstError; 19 20 SourceError lastError; 21 22 public SourceError getErrors() { return firstError; } 23 24 SourceLocator locator; 25 26 String current_filename; 27 int current_line; 28 int current_column; 29 30 31 public static boolean debugStackTraceOnWarning = false; 32 33 34 public static boolean debugStackTraceOnError = false; 35 36 37 38 public boolean seenErrors() { return errorCount > 0; } 39 40 public boolean seenErrorsOrWarnings() { return firstError != null; } 41 42 43 public int getErrorCount() { return errorCount; } 44 45 46 public void clearErrors() { errorCount = 0; } 47 48 49 public void clear() 50 { 51 firstError = lastError = null; 52 errorCount = 0; 53 } 54 55 SourceError lastPrevFilename = null; 57 58 59 public boolean sortMessages; 60 61 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 (); 73 } 74 75 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 129 public void error(char severity, String filename, int line, int column, 130 String message) 131 { 132 error(new SourceError(severity, filename, line, column, message)); 133 } 134 135 public void error(char severity, SourceLocator location, String message) 136 { 137 error(new SourceError(severity, location, message)); 138 } 139 140 public void error(char severity, String filename, int line, int column, 141 String message, String 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 message, String code) 151 { 152 SourceError err = new SourceError(severity, location, message); 153 err.code = code; 154 error(err); 155 } 156 157 162 public void error(char severity, String message) 163 { 164 error(new SourceError(severity, current_filename, 165 current_line, current_column, message)); 166 } 167 168 public void error(char severity, String message, String 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 177 public void printAll(java.io.PrintStream 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 187 public void printAll(java.io.PrintWriter 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 200 public String toString(int max) 201 { 202 if (firstError == null) 203 return null; 204 StringBuffer buffer = new StringBuffer (); 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 218 public boolean checkErrors(java.io.PrintWriter 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 234 public boolean checkErrors(java.io.PrintStream 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 248 public final void setSourceLocator (SourceLocator locator) 249 { 250 this.locator = locator == this ? null : locator; 251 } 252 253 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 getPublicId () 263 { 264 return locator == null ? null : locator.getPublicId(); 265 } 266 public String getSystemId () 267 { 268 return locator == null ? current_filename : locator.getSystemId(); 269 } 270 271 public boolean isStableSourceLocation () { return false; } 272 273 274 public final String getFileName() { return current_filename; } 275 276 277 public final int getLineNumber () 278 { 279 return locator == null ? current_line : locator.getLineNumber(); 280 } 281 282 283 public final int getColumnNumber () 284 { 285 return locator == null ? current_column : locator.getColumnNumber(); 286 } 287 288 289 public void setFile(String filename) { current_filename = filename; } 290 291 public void setLine(int line) { current_line = line; } 292 293 public void setColumn(int column) { current_column = column; } 294 295 296 public void setLine(String filename, int line, int column) 297 { 298 current_filename = filename; 299 current_line = line; 300 current_column = column; 301 } 302 303 } 304 | Popular Tags |