View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  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   */
17  /* Generated By:JavaCC: Do not edit this line. TokenMgrError.java Version 3.0 *
18   *
19   * !!!MODIFIED BY DMITRI PLOTNIKOV - DO NOT REGENERATE!!!
20   */
21  
22  package org.apache.commons.jxpath.ri.parser;
23  
24  public class TokenMgrError extends Error {
25      /*
26       * Ordinals for various reasons why an Error of this type can be thrown.
27       */
28  
29      /**
30       *
31       */
32      private static final long serialVersionUID = 1L;
33      /**
34       * Lexical error occurred.
35       */
36      static final int LEXICAL_ERROR = 0;
37      /**
38       * An attempt wass made to create a second instance of a static token manager.
39       */
40      static final int STATIC_LEXER_ERROR = 1;
41      /**
42       * Tried to change to an invalid lexical state.
43       */
44      static final int INVALID_LEXICAL_STATE = 2;
45      /**
46       * Detected (and bailed out of) an infinite loop in the token manager.
47       */
48      static final int LOOP_DETECTED = 3;
49  
50      /**
51       * Replaces unprintable characters by their escaped (or Unicode escaped) equivalents in the given string
52       *
53       * @param str TODO
54       * @return TODO
55       */
56      public static final String addEscapes(final String str) {
57          final StringBuilder retval = new StringBuilder();
58          char ch;
59          for (int i = 0; i < str.length(); i++) {
60              switch (str.charAt(i)) {
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                      final String s = "0000" + Integer.toString(ch, 16);
90                      retval.append("\\u" + s.substring(s.length() - 4));
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 token manager to indicate a lexical error.
102      *
103      * Note: You can customize the lexical error message by modifying this method.
104      *
105      * @param EOFSeen     indicates if EOF caused the lexicl error
106      * @param lexState    lexical state in which this error occurred
107      * @param errorLine   line number when the error occurred
108      * @param errorColumn column number when the error occurred
109      * @param errorAfter  prefix that was seen before this error occurred
110      * @param curChar     the offending character
111      * @return TODO
112      */
113     protected static String LexicalError(final boolean EOFSeen, final int lexState, final int errorLine, final int errorColumn, final String errorAfter,
114             final char curChar) {
115         return "Lexical error at line " + errorLine + ", column " + errorColumn + ".  Encountered: "
116                 + (EOFSeen ? "<EOF> " : "\"" + addEscapes(String.valueOf(curChar)) + "\"" + " (" + (int) curChar + "), ") + "after : \""
117                 + addEscapes(errorAfter) + "\"";
118     }
119 
120     /**
121      * Indicates the reason why the exception is thrown. It will have one of the above 4 values.
122      */
123     int errorCode;
124     private int position;
125     /*
126      * Constructors of various flavors follow.
127      */
128     private char character;
129 
130     public TokenMgrError() {
131     }
132 
133     public TokenMgrError(final boolean EOFSeen, final int lexState, final int errorLine, final int errorColumn, final String errorAfter, final char curChar,
134             final int reason) {
135         this(LexicalError(EOFSeen, lexState, errorLine, errorColumn, errorAfter, curChar), reason);
136         // ADDED BY ME FROM THIS POINT TO THE EOF - DMITRI PLOTNIKOV
137         position = errorColumn - 1;
138         character = curChar;
139     }
140 
141     public TokenMgrError(final String message, final int reason) {
142         super(message);
143         errorCode = reason;
144     }
145 
146     public char getCharacter() {
147         return character;
148     }
149 
150     /**
151      * You can also modify the body of this method to customize your error messages. For example, cases like LOOP_DETECTED and INVALID_LEXICAL_STATE are not of
152      * end-users concern, so you can return something like :
153      *
154      * "Internal Error : Please file a bug report .... "
155      *
156      * from this method for such cases in the release version of your parser.
157      */
158     @Override
159     public String getMessage() {
160         return super.getMessage();
161     }
162 
163     public int getPosition() {
164         return position;
165     }
166 }