KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > script > el > parser > TokenMgrError


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.script.el.parser;
19
20 import org.apache.beehive.netui.util.internal.InternalStringBuilder;
21
22 public class TokenMgrError
23     extends Error JavaDoc {
24
25     /*
26      * Ordinals for various reasons why an Error of this type can be thrown.
27      */

28
29     /**
30      * Lexical error occured.
31      */

32     static final int LEXICAL_ERROR = 0;
33
34     /**
35      * An attempt wass made to create a second instance of a static token manager.
36      */

37     static final int STATIC_LEXER_ERROR = 1;
38
39     /**
40      * Tried to change to an invalid lexical state.
41      */

42     static final int INVALID_LEXICAL_STATE = 2;
43
44     /**
45      * Detected (and bailed out of) an infinite loop in the token manager.
46      */

47     static final int LOOP_DETECTED = 3;
48
49     /**
50      * Indicates the reason why the exception is thrown. It will have
51      * one of the above 4 values.
52      */

53     int errorCode;
54
55     /**
56      * Replaces unprintable characters by their espaced (or unicode escaped)
57      * equivalents in the given string
58      */

59     protected static final String JavaDoc addEscapes(String JavaDoc str) {
60         InternalStringBuilder retval = new InternalStringBuilder();
61         char ch;
62         for(int i = 0; i < str.length(); i++) {
63             switch(str.charAt(i)) {
64                 case 0:
65                     continue;
66                 case '\b':
67                     retval.append("\\b");
68                     continue;
69                 case '\t':
70                     retval.append("\\t");
71                     continue;
72                 case '\n':
73                     retval.append("\\n");
74                     continue;
75                 case '\f':
76                     retval.append("\\f");
77                     continue;
78                 case '\r':
79                     retval.append("\\r");
80                     continue;
81                 case '\"':
82                     retval.append("\\\"");
83                     continue;
84                 case '\'':
85                     retval.append("\\\'");
86                     continue;
87                 case '\\':
88                     retval.append("\\\\");
89                     continue;
90                 default:
91                     if((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
92                         String JavaDoc s = "0000" + Integer.toString(ch, 16);
93                         retval.append("\\u" + s.substring(s.length() - 4, s.length()));
94                     } else {
95                         retval.append(ch);
96                     }
97                     continue;
98             }
99         }
100         return retval.toString();
101     }
102
103     /**
104      * Returns a detailed message for the Error when it is thrown by the
105      * token manager to indicate a lexical error.
106      * Parameters :
107      * EOFSeen : indicates if EOF caused the lexicl error
108      * curLexState : lexical state in which this error occured
109      * errorLine : line number when the error occured
110      * errorColumn : column number when the error occured
111      * errorAfter : prefix that was seen before this error occured
112      * curchar : the offending character
113      * Note: You can customize the lexical error message by modifying this method.
114      */

115     protected static String JavaDoc LexicalError(boolean EOFSeen, int lexState, int errorLine, int errorColumn, String JavaDoc errorAfter, char curChar) {
116         return ("Lexical error at line " +
117             errorLine + ", column " +
118             errorColumn + ". Encountered: " +
119             (EOFSeen ? "<EOF> " : ("\"" + addEscapes(String.valueOf(curChar)) + "\"") + " (" + (int)curChar + "), ") +
120             "after : \"" + addEscapes(errorAfter) + "\"");
121     }
122
123     /**
124      * You can also modify the body of this method to customize your error messages.
125      * For example, cases like LOOP_DETECTED and INVALID_LEXICAL_STATE are not
126      * of end-users concern, so you can return something like :
127      * <p/>
128      * "Internal Error : Please file a bug report .... "
129      * <p/>
130      * from this method for such cases in the release version of your parser.
131      */

132     public String JavaDoc getMessage() {
133         return super.getMessage();
134     }
135
136     /*
137      * Constructors of various flavors follow.
138      */

139
140     public TokenMgrError() {
141     }
142
143     public TokenMgrError(String JavaDoc message, int reason) {
144         super(message);
145         errorCode = reason;
146     }
147
148     public TokenMgrError(boolean EOFSeen, int lexState, int errorLine, int errorColumn, String JavaDoc errorAfter, char curChar, int reason) {
149         this(LexicalError(EOFSeen, lexState, errorLine, errorColumn, errorAfter, curChar), reason);
150     }
151 }
152
Popular Tags