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