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