001/* Generated By:JavaCC: Do not edit this line. ParseException.java Version 3.0 */ 002/**************************************************************** 003 * Licensed to the Apache Software Foundation (ASF) under one * 004 * or more contributor license agreements. See the NOTICE file * 005 * distributed with this work for additional information * 006 * regarding copyright ownership. The ASF licenses this file * 007 * to you under the Apache License, Version 2.0 (the * 008 * "License"); you may not use this file except in compliance * 009 * with the License. You may obtain a copy of the License at * 010 * * 011 * http://www.apache.org/licenses/LICENSE-2.0 * 012 * * 013 * Unless required by applicable law or agreed to in writing, * 014 * software distributed under the License is distributed on an * 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * 016 * KIND, either express or implied. See the License for the * 017 * specific language governing permissions and limitations * 018 * under the License. * 019 ****************************************************************/ 020package org.apache.james.mime4j.field.address; 021 022/** 023 * This exception is thrown when parse errors are encountered. 024 * You can explicitly create objects of this exception type by 025 * calling the method generateParseException in the generated 026 * parser. 027 * 028 * Changes for Mime4J: 029 * extends org.apache.james.mime4j.field.ParseException 030 * added serialVersionUID 031 * added constructor ParseException(Throwable) 032 * default detail message is "Cannot parse field" 033 */ 034public class ParseException extends org.apache.james.mime4j.dom.field.ParseException { 035 036 private static final long serialVersionUID = 1L; 037 038 /** 039 * This constructor is used by the method "generateParseException" 040 * in the generated parser. Calling this constructor generates 041 * a new object of this type with the fields "currentToken", 042 * "expectedTokenSequences", and "tokenImage" set. The boolean 043 * flag "specialConstructor" is also set to true to indicate that 044 * this constructor was used to create this object. 045 * This constructor calls its super class with the empty string 046 * to force the "toString" method of parent class "Throwable" to 047 * print the error message in the form: 048 * ParseException: <result of getMessage> 049 */ 050 public ParseException(Token currentTokenVal, 051 int[][] expectedTokenSequencesVal, 052 String[] tokenImageVal 053 ) 054 { 055 super(""); 056 specialConstructor = true; 057 currentToken = currentTokenVal; 058 expectedTokenSequences = expectedTokenSequencesVal; 059 tokenImage = tokenImageVal; 060 } 061 062 /** 063 * The following constructors are for use by you for whatever 064 * purpose you can think of. Constructing the exception in this 065 * manner makes the exception behave in the normal way - i.e., as 066 * documented in the class "Throwable". The fields "errorToken", 067 * "expectedTokenSequences", and "tokenImage" do not contain 068 * relevant information. The JavaCC generated code does not use 069 * these constructors. 070 */ 071 072 public ParseException() { 073 super("Cannot parse field"); 074 specialConstructor = false; 075 } 076 077 public ParseException(Throwable cause) { 078 super(cause); 079 specialConstructor = false; 080 } 081 082 public ParseException(String message) { 083 super(message); 084 specialConstructor = false; 085 } 086 087 /** 088 * This variable determines which constructor was used to create 089 * this object and thereby affects the semantics of the 090 * "getMessage" method (see below). 091 */ 092 protected boolean specialConstructor; 093 094 /** 095 * This is the last token that has been consumed successfully. If 096 * this object has been created due to a parse error, the token 097 * followng this token will (therefore) be the first error token. 098 */ 099 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}