1 16 17 package org.apache.commons.jexl.parser; 18 19 27 public class ParseException extends Exception { 28 29 30 static final long serialVersionUID = 654626477565941968L; 31 32 42 public ParseException(Token currentTokenVal, int[][] expectedTokenSequencesVal, String [] tokenImageVal) { 43 super(""); 44 specialConstructor = true; 45 currentToken = currentTokenVal; 46 expectedTokenSequences = expectedTokenSequencesVal; 47 tokenImage = tokenImageVal; 48 } 49 50 58 59 public ParseException() { 60 super(); 61 specialConstructor = false; 62 } 63 64 public ParseException(String message) { 65 super(message); 66 specialConstructor = false; 67 } 68 69 73 protected boolean specialConstructor; 74 75 80 public Token currentToken; 81 82 87 public int[][] expectedTokenSequences; 88 89 94 public String [] tokenImage; 95 96 105 public String getMessage() { 106 if (!specialConstructor) { 107 return super.getMessage(); 108 } 109 String expected = ""; 110 int maxSize = 0; 111 for (int i = 0; i < expectedTokenSequences.length; i++) { 112 if (maxSize < expectedTokenSequences[i].length) { 113 maxSize = expectedTokenSequences[i].length; 114 } 115 for (int j = 0; j < expectedTokenSequences[i].length; j++) { 116 expected += tokenImage[expectedTokenSequences[i][j]] + " "; 117 } 118 if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) { 119 expected += "..."; 120 } 121 expected += eol + " "; 122 } 123 String retval = "Encountered \""; 124 Token tok = currentToken.next; 125 for (int i = 0; i < maxSize; i++) { 126 if (i != 0) 127 retval += " "; 128 if (tok.kind == 0) { 129 retval += tokenImage[0]; 130 break; 131 } 132 retval += add_escapes(tok.image); 133 tok = tok.next; 134 } 135 retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn; 136 retval += "." + eol; 137 if (expectedTokenSequences.length == 1) { 138 retval += "Was expecting:" + eol + " "; 139 } else { 140 retval += "Was expecting one of:" + eol + " "; 141 } 142 retval += expected; 143 return retval; 144 } 145 146 149 protected String eol = System.getProperty("line.separator", "\n"); 150 151 155 protected String add_escapes(String str) { 156 StringBuffer retval = new StringBuffer (); 157 char ch; 158 for (int i = 0; i < str.length(); i++) { 159 switch (str.charAt(i)) { 160 case 0: 161 continue; 162 case '\b': 163 retval.append("\\b"); 164 continue; 165 case '\t': 166 retval.append("\\t"); 167 continue; 168 case '\n': 169 retval.append("\\n"); 170 continue; 171 case '\f': 172 retval.append("\\f"); 173 continue; 174 case '\r': 175 retval.append("\\r"); 176 continue; 177 case '\"': 178 retval.append("\\\""); 179 continue; 180 case '\'': 181 retval.append("\\\'"); 182 continue; 183 case '\\': 184 retval.append("\\\\"); 185 continue; 186 default: 187 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) { 188 String s = "0000" + Integer.toString(ch, 16); 189 retval.append("\\u").append(s.substring(s.length() - 4, s.length())); 190 } else { 191 retval.append(ch); 192 } 193 continue; 194 } 195 } 196 return retval.toString(); 197 } 198 199 } 200 | Popular Tags |