1 16 19 20 package com.sun.org.apache.xml.internal.utils; 21 22 import java.io.BufferedReader ; 23 import java.io.InputStream ; 24 import java.io.InputStreamReader ; 25 import java.io.PrintWriter ; 26 import java.net.URL ; 27 import java.net.URLConnection ; 28 29 import javax.xml.transform.ErrorListener ; 30 import javax.xml.transform.SourceLocator ; 31 import javax.xml.transform.TransformerException ; 32 33 import com.sun.org.apache.xml.internal.res.XMLErrorResources; 34 import com.sun.org.apache.xml.internal.res.XMLMessages; 35 36 import org.xml.sax.ErrorHandler ; 37 import org.xml.sax.SAXException ; 38 import org.xml.sax.SAXParseException ; 39 40 41 52 public class ListingErrorHandler implements ErrorHandler , ErrorListener 53 { 54 protected PrintWriter m_pw = null; 55 56 57 60 public ListingErrorHandler(PrintWriter pw) 61 { 62 if (null == pw) 63 throw new NullPointerException (XMLMessages.createXMLMessage(XMLErrorResources.ER_ERRORHANDLER_CREATED_WITH_NULL_PRINTWRITER, null)); 64 66 m_pw = pw; 67 } 68 69 72 public ListingErrorHandler() 73 { 74 m_pw = new PrintWriter (System.err, true); 75 } 76 77 78 79 99 public void warning (SAXParseException exception) 100 throws SAXException 101 { 102 logExceptionLocation(m_pw, exception); 103 m_pw.println("warning: " + exception.getMessage()); 106 m_pw.flush(); 107 108 if (getThrowOnWarning()) 109 throw exception; 110 } 111 112 113 138 public void error (SAXParseException exception) 139 throws SAXException 140 { 141 logExceptionLocation(m_pw, exception); 142 m_pw.println("error: " + exception.getMessage()); 143 m_pw.flush(); 144 145 if (getThrowOnError()) 146 throw exception; 147 } 148 149 150 170 public void fatalError (SAXParseException exception) 171 throws SAXException 172 { 173 logExceptionLocation(m_pw, exception); 174 m_pw.println("fatalError: " + exception.getMessage()); 175 m_pw.flush(); 176 177 if (getThrowOnFatalError()) 178 throw exception; 179 } 180 181 182 183 184 203 public void warning(TransformerException exception) 204 throws TransformerException 205 { 206 logExceptionLocation(m_pw, exception); 207 m_pw.println("warning: " + exception.getMessage()); 208 m_pw.flush(); 209 210 if (getThrowOnWarning()) 211 throw exception; 212 } 213 214 230 public void error(TransformerException exception) 231 throws TransformerException 232 { 233 logExceptionLocation(m_pw, exception); 234 m_pw.println("error: " + exception.getMessage()); 235 m_pw.flush(); 236 237 if (getThrowOnError()) 238 throw exception; 239 } 240 241 258 public void fatalError(TransformerException exception) 259 throws TransformerException 260 { 261 logExceptionLocation(m_pw, exception); 262 m_pw.println("error: " + exception.getMessage()); 263 m_pw.flush(); 264 265 if (getThrowOnError()) 266 throw exception; 267 } 268 269 270 271 272 273 274 282 public static void logExceptionLocation(PrintWriter pw, Throwable exception) 283 { 284 if (null == pw) 285 pw = new PrintWriter (System.err, true); 286 287 SourceLocator locator = null; 288 Throwable cause = exception; 289 290 do 292 { 293 if(cause instanceof SAXParseException ) 295 { 296 locator = new SAXSourceLocator((SAXParseException )cause); 302 } 303 else if (cause instanceof TransformerException ) 304 { 305 SourceLocator causeLocator = ((TransformerException )cause).getLocator(); 306 if(null != causeLocator) 307 { 308 locator = causeLocator; 309 } 310 } 311 312 if(cause instanceof TransformerException ) 314 cause = ((TransformerException )cause).getCause(); 315 else if(cause instanceof WrappedRuntimeException) 316 cause = ((WrappedRuntimeException)cause).getException(); 317 else if(cause instanceof SAXException ) 318 cause = ((SAXException )cause).getException(); 319 else 320 cause = null; 321 } 322 while(null != cause); 323 324 if(null != locator) 328 { 329 String id = (locator.getPublicId() != locator.getPublicId()) 330 ? locator.getPublicId() 331 : (null != locator.getSystemId()) 332 ? locator.getSystemId() : "SystemId-Unknown"; 333 334 pw.print(id + ":Line=" + locator.getLineNumber() 335 + ";Column=" + locator.getColumnNumber()+": "); 336 pw.println("exception:" + exception.getMessage()); 337 pw.println("root-cause:" 338 + ((null != cause) ? cause.getMessage() : "null")); 339 logSourceLine(pw, locator); 340 } 341 else 342 { 343 pw.print("SystemId-Unknown:locator-unavailable: "); 344 pw.println("exception:" + exception.getMessage()); 345 pw.println("root-cause:" 346 + ((null != cause) ? cause.getMessage() : "null")); 347 } 348 } 349 350 351 360 public static void logSourceLine(PrintWriter pw, SourceLocator locator) 361 { 362 if (null == locator) 363 return; 364 365 if (null == pw) 366 pw = new PrintWriter (System.err, true); 367 368 String url = locator.getSystemId(); 369 if (null == url) 373 { 374 pw.println("line: (No systemId; cannot read file)"); 375 pw.println(); 376 return; 377 } 378 379 381 try 382 { 383 int line = locator.getLineNumber(); 384 int column = locator.getColumnNumber(); 385 pw.println("line: " + getSourceLine(url, line)); 386 StringBuffer buf = new StringBuffer ("line: "); 387 for (int i = 1; i < column; i++) 388 { 389 buf.append(' '); 390 } 391 buf.append('^'); 392 pw.println(buf.toString()); 393 } 394 catch (Exception e) 395 { 396 pw.println("line: logSourceLine unavailable due to: " + e.getMessage()); 397 pw.println(); 398 } 399 } 400 401 402 408 protected static String getSourceLine(String sourceUrl, int lineNum) 409 throws Exception 410 { 411 URL url = null; 412 try 414 { 415 url = new URL (sourceUrl); 417 } 418 catch (java.net.MalformedURLException mue) 419 { 420 int indexOfColon = sourceUrl.indexOf(':'); 421 int indexOfSlash = sourceUrl.indexOf('/'); 422 423 if ((indexOfColon != -1) 424 && (indexOfSlash != -1) 425 && (indexOfColon < indexOfSlash)) 426 { 427 throw mue; 430 } 431 else 432 { 433 url = new URL (SystemIDResolver.getAbsoluteURI(sourceUrl)); 435 } 437 } 438 439 String line = null; 440 InputStream is = null; 441 BufferedReader br = null; 442 try 443 { 444 URLConnection uc = url.openConnection(); 446 is = uc.getInputStream(); 447 br = new BufferedReader (new InputStreamReader (is)); 448 449 for (int i = 1; i <= lineNum; i++) 452 { 453 line = br.readLine(); 454 } 455 456 } 457 finally 460 { 461 br.close(); 462 is.close(); 463 } 464 465 return line; 467 } 468 469 470 471 472 482 public void setThrowOnWarning(boolean b) 483 { 484 throwOnWarning = b; 485 } 486 487 492 public boolean getThrowOnWarning() 493 { 494 return throwOnWarning; 495 } 496 497 498 protected boolean throwOnWarning = false; 499 500 501 514 public void setThrowOnError(boolean b) 515 { 516 throwOnError = b; 517 } 518 519 524 public boolean getThrowOnError() 525 { 526 return throwOnError; 527 } 528 529 530 protected boolean throwOnError = true; 531 532 533 547 public void setThrowOnFatalError(boolean b) 548 { 549 throwOnFatalError = b; 550 } 551 552 557 public boolean getThrowOnFatalError() 558 { 559 return throwOnFatalError; 560 } 561 562 563 protected boolean throwOnFatalError = true; 564 565 } 566 | Popular Tags |