KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > spi > conf > util > TokenMgrError


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  */

22
23 package org.enhydra.spi.conf.util;
24
25 public class TokenMgrError extends Error JavaDoc
26 {
27    /*
28     * Ordinals for various reasons why an Error of this type can be thrown.
29     */

30
31    /**
32     * Lexical error occured.
33     */

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

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

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

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

55    int errorCode;
56
57    /**
58     * Replaces unprintable characters by their espaced (or unicode escaped)
59     * equivalents in the given string.
60     * @param str The given string.
61     * @return String with replaced unprintable characters with their espaced
62     * (or unicode escaped) equivalents.
63     */

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

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

141    public String JavaDoc getMessage() {
142       return super.getMessage();
143    }
144
145    /*
146     * Constructors of various flavors follow.
147     */

148
149    public TokenMgrError() {
150    }
151
152    public TokenMgrError(String JavaDoc message, int reason) {
153       super(message);
154       errorCode = reason;
155    }
156
157    public TokenMgrError(boolean EOFSeen, int lexState, int errorLine, int errorColumn, String JavaDoc errorAfter, char curChar, int reason) {
158       this(LexicalError(EOFSeen, lexState, errorLine, errorColumn, errorAfter, curChar), reason);
159    }
160 }
161
Popular Tags