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