1 16 package org.apache.cocoon.components.xpointer.parser; 17 18 27 public class ParseException extends Exception { 28 29 41 public ParseException( 42 Token currentTokenVal, 43 int[][] expectedTokenSequencesVal, 44 String [] tokenImageVal) { 45 super(""); 46 specialConstructor = true; 47 currentToken = currentTokenVal; 48 expectedTokenSequences = expectedTokenSequencesVal; 49 tokenImage = tokenImageVal; 50 } 51 52 61 62 public ParseException() { 63 super(); 64 specialConstructor = false; 65 } 66 67 public ParseException(String message) { 68 super(message); 69 specialConstructor = false; 70 } 71 72 77 protected boolean specialConstructor; 78 79 84 public Token currentToken; 85 86 91 public int[][] expectedTokenSequences; 92 93 98 public String [] tokenImage; 99 100 110 public String getMessage() { 111 if (!specialConstructor) { 112 return super.getMessage(); 113 } 114 StringBuffer expected = new StringBuffer (); 115 int maxSize = 0; 116 for (int i = 0; i < expectedTokenSequences.length; i++) { 117 if (maxSize < expectedTokenSequences[i].length) { 118 maxSize = expectedTokenSequences[i].length; 119 } 120 for (int j = 0; j < expectedTokenSequences[i].length; j++) { 121 expected.append(tokenImage[expectedTokenSequences[i][j]]); 122 expected.append(" "); 123 } 124 if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] 125 != 0) { 126 expected.append("..."); 127 } 128 expected.append(eol); 129 expected.append(" "); 130 } 131 StringBuffer retval = new StringBuffer ("Encountered \""); 132 Token tok = currentToken.next; 133 for (int i = 0; i < maxSize; i++) { 134 if (i != 0) 135 retval.append(" "); 136 if (tok.kind == 0) { 137 retval.append(tokenImage[0]); 138 break; 139 } 140 retval.append(add_escapes(tok.image)); 141 tok = tok.next; 142 } 143 retval.append("\" at line "); 144 retval.append(currentToken.next.beginLine); 145 retval.append(", column "); 146 retval.append(currentToken.next.beginColumn); 147 retval.append("."); 148 retval.append(eol); 149 retval.append("Was expecting"); 150 if (expectedTokenSequences.length != 1) { 151 retval.append(" one of"); 152 } 153 retval.append(":"); 154 retval.append(eol); 155 retval.append(" "); 156 retval.append(expected); 157 return retval.toString(); 158 } 159 160 163 protected String eol = System.getProperty("line.separator", "\n"); 164 165 170 protected String add_escapes(String str) { 171 StringBuffer retval = new StringBuffer (); 172 char ch; 173 for (int i = 0; i < str.length(); i++) { 174 switch (str.charAt(i)) { 175 case 0 : 176 continue; 177 case '\b' : 178 retval.append("\\b"); 179 continue; 180 case '\t' : 181 retval.append("\\t"); 182 continue; 183 case '\n' : 184 retval.append("\\n"); 185 continue; 186 case '\f' : 187 retval.append("\\f"); 188 continue; 189 case '\r' : 190 retval.append("\\r"); 191 continue; 192 case '\"' : 193 retval.append("\\\""); 194 continue; 195 case '\'' : 196 retval.append("\\\'"); 197 continue; 198 case '\\' : 199 retval.append("\\\\"); 200 continue; 201 default : 202 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) { 203 String s = "0000" + Integer.toString(ch, 16); 204 retval.append( 205 "\\u" + s.substring(s.length() - 4, s.length())); 206 } else { 207 retval.append(ch); 208 } 209 continue; 210 } 211 } 212 return retval.toString(); 213 } 214 215 } 216 | Popular Tags |