1 /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 3.0 */
3 * Copyright 2004 the mime4j project
\r
5 * Licensed under the Apache License, Version 2.0 (the "License");
\r
6 * you may not use this file except in compliance with the License.
\r
7 * You may obtain a copy of the License at
\r
9 * http://www.apache.org/licenses/LICENSE-2.0
\r
11 * Unless required by applicable law or agreed to in writing, software
\r
12 * distributed under the License is distributed on an "AS IS" BASIS,
\r
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
14 * See the License for the specific language governing permissions and
\r
15 * limitations under the License.
\r
17 package org.apache.james.mime4j.field.datetime.parser;
20 * This exception is thrown when parse errors are encountered.
21 * You can explicitly create objects of this exception type by
22 * calling the method generateParseException in the generated
25 * You can modify this class to customize your error reporting
26 * mechanisms so long as you retain the public fields.
28 public class ParseException extends Exception {
31 * This constructor is used by the method "generateParseException"
32 * in the generated parser. Calling this constructor generates
33 * a new object of this type with the fields "currentToken",
34 * "expectedTokenSequences", and "tokenImage" set. The boolean
35 * flag "specialConstructor" is also set to true to indicate that
36 * this constructor was used to create this object.
37 * This constructor calls its super class with the empty string
38 * to force the "toString" method of parent class "Throwable" to
39 * print the error message in the form:
40 * ParseException: <result of getMessage>
42 public ParseException(Token currentTokenVal,
43 int[][] expectedTokenSequencesVal,
44 String[] tokenImageVal
48 specialConstructor = true;
49 currentToken = currentTokenVal;
50 expectedTokenSequences = expectedTokenSequencesVal;
51 tokenImage = tokenImageVal;
55 * The following constructors are for use by you for whatever
56 * purpose you can think of. Constructing the exception in this
57 * manner makes the exception behave in the normal way - i.e., as
58 * documented in the class "Throwable". The fields "errorToken",
59 * "expectedTokenSequences", and "tokenImage" do not contain
60 * relevant information. The JavaCC generated code does not use
64 public ParseException() {
66 specialConstructor = false;
69 public ParseException(String message) {
71 specialConstructor = false;
75 * This variable determines which constructor was used to create
76 * this object and thereby affects the semantics of the
77 * "getMessage" method (see below).
79 protected boolean specialConstructor;
82 * This is the last token that has been consumed successfully. If
83 * this object has been created due to a parse error, the token
84 * followng this token will (therefore) be the first error token.
86 public Token currentToken;
89 * Each entry in this array is an array of integers. Each array
90 * of integers represents a sequence of tokens (by their ordinal
91 * values) that is expected at this point of the parse.
93 public int[][] expectedTokenSequences;
96 * This is a reference to the "tokenImage" array of the generated
97 * parser within which the parse error occurred. This array is
98 * defined in the generated ...Constants interface.
100 public String[] tokenImage;
103 * This method has the standard behavior when this object has been
104 * created using the standard constructors. Otherwise, it uses
105 * "currentToken" and "expectedTokenSequences" to generate a parse
106 * error message and returns it. If this object has been created
107 * due to a parse error, and you do not catch it (it gets thrown
108 * from the parser), then this method is called during the printing
109 * of the final stack trace, and hence the correct error message
112 public String getMessage() {
113 if (!specialConstructor) {
114 return super.getMessage();
116 StringBuffer expected = new StringBuffer();
118 for (int i = 0; i < expectedTokenSequences.length; i++) {
119 if (maxSize < expectedTokenSequences[i].length) {
120 maxSize = expectedTokenSequences[i].length;
122 for (int j = 0; j < expectedTokenSequences[i].length; j++) {
123 expected.append(tokenImage[expectedTokenSequences[i][j]]).append(" ");
125 if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
126 expected.append("...");
128 expected.append(eol).append(" ");
130 String retval = "Encountered \"";
131 Token tok = currentToken.next;
132 for (int i = 0; i < maxSize; i++) {
133 if (i != 0) retval += " ";
135 retval += tokenImage[0];
138 retval += add_escapes(tok.image);
141 retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn;
143 if (expectedTokenSequences.length == 1) {
144 retval += "Was expecting:" + eol + " ";
146 retval += "Was expecting one of:" + eol + " ";
148 retval += expected.toString();
153 * The end of line string for this machine.
155 protected String eol = System.getProperty("line.separator", "\n");
158 * Used to convert raw characters to their escaped version
159 * when these raw version cannot be used as part of an ASCII
162 protected String add_escapes(String str) {
163 StringBuffer retval = new StringBuffer();
165 for (int i = 0; i < str.length(); i++) {
166 switch (str.charAt(i))
171 retval.append("\\b");
174 retval.append("\\t");
177 retval.append("\\n");
180 retval.append("\\f");
183 retval.append("\\r");
186 retval.append("\\\"");
189 retval.append("\\\'");
192 retval.append("\\\\");
195 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
196 String s = "0000" + Integer.toString(ch, 16);
197 retval.append("\\u" + s.substring(s.length() - 4, s.length()));
204 return retval.toString();