1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.seasar.tuigwaa.cms.core.wiki.engine;
18
19 public class TokenMgrError extends Error
20 {
21
22
23
24
25 /***
26 *
27 */
28 private static final long serialVersionUID = -6372620625095654048L;
29
30 /***
31 * Lexical error occured.
32 */
33 static final int LEXICAL_ERROR = 0;
34
35 /***
36 * An attempt wass made to create a second instance of a static token manager.
37 */
38 static final int STATIC_LEXER_ERROR = 1;
39
40 /***
41 * Tried to change to an invalid lexical state.
42 */
43 static final int INVALID_LEXICAL_STATE = 2;
44
45 /***
46 * Detected (and bailed out of) an infinite loop in the token manager.
47 */
48 static final int LOOP_DETECTED = 3;
49
50 /***
51 * Indicates the reason why the exception is thrown. It will have
52 * one of the above 4 values.
53 */
54 int errorCode;
55
56 /***
57 * Replaces unprintable characters by their espaced (or unicode escaped)
58 * equivalents in the given string
59 */
60 protected static final String addEscapes(String str) {
61 StringBuffer retval = new StringBuffer();
62 char ch;
63 for (int i = 0; i < str.length(); i++) {
64 switch (str.charAt(i))
65 {
66 case 0 :
67 continue;
68 case '\b':
69 retval.append("//b");
70 continue;
71 case '\t':
72 retval.append("//t");
73 continue;
74 case '\n':
75 retval.append("//n");
76 continue;
77 case '\f':
78 retval.append("//f");
79 continue;
80 case '\r':
81 retval.append("//r");
82 continue;
83 case '\"':
84 retval.append("//\"");
85 continue;
86 case '\'':
87 retval.append("//\'");
88 continue;
89 case '//':
90 retval.append("////");
91 continue;
92 default:
93 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
94 String s = "0000" + Integer.toString(ch, 16);
95 retval.append("//u" + s.substring(s.length() - 4, s.length()));
96 } else {
97 retval.append(ch);
98 }
99 continue;
100 }
101 }
102 return retval.toString();
103 }
104
105 /***
106 * Returns a detailed message for the Error when it is thrown by the
107 * token manager to indicate a lexical error.
108 * Parameters :
109 * EOFSeen : indicates if EOF caused the lexicl error
110 * curLexState : lexical state in which this error occured
111 * errorLine : line number when the error occured
112 * errorColumn : column number when the error occured
113 * errorAfter : prefix that was seen before this error occured
114 * curchar : the offending character
115 * Note: You can customize the lexical error message by modifying this method.
116 */
117 protected static String LexicalError(boolean EOFSeen, int lexState, int errorLine, int errorColumn, String errorAfter, char curChar) {
118 return("Lexical error at line " +
119 errorLine + ", column " +
120 errorColumn + ". Encountered: " +
121 (EOFSeen ? "<EOF> " : ("\"" + addEscapes(String.valueOf(curChar)) + "\"") + " (" + (int)curChar + "), ") +
122 "after : \"" + addEscapes(errorAfter) + "\"");
123 }
124
125 /***
126 * You can also modify the body of this method to customize your error messages.
127 * For example, cases like LOOP_DETECTED and INVALID_LEXICAL_STATE are not
128 * of end-users concern, so you can return something like :
129 *
130 * "Internal Error : Please file a bug report .... "
131 *
132 * from this method for such cases in the release version of your parser.
133 */
134 public String getMessage() {
135 return super.getMessage();
136 }
137
138
139
140
141
142 public TokenMgrError() {
143 }
144
145 public TokenMgrError(String message, int reason) {
146 super(message);
147 errorCode = reason;
148 }
149
150 public TokenMgrError(boolean EOFSeen, int lexState, int errorLine, int errorColumn, String errorAfter, char curChar, int reason) {
151 this(LexicalError(EOFSeen, lexState, errorLine, errorColumn, errorAfter, curChar), reason);
152 }
153 }