1 17 18 19 20 package org.apache.lenya.lucene.html; 21 22 28 public class ParseException extends Exception { 29 33 protected boolean specialConstructor; 34 35 40 public Token currentToken; 41 42 46 public int[][] expectedTokenSequences; 47 48 52 public String [] tokenImage; 53 54 55 protected String eol = System.getProperty("line.separator", "\n"); 56 57 70 public ParseException(Token currentTokenVal, int[][] expectedTokenSequencesVal, 71 String [] tokenImageVal) { 72 super(""); 73 specialConstructor = true; 74 currentToken = currentTokenVal; 75 expectedTokenSequences = expectedTokenSequencesVal; 76 tokenImage = tokenImageVal; 77 } 78 79 86 public ParseException() { 87 super(); 88 specialConstructor = false; 89 } 90 91 96 public ParseException(String message) { 97 super(message); 98 specialConstructor = false; 99 } 100 101 110 public String getMessage() { 111 if (!specialConstructor) { 112 return super.getMessage(); 113 } 114 115 String expected = ""; 116 int maxSize = 0; 117 118 for (int i = 0; i < expectedTokenSequences.length; i++) { 119 if (maxSize < expectedTokenSequences[i].length) { 120 maxSize = expectedTokenSequences[i].length; 121 } 122 123 for (int j = 0; j < expectedTokenSequences[i].length; j++) { 124 expected += (tokenImage[expectedTokenSequences[i][j]] + " "); 125 } 126 127 if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) { 128 expected += "..."; 129 } 130 131 expected += (eol + " "); 132 } 133 134 String retval = "Encountered \""; 135 Token tok = currentToken.next; 136 137 for (int i = 0; i < maxSize; i++) { 138 if (i != 0) { 139 retval += " "; 140 } 141 142 if (tok.kind == 0) { 143 retval += tokenImage[0]; 144 145 break; 146 } 147 148 retval += add_escapes(tok.image); 149 tok = tok.next; 150 } 151 152 retval += ("\" at line " + currentToken.next.beginLine + ", column " + 153 currentToken.next.beginColumn); 154 retval += ("." + eol); 155 156 if (expectedTokenSequences.length == 1) { 157 retval += ("Was expecting:" + eol + " "); 158 } else { 159 retval += ("Was expecting one of:" + eol + " "); 160 } 161 162 retval += expected; 163 164 return retval; 165 } 166 167 175 protected String add_escapes(String str) { 176 StringBuffer retval = new StringBuffer (); 177 char ch; 178 179 for (int i = 0; i < str.length(); i++) { 180 switch (str.charAt(i)) { 181 case 0: 182 183 continue; 184 185 case '\b': 186 retval.append("\\b"); 187 188 continue; 189 190 case '\t': 191 retval.append("\\t"); 192 193 continue; 194 195 case '\n': 196 retval.append("\\n"); 197 198 continue; 199 200 case '\f': 201 retval.append("\\f"); 202 203 continue; 204 205 case '\r': 206 retval.append("\\r"); 207 208 continue; 209 210 case '\"': 211 retval.append("\\\""); 212 213 continue; 214 215 case '\'': 216 retval.append("\\\'"); 217 218 continue; 219 220 case '\\': 221 retval.append("\\\\"); 222 223 continue; 224 225 default: 226 227 if (((ch = str.charAt(i)) < 0x20) || (ch > 0x7e)) { 228 String s = "0000" + Integer.toString(ch, 16); 229 retval.append("\\u" + s.substring(s.length() - 4, s.length())); 230 } else { 231 retval.append(ch); 232 } 233 234 continue; 235 } 236 } 237 238 return retval.toString(); 239 } 240 } 241 | Popular Tags |