1 /**
2 * .
3 * Copyright � 1999 Erich P G.
4 *
5 */
6
7 package creator.compiler;
8
9 import java.util.Vector;
10 import org.xml.sax.ErrorHandler;
11 import org.xml.sax.HandlerBase; // <-- remove??
12 import org.xml.sax.SAXException;
13 import org.xml.sax.SAXParseException;
14
15 /**
16 * Parse Error Handler. It will log and present errors and warnings
17 * for a compilation. It is also the error handler for the XML
18 * parser.
19 *
20 * WARNING!!! An instance of this must be registered with the parser
21 * before EACH compile.
22 *
23 *
24 * @see autohit.Sim
25 *
26 * @author Erich P. Gatejen
27 * @version 1.0
28 * <i>Version History</i>
29 * <code>EPG - Initial - 13Jan99</code>
30 *
31 */
32 public class XmlParseErrorHandler implements ErrorHandler {
33
34 // --- FINAL FIELDS ---------------------------------------------
35
36 // --- FIELDS ------------------------------------------------------------
37
38 /**
39 * A vector containing error strings.
40 */
41 public Vector errors;
42
43 /**
44 * A vector containing warning strings.
45 */
46 public Vector warnings;
47
48 // --- PUBLIC METHODS ----------------------------------------------------
49
50 /**
51 * Constructor.
52 *
53 */
54 public XmlParseErrorHandler() {
55
56 super(); // Call constructor for HandlerBase()
57
58 errors = new Vector();
59 warnings = new Vector();
60 }
61
62 /**
63 * Add an error to the error log.
64 */
65 public void error (String text) {
66 errors.add(text);
67 }
68
69
70 /**
71 * Pretty print the errors and warnings.
72 *
73 * @return a string containing the print.
74 */
75 public String prettyPrint() {
76
77 StringBuffer p = new StringBuffer();
78
79 // do errors
80 p.append("Errors ---------------------------------------\n");
81 for (int eidx = 0; eidx < errors.size(); eidx++) {
82 p.append(errors.get(eidx));
83 p.append("\n");
84 }
85 // do warnings
86 p.append("Warnings ------------------------------------\n");
87 for (int eidx = 0; eidx < warnings.size(); eidx++) {
88 p.append(warnings.get(eidx));
89 p.append("\n");
90 }
91
92 return p.toString();
93 }
94
95
96
97 // --- PRIVATE METHODS ---------------------------------------------------
98
99
100 // --- INTERNAL METHODS ---------------------------------------------------
101
102 /**
103 * Receive an error from the compiler. Do not call this method directly.
104 *
105 * @throws SAXException Send back to document builder
106 */
107 public void error (SAXParseException e) throws SAXException {
108
109 String entry = "ParseError @ line " + e.getLineNumber() + " : " + e.getMessage();
110 errors.add(entry);
111
112 //throw e; // Do I want to can these?
113 }
114
115 /**
116 * Receive a fatal error from the compiler. Do not call this method directly.
117 *
118 * @throws SAXException Send back to document builder
119 */
120 public void fatalError (SAXParseException e) throws SAXException {
121
122 String entry = "FATAL ParseError @ line " + e.getLineNumber() + " : " + e.getMessage();
123 errors.add(entry);
124
125 //throw e; // Do I want to can these?
126 }
127
128
129 /**
130 * Receive a warning from the compiler. Do not call this method directly.
131 *
132 * @throws SAXException Send back to document builder
133 */
134 public void warning (SAXParseException e) throws SAXException {
135
136 // trap a "!DOCTYPE" error and let the user know
137 if (e.getMessage().indexOf("<!DOCTYPE", 0) > 0) {
138 // BIG No
139 errors.add("Does not have a proper <!DOCTYPE>. Add or fix it. It should look something like <!DOCTYPE sim SYSTEM \"file:sim.dtd\">");
140 throw e;
141
142 } else {
143
144 String entry = "ParseWarning @ line " + e.getLineNumber() + " : " + e.getMessage();
145 errors.add(entry);
146 }
147 // throw e; // Keep chugging on a warning.
148 }
149
150 }
|