1 2 30 package com.hp.hpl.jena.rdf.arp; 31 32 import org.xml.sax.SAXParseException ; 33 import java.util.*; 34 35 42 public class ParseException 43 extends SAXParseException 44 implements ARPErrorNumbers, RDFParserConstants { 45 46 51 ParseException( 52 Token cTok, 53 int[][] expectedTokenSequencesVal, 54 String [] tokenImageVal) { 55 super(null, publicId(cTok), systemId(cTok), line(cTok), col(cTok)); 56 specialConstructor = true; 57 where = cTok.location; 58 currentToken = cTok; 59 expectedTokenSequences = expectedTokenSequencesVal; 60 id = ERR_SYNTAX_ERROR; 61 tokenImage = tokenImageVal; 62 } 63 64 private static String systemId(Token c) { 65 return c.location != null ? c.location.inputName : null; 66 } 67 private static String publicId(Token c) { 68 return null; 69 } 70 private static int line(Token c) { 71 return c.location != null ? c.location.endLine : -1; 72 } 73 private static int col(Token c) { 74 return c.location != null ? c.location.endColumn : -1; 75 } 76 77 ParseException(int id, Location where, String msg, SAXParseException e) { 78 super(msg, where.inputName, null, where.endLine, where.endColumn, e); 79 this.where = where; 80 this.id = id; 81 specialConstructor = false; 82 } 83 84 ParseException(int id, Location where, String msg) { 85 super(msg, where.inputName, null, where.endLine, where.endColumn); 86 this.where = where; 87 this.id = id; 88 specialConstructor = false; 89 } 90 91 ParseException(int id, String message) { 92 super(message, null); 93 this.id = id; 94 specialConstructor = false; 95 } 96 97 ParseException() { 98 super(null, null); 99 specialConstructor = false; 100 } 101 SAXParseException rootCause() { 102 Exception e = getException(); 103 return e == null ? this : (SAXParseException ) e; 104 } 105 106 private int id; 107 113 public int getErrorNumber() { 114 return id; 115 } 116 private Location where; 117 118 boolean specialConstructor; 119 120 Token currentToken; 121 122 private boolean isFatal; 123 void setFatal(boolean v) { 124 isFatal = v; 125 } 126 boolean getFatal() { 127 return isFatal; 128 } 129 130 int[][] expectedTokenSequences; 131 private String tokenImage[]; 132 133 boolean promoteMe; 134 135 140 public void promote() { 141 promoteMe = true; 142 } 143 144 private Token startAttributes; 145 146 void setStartAttribute(Token t) { 147 startAttributes = t; 148 } 149 private void startAttrSkip(Token upto) { 150 Token t; 151 for (t = startAttributes; t != upto.next; t = t.next) { 152 switch (t.kind) { 153 case A_XMLBASE : 154 case A_XMLLANG : 155 case AV_STRING : 156 case X_WARNING : 157 case A_XMLNS : 158 continue; 159 default : 160 startAttributes = t; 161 return; 162 } 163 } 164 } 165 166 private String getAttributes(Token upto) { 167 Token t; 168 String rslt = ""; 169 for (t = startAttributes; t != upto.next; t = t.next) { 170 switch (t.kind) { 171 case A_TYPE : 172 rslt += ", rdf:type"; 173 break; 174 case A_ABOUT : 175 rslt += ", rdf:about"; 176 break; 177 case A_DATATYPE : 178 rslt += ", rdf:datatype"; 179 break; 180 case A_NODEID : 181 rslt += ", rdf:nodeID"; 182 break; 183 case A_ID : 184 rslt += ", rdf:ID"; 185 break; 186 case A_PARSETYPE : 187 rslt += ", rdf:parseType"; 188 break; 189 case A_RESOURCE : 190 rslt += ", rdf:resource"; 191 break; 192 case A_RDF_N : 193 rslt += ", rdf:_NNN"; 194 break; 195 196 case A_OTHER : 197 rslt += ", a property attribute"; 198 break; 199 200 case AV_LITERAL : 201 rslt += "='Literal'"; 202 break; 203 case AV_RESOURCE : 204 rslt += "='Resource'"; 205 break; 206 case AV_DAMLCOLLECTION : 207 rslt += "='daml:collection'"; 208 break; 209 210 case AV_STRING : 211 case X_WARNING : 212 continue; 213 case A_XMLNS : 214 case A_XMLBASE : 215 case A_XMLLANG : 216 default : 217 String msg = 218 "Internal mishap in ParseException.getAttributes()"; 219 System.err.println(msg); 220 return msg; 221 } 222 } 223 return rslt.length() > 2 ? rslt.substring(2) : rslt; 224 } 225 226 233 public String getMessage() { 234 String idStr = 237 id != 0 238 ? "{" 239 + (id < 200 ? "W" : "E") 240 + ("" + (1000 + id)).substring(1) 241 + "} " 242 : ""; 243 if (!specialConstructor) { 244 return idStr + super.getMessage(); 245 } 246 Token tok = currentToken.next; 247 startAttrSkip(tok); 248 String retval = 249 "Syntax error when processing " + tok.toString() + "." + eol; 250 if (isElementStripingProblem(tok)) { 256 return idStr 257 + retval 258 + "Cannot have another XML element here." 259 + eol 260 + "(Maybe one object has already been given as the value of the enclosing property)."; 261 } 262 switch (tok.kind) { 263 case CD_STRING : 264 break; 265 case EOF : 266 retval 267 += "Input to RDF parser ended prematurely. This is often related to an XML parser abort." 268 + eol; 269 break; 270 271 case E_DESCRIPTION : 272 return idStr 273 + retval 274 + "rdf:Description elements generally may" 275 + eol 276 + "only occur to describe an object."; 277 case E_RDF : 278 return idStr 279 + retval 280 + "rdf:RDF element tags generally may not occur inside RDF content."; 281 case A_OTHER : 282 case A_RDF_N : 283 if (startAttributes != null) { 284 if (startAttributes == tok) { 285 return idStr 286 + retval 287 + "Cannot have property attributes in this context."; 288 } else { 289 return idStr 290 + retval 291 + "Cannot have property attributes with the following other attributes:" 292 + eol 293 + " " 294 + getAttributes(currentToken); 295 } 296 } 297 case A_TYPE : 298 if (startAttributes != null) { 299 if (startAttributes == tok) { 300 return idStr 301 + retval 302 + "Cannot have rdf:type attribute in this context."; 303 } else { 304 return idStr 305 + retval 306 + "Cannot have rdf:type attribute with the following other attributes:" 307 + eol 308 + " " 309 + getAttributes(currentToken); 310 } 311 } 312 case A_ABOUT : 313 case A_ID : 314 case A_PARSETYPE : 315 case A_RESOURCE : 316 case A_NODEID : 317 case A_DATATYPE : 318 if (startAttributes != null) { 319 if (startAttributes == tok) { 320 return idStr 321 + retval 322 + "Cannot have " 323 + tokenImage[tok.kind] 324 + " in this context."; 325 } else { 326 return idStr 327 + retval 328 + "In this context, the following attributes are not allowed together:" 329 + eol 330 + " " 331 + getAttributes(tok); 332 } 333 } 334 335 case A_XMLBASE : 336 case A_XMLLANG : 337 case A_XMLNS : 338 default : 339 retval = "Unusual " + retval; 340 341 } 342 String expected = ""; 343 int maxSize = 0; 344 BitSet suppress = new BitSet(); 345 suppress.set(X_WARNING); 346 for (int i = 0; i < expectedTokenSequences.length; i++) { 347 switch (expectedTokenSequences[i][0]) { 348 case E_OTHER : 349 suppress.set(E_LI); 350 suppress.set(E_RDF_N); 351 break; 352 case A_OTHER : 353 suppress.set(A_RDF_N); 354 break; 355 case CD_STRING : 356 suppress.set(COMMENT); 357 suppress.set(PROCESSING_INSTRUCTION); 358 break; 359 } 360 } 361 for (int i = 0; i < expectedTokenSequences.length; i++) { 362 if (suppress.get(expectedTokenSequences[i][0])) 363 continue; 364 if (maxSize < expectedTokenSequences[i].length) { 365 maxSize = expectedTokenSequences[i].length; 366 } 367 for (int j = 0; j < expectedTokenSequences[i].length; j++) { 368 expected += tokenImage[expectedTokenSequences[i][j]] + " "; 369 } 370 expected += eol + " "; 371 } 372 retval += "Encountered "; 373 for (int i = 0; i < maxSize; i++) { 374 if (i != 0) 375 retval += " "; 376 retval += tok.toString(); 377 if (tok.kind == 0) { 378 break; 379 } 380 tok = tok.next; 381 } 382 if (expectedTokenSequences.length == 1) { 383 retval += " Was expecting:" + eol + " "; 384 } else { 385 retval += " Was expecting one of:" + eol + " "; 386 } 387 retval += expected; 388 return idStr + retval; 389 390 } 391 static final private int elementStriping[] = 392 new int[] { 393 CD_STRING, 394 PROCESSING_INSTRUCTION, 395 COMMENT, 396 X_SAX_EX, 397 E_END }; 398 static { 399 Arrays.sort(elementStriping); 400 } 401 private boolean isElementStripingProblem(Token tok) { 402 if ( expectedTokenSequences.length != elementStriping.length ) 403 return false; 404 int e[] = new int[elementStriping.length]; 405 for (int i=0;i<e.length; i++) 406 e[i] = expectedTokenSequences[i][0]; 407 Arrays.sort(e); 408 for (int i=0;i<e.length;i++) 409 if (e[i]!=elementStriping[i]) 410 return false; 411 return true; 412 } 413 414 417 static String eol; 418 419 static { 420 try { 421 eol = System.getProperty("line.separator", "\n"); 422 } catch (SecurityException e) { 423 eol = "\n"; 424 } 425 } 426 427 435 static public String formatMessage(Exception e) { 436 String msg = e.getMessage(); 437 if (msg == null) 438 msg = e.toString(); 439 if (e instanceof SAXParseException ) { 440 SAXParseException sax = (SAXParseException ) e; 441 String file = sax.getSystemId(); 442 if (file == null) 443 file = sax.getPublicId(); 444 String rslt = file == null ? "" : file; 445 if (sax.getLineNumber() != -1) { 446 if (sax.getColumnNumber() == -1) { 447 return rslt + "[" + sax.getLineNumber() + "]: " + msg; 448 } else { 449 return rslt 450 + "[" 451 + sax.getLineNumber() 452 + ":" 453 + sax.getColumnNumber() 454 + "]: " 455 + msg; 456 } 457 } else { 458 return (file != null ? (file + ": ") : "") + msg; 459 } 460 } else { 461 return msg; 462 } 463 } 464 465 } 466 | Popular Tags |