01: // This file is part of KeY - Integrated Deductive Software Design
02: // Copyright (C) 2001-2007 Universitaet Karlsruhe, Germany
03: // Universitaet Koblenz-Landau, Germany
04: // Chalmers University of Technology, Sweden
05: //
06: // The KeY system is protected by the GNU General Public License.
07: // See LICENSE.TXT for details.
08: //
09: //
10:
11: package de.uka.ilkd.key.parser;
12:
13: /** This class is used to collect errors and warnings that occured during parsing */
14:
15: import java.util.Iterator;
16: import java.util.Stack;
17:
18: public class ErrorHandler {
19:
20: /** Stack collecting errors and warnings*/
21: private Stack errorStack = new Stack();
22:
23: /** amount of errors */
24: private int errors = 0;
25:
26: /** amount of warnings */
27: private int warnings = 0;
28:
29: /** maximal allowed amount of errors */
30: private final int maxErrors = 5;
31:
32: /** create new errorhandler */
33: public ErrorHandler() {
34:
35: }
36:
37: /** collect error
38: * @param ex the Exception thrown by the de.uka.ilkd.prins.parser
39: */
40: public void reportError(Exception ex) {
41: if (errors < maxErrors) { // more errors occured than allowed
42: errors++;
43: errorStack.push(ex);
44: }
45: }
46:
47: /** collect warning
48: * @param ex the Exception thrown by the de.uka.ilkd.prins.parser
49: */
50: public void reportWarning(Exception ex) {
51: warnings++;
52: errorStack.push(ex);
53: }
54:
55: /** errors?
56: * @return boolean true if errors were thrown, false otherwise
57: */
58: public boolean error() {
59: return (errors > 0);
60: }
61:
62: /**warnings?
63: * @return boolean true if warnings were thrown, false otherwise
64: */
65: public boolean warning() {
66: return (warnings > 0);
67: }
68:
69: /** print all collected warnings and errors */
70: public void printErrorsAndWarnings() {
71:
72: // print
73: Iterator it = errorStack.iterator();
74: while (it.hasNext()) {
75: Exception ex = (Exception) it.next();
76: if (ex instanceof WarningException) {
77: System.out.println("Warning: " + ex.getMessage());
78: } else
79: System.out.println("Error: " + ex.getMessage());
80: }
81:
82: }
83:
84: }
|