1 17 package org.apache.jasper.compiler; 18 19 import java.io.BufferedReader ; 20 import java.io.IOException ; 21 import java.io.StringReader ; 22 import java.net.MalformedURLException ; 23 import java.util.ArrayList ; 24 25 import org.apache.jasper.JasperException; 26 import org.xml.sax.SAXException ; 27 28 42 public class ErrorDispatcher { 43 44 private ErrorHandler errHandler; 46 47 private boolean jspcMode = false; 49 50 51 57 public ErrorDispatcher(boolean jspcMode) { 58 errHandler = new DefaultErrorHandler(); 60 this.jspcMode = jspcMode; 61 } 62 63 72 public void jspError(String errCode) throws JasperException { 73 dispatch(null, errCode, null, null); 74 } 75 76 86 public void jspError(Mark where, String errCode) throws JasperException { 87 dispatch(where, errCode, null, null); 88 } 89 90 100 public void jspError(Node n, String errCode) throws JasperException { 101 dispatch(n.getStart(), errCode, null, null); 102 } 103 104 114 public void jspError(String errCode, String arg) throws JasperException { 115 dispatch(null, errCode, new Object [] {arg}, null); 116 } 117 118 129 public void jspError(Mark where, String errCode, String arg) 130 throws JasperException { 131 dispatch(where, errCode, new Object [] {arg}, null); 132 } 133 134 145 public void jspError(Node n, String errCode, String arg) 146 throws JasperException { 147 dispatch(n.getStart(), errCode, new Object [] {arg}, null); 148 } 149 150 161 public void jspError(String errCode, String arg1, String arg2) 162 throws JasperException { 163 dispatch(null, errCode, new Object [] {arg1, arg2}, null); 164 } 165 166 178 public void jspError(String errCode, String arg1, String arg2, String arg3) 179 throws JasperException { 180 dispatch(null, errCode, new Object [] {arg1, arg2, arg3}, null); 181 } 182 183 195 public void jspError(Mark where, String errCode, String arg1, String arg2) 196 throws JasperException { 197 dispatch(where, errCode, new Object [] {arg1, arg2}, null); 198 } 199 200 213 214 public void jspError(Mark where, String errCode, String arg1, String arg2, 215 String arg3) 216 throws JasperException { 217 dispatch(where, errCode, new Object [] {arg1, arg2, arg3}, null); 218 } 219 220 232 233 public void jspError(Node n, String errCode, String arg1, String arg2) 234 throws JasperException { 235 dispatch(n.getStart(), errCode, new Object [] {arg1, arg2}, null); 236 } 237 238 251 252 public void jspError(Node n, String errCode, String arg1, String arg2, 253 String arg3) 254 throws JasperException { 255 dispatch(n.getStart(), errCode, new Object [] {arg1, arg2, arg3}, null); 256 } 257 258 263 public void jspError(Exception e) throws JasperException { 264 dispatch(null, null, null, e); 265 } 266 267 278 public void jspError(String errCode, String arg, Exception e) 279 throws JasperException { 280 dispatch(null, errCode, new Object [] {arg}, e); 281 } 282 283 295 public void jspError(Node n, String errCode, String arg, Exception e) 296 throws JasperException { 297 dispatch(n.getStart(), errCode, new Object [] {arg}, e); 298 } 299 300 312 public static JavacErrorDetail[] parseJavacErrors(String errMsg, 313 String fname, 314 Node.Nodes page) 315 throws JasperException, IOException { 316 317 return parseJavacMessage(errMsg, fname, page); 318 } 319 320 326 public void javacError(JavacErrorDetail[] javacErrors) 327 throws JasperException { 328 329 errHandler.javacError(javacErrors); 330 } 331 332 333 340 public void javacError(String errorReport, Exception e) 341 throws JasperException { 342 343 errHandler.javacError(errorReport, e); 344 } 345 346 347 350 362 private void dispatch(Mark where, String errCode, Object [] args, 363 Exception e) throws JasperException { 364 String file = null; 365 String errMsg = null; 366 int line = -1; 367 int column = -1; 368 boolean hasLocation = false; 369 370 if (errCode != null) { 372 errMsg = Localizer.getMessage(errCode, args); 373 } else if (e != null) { 374 errMsg = e.getMessage(); 376 } 377 378 if (where != null) { 380 if (jspcMode) { 381 try { 383 file = where.getURL().toString(); 384 } catch (MalformedURLException me) { 385 file = where.getFile(); 387 } 388 } else { 389 file = where.getFile(); 392 } 393 line = where.getLineNumber(); 394 column = where.getColumnNumber(); 395 hasLocation = true; 396 } 397 398 Exception nestedEx = e; 400 if ((e instanceof SAXException ) 401 && (((SAXException ) e).getException() != null)) { 402 nestedEx = ((SAXException ) e).getException(); 403 } 404 405 if (hasLocation) { 406 errHandler.jspError(file, line, column, errMsg, nestedEx); 407 } else { 408 errHandler.jspError(errMsg, nestedEx); 409 } 410 } 411 412 428 private static JavacErrorDetail[] parseJavacMessage( 429 String errMsg, String fname, Node.Nodes page) 430 throws IOException , JasperException { 431 432 ArrayList <JavacErrorDetail> errors = new ArrayList <JavacErrorDetail>(); 433 StringBuffer errMsgBuf = null; 434 int lineNum = -1; 435 JavacErrorDetail javacError = null; 436 437 BufferedReader reader = new BufferedReader (new StringReader (errMsg)); 438 439 444 String line = null; 445 while ((line = reader.readLine()) != null) { 446 447 452 int beginColon = line.indexOf(':', 2); 453 int endColon = line.indexOf(':', beginColon + 1); 454 if ((beginColon >= 0) && (endColon >= 0)) { 455 if (javacError != null) { 456 errors.add(javacError); 458 } 459 460 String lineNumStr = line.substring(beginColon + 1, endColon); 461 try { 462 lineNum = Integer.parseInt(lineNumStr); 463 } catch (NumberFormatException e) { 464 lineNum = -1; 465 } 466 467 errMsgBuf = new StringBuffer (); 468 469 javacError = createJavacError(fname, page, errMsgBuf, lineNum); 470 } 471 472 if (errMsgBuf != null) { 474 errMsgBuf.append(line); 475 errMsgBuf.append("\n"); 476 } 477 } 478 479 if (javacError != null) { 481 errors.add(javacError); 482 } 483 484 reader.close(); 485 486 JavacErrorDetail[] errDetails = null; 487 if (errors.size() > 0) { 488 errDetails = new JavacErrorDetail[errors.size()]; 489 errors.toArray(errDetails); 490 } 491 492 return errDetails; 493 } 494 495 496 504 public static JavacErrorDetail createJavacError(String fname, Node.Nodes page, 505 StringBuffer errMsgBuf, int lineNum) throws JasperException { 506 JavacErrorDetail javacError; 507 ErrorVisitor errVisitor = new ErrorVisitor(lineNum); 509 page.visit(errVisitor); 510 Node errNode = errVisitor.getJspSourceNode(); 511 if ((errNode != null) && (errNode.getStart() != null)) { 512 javacError = new JavacErrorDetail( 513 fname, 514 lineNum, 515 errNode.getStart().getFile(), 516 errNode.getStart().getLineNumber(), 517 errMsgBuf); 518 } else { 519 532 javacError = new JavacErrorDetail( 533 fname, 534 lineNum, 535 errMsgBuf); 536 } 537 return javacError; 538 } 539 540 541 545 static class ErrorVisitor extends Node.Visitor { 546 547 private int lineNum; 549 550 554 Node found; 555 556 561 public ErrorVisitor(int lineNum) { 562 this.lineNum = lineNum; 563 } 564 565 public void doVisit(Node n) throws JasperException { 566 if ((lineNum >= n.getBeginJavaLine()) 567 && (lineNum < n.getEndJavaLine())) { 568 found = n; 569 } 570 } 571 572 579 public Node getJspSourceNode() { 580 return found; 581 } 582 } 583 } 584 | Popular Tags |