KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > lucene > html > 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  */

17
18 /* $Id: TokenMgrError.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.lucene.html;
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     /** Lexical error occured. */
28     static final int LEXICAL_ERROR = 0;
29
30     /** An attempt wass made to create a second instance of a static token manager. */
31     static final int STATIC_LEXER_ERROR = 1;
32
33     /** Tried to change to an invalid lexical state. */
34     static final int INVALID_LEXICAL_STATE = 2;
35
36     /** Detected (and bailed out of) an infinite loop in the token manager. */
37     static final int LOOP_DETECTED = 3;
38
39     /** Indicates the reason why the exception is thrown. It will have one of the above 4 values. */
40     int errorCode;
41
42     /*
43      * Constructors of various flavors follow.
44      */

45     public TokenMgrError() {
46     }
47
48     /**
49      * Creates a new TokenMgrError object.
50      *
51      * @param message DOCUMENT ME!
52      * @param reason DOCUMENT ME!
53      */

54     public TokenMgrError(String JavaDoc message, int reason) {
55         super(message);
56         errorCode = reason;
57     }
58
59     /**
60      * Creates a new TokenMgrError object.
61      *
62      * @param EOFSeen DOCUMENT ME!
63      * @param lexState DOCUMENT ME!
64      * @param errorLine DOCUMENT ME!
65      * @param errorColumn DOCUMENT ME!
66      * @param errorAfter DOCUMENT ME!
67      * @param curChar DOCUMENT ME!
68      * @param reason DOCUMENT ME!
69      */

70     public TokenMgrError(boolean EOFSeen, int lexState, int errorLine, int errorColumn,
71         String JavaDoc errorAfter, char curChar, int reason) {
72         this(LexicalError(EOFSeen, lexState, errorLine, errorColumn, errorAfter, curChar), reason);
73     }
74
75     /**
76      * Replaces unprintable characters by their espaced (or unicode escaped) equivalents in the
77      * given string
78      *
79      * @param str DOCUMENT ME!
80      *
81      * @return DOCUMENT ME!
82      */

83     protected static final String JavaDoc addEscapes(String JavaDoc str) {
84         StringBuffer JavaDoc retval = new StringBuffer JavaDoc();
85         char ch;
86
87         for (int i = 0; i < str.length(); i++) {
88             switch (str.charAt(i)) {
89             case 0:
90
91                 continue;
92
93             case '\b':
94                 retval.append("\\b");
95
96                 continue;
97
98             case '\t':
99                 retval.append("\\t");
100
101                 continue;
102
103             case '\n':
104                 retval.append("\\n");
105
106                 continue;
107
108             case '\f':
109                 retval.append("\\f");
110
111                 continue;
112
113             case '\r':
114                 retval.append("\\r");
115
116                 continue;
117
118             case '\"':
119                 retval.append("\\\"");
120
121                 continue;
122
123             case '\'':
124                 retval.append("\\\'");
125
126                 continue;
127
128             case '\\':
129                 retval.append("\\\\");
130
131                 continue;
132
133             default:
134
135                 if (((ch = str.charAt(i)) < 0x20) || (ch > 0x7e)) {
136                     String JavaDoc s = "0000" + Integer.toString(ch, 16);
137                     retval.append("\\u" + s.substring(s.length() - 4, s.length()));
138                 } else {
139                     retval.append(ch);
140                 }
141
142                 continue;
143             }
144         }
145
146         return retval.toString();
147     }
148
149     /**
150      * Returns a detailed message for the Error when it is thrown by the token manager to indicate
151      * a lexical error. Parameters : EOFSeen : indicates if EOF caused the lexicl error
152      * curLexState : lexical state in which this error occured errorLine : line number when the
153      * error occured errorColumn : column number when the error occured errorAfter : prefix that
154      * was seen before this error occured curchar : the offending character Note: You can
155      * customize the lexical error message by modifying this method.
156      *
157      * @param EOFSeen DOCUMENT ME!
158      * @param lexState DOCUMENT ME!
159      * @param errorLine DOCUMENT ME!
160      * @param errorColumn DOCUMENT ME!
161      * @param errorAfter DOCUMENT ME!
162      * @param curChar DOCUMENT ME!
163      *
164      * @return DOCUMENT ME!
165      */

166     private static final String JavaDoc LexicalError(boolean EOFSeen, int lexState, int errorLine,
167         int errorColumn, String JavaDoc errorAfter, char curChar) {
168         return ("Lexical error at line " + errorLine + ", column " + errorColumn +
169         ". Encountered: " +
170         (EOFSeen ? "<EOF> "
171                  : (("\"" + addEscapes(String.valueOf(curChar)) + "\"") + " (" + (int) curChar +
172         "), ")) + "after : \"" + addEscapes(errorAfter) + "\"");
173     }
174
175     /**
176      * You can also modify the body of this method to customize your error messages. For example,
177      * cases like LOOP_DETECTED and INVALID_LEXICAL_STATE are not of end-users concern, so you can
178      * return something like : "Internal Error : Please file a bug report .... " from this method
179      * for such cases in the release version of your parser.
180      *
181      * @return DOCUMENT ME!
182      */

183     public String JavaDoc getMessage() {
184         return super.getMessage();
185     }
186 }
187
Popular Tags