1
/* Generated By:JavaCC: Do not edit this line. ParseException.java Version 3.0 */
2
/****************************************************************
3
* Licensed to the Apache Software Foundation (ASF) under one *
4
* or more contributor license agreements. See the NOTICE file *
5
* distributed with this work for additional information *
6
* regarding copyright ownership. The ASF licenses this file *
7
* to you under the Apache License, Version 2.0 (the *
8
* "License"); you may not use this file except in compliance *
9
* with the License. You may obtain a copy of the License at *
11
* http://www.apache.org/licenses/LICENSE-2.0 *
13
* Unless required by applicable law or agreed to in writing, *
14
* software distributed under the License is distributed on an *
15
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
16
* KIND, either express or implied. See the License for the *
17
* specific language governing permissions and limitations *
18
* under the License. *
19
****************************************************************/
20
package org.apache.james.mime4j.field.contenttype.parser;
23
* This exception is thrown when parse errors are encountered.
24
* You can explicitly create objects of this exception type by
25
* calling the method generateParseException in the generated
29
* extends org.apache.james.mime4j.field.ParseException
30
* added serialVersionUID
31
* added constructor ParseException(Throwable)
32
* default detail message is "Cannot parse field"
34
public class ParseException extends org.apache.james.mime4j.field.ParseException {
36
private static final long serialVersionUID = 1L;
39
* This constructor is used by the method "generateParseException"
40
* in the generated parser. Calling this constructor generates
41
* a new object of this type with the fields "currentToken",
42
* "expectedTokenSequences", and "tokenImage" set. The boolean
43
* flag "specialConstructor" is also set to true to indicate that
44
* this constructor was used to create this object.
45
* This constructor calls its super class with the empty string
46
* to force the "toString" method of parent class "Throwable" to
47
* print the error message in the form:
48
* ParseException: <result of getMessage>
50
public ParseException(Token currentTokenVal,
51
int[][] expectedTokenSequencesVal,
52
String[] tokenImageVal
56
specialConstructor = true;
57
currentToken = currentTokenVal;
58
expectedTokenSequences = expectedTokenSequencesVal;
59
tokenImage = tokenImageVal;
63
* The following constructors are for use by you for whatever
64
* purpose you can think of. Constructing the exception in this
65
* manner makes the exception behave in the normal way - i.e., as
66
* documented in the class "Throwable". The fields "errorToken",
67
* "expectedTokenSequences", and "tokenImage" do not contain
68
* relevant information. The JavaCC generated code does not use
72
public ParseException() {
73
super("Cannot parse field");
74
specialConstructor = false;
77
public ParseException(Throwable cause) {
79
specialConstructor = false;
82
public ParseException(String message) {
84
specialConstructor = false;
88
* This variable determines which constructor was used to create
89
* this object and thereby affects the semantics of the
90
* "getMessage" method (see below).
92
protected boolean specialConstructor;
95
* This is the last token that has been consumed successfully. If
96
* this object has been created due to a parse error, the token
97
* followng this token will (therefore) be the first error token.
99
public Token currentToken;
102
* Each entry in this array is an array of integers. Each array
103
* of integers represents a sequence of tokens (by their ordinal
104
* values) that is expected at this point of the parse.
106
public int[][] expectedTokenSequences;
109
* This is a reference to the "tokenImage" array of the generated
110
* parser within which the parse error occurred. This array is
111
* defined in the generated ...Constants interface.
113
public String[] tokenImage;
116
* This method has the standard behavior when this object has been
117
* created using the standard constructors. Otherwise, it uses
118
* "currentToken" and "expectedTokenSequences" to generate a parse
119
* error message and returns it. If this object has been created
120
* due to a parse error, and you do not catch it (it gets thrown
121
* from the parser), then this method is called during the printing
122
* of the final stack trace, and hence the correct error message
125
public String getMessage() {
126
if (!specialConstructor) {
127
return super.getMessage();
129
StringBuffer expected = new StringBuffer();
131
for (int i = 0; i < expectedTokenSequences.length; i++) {
132
if (maxSize < expectedTokenSequences[i].length) {
133
maxSize = expectedTokenSequences[i].length;
135
for (int j = 0; j < expectedTokenSequences[i].length; j++) {
136
expected.append(tokenImage[expectedTokenSequences[i][j]]).append(" ");
138
if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
139
expected.append("...");
141
expected.append(eol).append(" ");
143
String retval = "Encountered \"";
144
Token tok = currentToken.next;
145
for (int i = 0; i < maxSize; i++) {
146
if (i != 0) retval += " ";
148
retval += tokenImage[0];
151
retval += add_escapes(tok.image);
154
retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn;
156
if (expectedTokenSequences.length == 1) {
157
retval += "Was expecting:" + eol + " ";
159
retval += "Was expecting one of:" + eol + " ";
161
retval += expected.toString();
166
* The end of line string for this machine.
168
protected String eol = System.getProperty("line.separator", "\n");
171
* Used to convert raw characters to their escaped version
172
* when these raw version cannot be used as part of an ASCII
175
protected String add_escapes(String str) {
176
StringBuffer retval = new StringBuffer();
178
for (int i = 0; i < str.length(); i++) {
179
switch (str.charAt(i))
184
retval.append("\\b");
187
retval.append("\\t");
190
retval.append("\\n");
193
retval.append("\\f");
196
retval.append("\\r");
199
retval.append("\\\"");
202
retval.append("\\\'");
205
retval.append("\\\\");
208
if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
209
String s = "0000" + Integer.toString(ch, 16);
210
retval.append("\\u" + s.substring(s.length() - 4, s.length()));
217
return retval.toString();