01: // Copyright (c) 2003 Per M.A. Bothner.
02: // This is free software; for terms and warranty disclaimer see ./COPYING.
03:
04: package gnu.text;
05:
06: /** Used to signal a non-recoverable (fatal) syntax error.
07: * Can also be used to signal a syntax error from embedded parsers,
08: * such as an <code>eval</code> or </code>execute</code> method,
09: * or well-formedness errors when reading an XML document.
10: */
11:
12: public class SyntaxException extends Exception {
13: /** If non-null, an extra one-line message printed before the messages.
14: * Can be used to provide extra context. */
15: String header;
16:
17: /** One or more error messages that triggered this exception. */
18: SourceMessages messages;
19:
20: public SyntaxException(SourceMessages messages) {
21: this .messages = messages;
22: }
23:
24: public SyntaxException(String header, SourceMessages messages) {
25: this .header = header;
26: this .messages = messages;
27: }
28:
29: /** If non-null, an extra one-line message printed before the messages.
30: * Can be used to provide extra context. */
31: public final String getHeader() {
32: return header;
33: }
34:
35: /** Set the header returned by <code>getHeader</code>. */
36: public final void setHeader(String header) {
37: this .header = header;
38: }
39:
40: public SourceMessages getMessages() {
41: return messages;
42: }
43:
44: public void printAll(java.io.PrintWriter out, int max) {
45: if (header != null)
46: out.println(header);
47: messages.printAll(out, max);
48: }
49:
50: public void clear() {
51: messages.clear();
52: }
53:
54: public int maxToPrint = 10;
55:
56: public String getMessage() {
57: StringBuffer buffer = new StringBuffer();
58: if (header != null)
59: buffer.append(header);
60: int max = maxToPrint;
61: for (SourceError err = messages.firstError; err != null
62: && --max >= 0; err = err.next) {
63: buffer.append('\n');
64: buffer.append(err);
65: }
66: return buffer.toString();
67: }
68: }
|