~ubuntu-branches/ubuntu/utopic/apache-mime4j/utopic

« back to all changes in this revision

Viewing changes to src/main/javacc/org/apache/james/mime4j/field/contenttype/ParseException.java

  • Committer: Package Import Robot
  • Author(s): David Paleino
  • Date: 2013-11-03 11:13:27 UTC
  • mfrom: (2.1.2 experimental)
  • Revision ID: package-import@ubuntu.com-20131103111327-09huep00ex05z113
Tags: 0.7.2-2
* Upload to unstable
* Fixed Vcs-* fields in debian/control
* Updated debian/watch
* Standards-Version bump to 3.9.5, no changes needed

Show diffs side-by-side

added added

removed removed

Lines of Context:
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   *
10
 
 *                                                              *
11
 
 *   http://www.apache.org/licenses/LICENSE-2.0                 *
12
 
 *                                                              *
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;
21
 
 
22
 
/**
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
26
 
 * parser.
27
 
 *
28
 
 * Changes for Mime4J:
29
 
 *   extends org.apache.james.mime4j.field.ParseException
30
 
 *   added serialVersionUID
31
 
 *   added constructor ParseException(Throwable)
32
 
 *   default detail message is "Cannot parse field"
33
 
 */
34
 
public class ParseException extends org.apache.james.mime4j.field.ParseException {
35
 
 
36
 
  private static final long serialVersionUID = 1L;
37
 
 
38
 
  /**
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>
49
 
   */
50
 
  public ParseException(Token currentTokenVal,
51
 
                        int[][] expectedTokenSequencesVal,
52
 
                        String[] tokenImageVal
53
 
                       )
54
 
  {
55
 
    super("");
56
 
    specialConstructor = true;
57
 
    currentToken = currentTokenVal;
58
 
    expectedTokenSequences = expectedTokenSequencesVal;
59
 
    tokenImage = tokenImageVal;
60
 
  }
61
 
 
62
 
  /**
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
69
 
   * these constructors.
70
 
   */
71
 
 
72
 
  public ParseException() {
73
 
    super("Cannot parse field");
74
 
    specialConstructor = false;
75
 
  }
76
 
 
77
 
  public ParseException(Throwable cause) {
78
 
    super(cause);
79
 
    specialConstructor = false;
80
 
  }
81
 
 
82
 
  public ParseException(String message) {
83
 
    super(message);
84
 
    specialConstructor = false;
85
 
  }
86
 
 
87
 
  /**
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).
91
 
   */
92
 
  protected boolean specialConstructor;
93
 
 
94
 
  /**
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.
98
 
   */
99
 
  public Token currentToken;
100
 
 
101
 
  /**
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.
105
 
   */
106
 
  public int[][] expectedTokenSequences;
107
 
 
108
 
  /**
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.
112
 
   */
113
 
  public String[] tokenImage;
114
 
 
115
 
  /**
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
123
 
   * gets displayed.
124
 
   */
125
 
  public String getMessage() {
126
 
    if (!specialConstructor) {
127
 
      return super.getMessage();
128
 
    }
129
 
    StringBuffer expected = new StringBuffer();
130
 
    int maxSize = 0;
131
 
    for (int i = 0; i < expectedTokenSequences.length; i++) {
132
 
      if (maxSize < expectedTokenSequences[i].length) {
133
 
        maxSize = expectedTokenSequences[i].length;
134
 
      }
135
 
      for (int j = 0; j < expectedTokenSequences[i].length; j++) {
136
 
        expected.append(tokenImage[expectedTokenSequences[i][j]]).append(" ");
137
 
      }
138
 
      if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
139
 
        expected.append("...");
140
 
      }
141
 
      expected.append(eol).append("    ");
142
 
    }
143
 
    String retval = "Encountered \"";
144
 
    Token tok = currentToken.next;
145
 
    for (int i = 0; i < maxSize; i++) {
146
 
      if (i != 0) retval += " ";
147
 
      if (tok.kind == 0) {
148
 
        retval += tokenImage[0];
149
 
        break;
150
 
      }
151
 
      retval += add_escapes(tok.image);
152
 
      tok = tok.next; 
153
 
    }
154
 
    retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn;
155
 
    retval += "." + eol;
156
 
    if (expectedTokenSequences.length == 1) {
157
 
      retval += "Was expecting:" + eol + "    ";
158
 
    } else {
159
 
      retval += "Was expecting one of:" + eol + "    ";
160
 
    }
161
 
    retval += expected.toString();
162
 
    return retval;
163
 
  }
164
 
 
165
 
  /**
166
 
   * The end of line string for this machine.
167
 
   */
168
 
  protected String eol = System.getProperty("line.separator", "\n");
169
 
 
170
 
  /**
171
 
   * Used to convert raw characters to their escaped version
172
 
   * when these raw version cannot be used as part of an ASCII
173
 
   * string literal.
174
 
   */
175
 
  protected String add_escapes(String str) {
176
 
      StringBuffer retval = new StringBuffer();
177
 
      char ch;
178
 
      for (int i = 0; i < str.length(); i++) {
179
 
        switch (str.charAt(i))
180
 
        {
181
 
           case 0 :
182
 
              continue;
183
 
           case '\b':
184
 
              retval.append("\\b");
185
 
              continue;
186
 
           case '\t':
187
 
              retval.append("\\t");
188
 
              continue;
189
 
           case '\n':
190
 
              retval.append("\\n");
191
 
              continue;
192
 
           case '\f':
193
 
              retval.append("\\f");
194
 
              continue;
195
 
           case '\r':
196
 
              retval.append("\\r");
197
 
              continue;
198
 
           case '\"':
199
 
              retval.append("\\\"");
200
 
              continue;
201
 
           case '\'':
202
 
              retval.append("\\\'");
203
 
              continue;
204
 
           case '\\':
205
 
              retval.append("\\\\");
206
 
              continue;
207
 
           default:
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()));
211
 
              } else {
212
 
                 retval.append(ch);
213
 
              }
214
 
              continue;
215
 
        }
216
 
      }
217
 
      return retval.toString();
218
 
   }
219
 
 
220
 
}