KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > xpointer > parser > TokenMgrError


1 /*
2  * Copyright 1999-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 package org.apache.cocoon.components.xpointer.parser;
17
18 /**
19  * @version CVS $Id: TokenMgrError.java 123904 2005-01-02 21:40:57Z antonio $
20  *
21  */

22 public class TokenMgrError extends Error JavaDoc {
23     /*
24      * Ordinals for various reasons why an Error of this type can be thrown.
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 manager.
34      */

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

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

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

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

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

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

147     public String JavaDoc getMessage() {
148         return super.getMessage();
149     }
150
151     /*
152      * Constructors of various flavors follow.
153      */

154
155     public TokenMgrError() {
156     }
157
158     public TokenMgrError(String JavaDoc message, int reason) {
159         super(message);
160         errorCode = reason;
161     }
162
163     public TokenMgrError(
164         boolean EOFSeen,
165         int lexState,
166         int errorLine,
167         int errorColumn,
168         String JavaDoc errorAfter,
169         char curChar,
170         int reason) {
171         this(
172             lexicalError(
173                 EOFSeen,
174                 lexState,
175                 errorLine,
176                 errorColumn,
177                 errorAfter,
178                 curChar),
179             reason);
180     }
181 }
182
Popular Tags