1 package com.thaiopensource.xml.sax; 2 3 import java.util.ResourceBundle ; 4 import java.text.MessageFormat ; 5 import java.io.PrintWriter ; 6 import java.io.Writer ; 7 import java.io.OutputStream ; 8 import java.io.FileNotFoundException ; 9 10 import org.xml.sax.ErrorHandler ; 11 import org.xml.sax.SAXParseException ; 12 import org.xml.sax.SAXException ; 13 import com.thaiopensource.util.UriOrFile; 14 15 public class ErrorHandlerImpl implements ErrorHandler { 16 private final PrintWriter err; 17 18 private final String bundleName 19 = "com.thaiopensource.xml.sax.resources.Messages"; 20 21 private ResourceBundle bundle = null; 22 23 public ErrorHandlerImpl() { 24 this(System.err); 25 } 26 27 public ErrorHandlerImpl(OutputStream os) { 28 this.err = new PrintWriter (os); 29 } 30 31 public ErrorHandlerImpl(Writer w) { 32 this.err = new PrintWriter (w); 33 } 34 35 public void close() { 36 err.close(); 37 } 38 39 private String getString(String key) { 40 if (bundle == null) 41 bundle = ResourceBundle.getBundle(bundleName); 42 return bundle.getString(key); 43 } 44 45 private String format(String key, Object [] args) { 46 return MessageFormat.format(getString(key), args); 47 } 48 49 public void warning(SAXParseException e) throws SAXParseException { 50 print(format("warning", 51 new Object [] { formatMessage(e), formatLocation(e) })); 52 } 53 54 public void error(SAXParseException e) { 55 print(format("error", 56 new Object [] { formatMessage(e), formatLocation(e) })); 57 } 58 59 public void fatalError(SAXParseException e) throws SAXParseException { 60 throw e; 61 } 62 63 public void printException(Throwable e) { 64 String loc; 65 if (e instanceof SAXParseException ) 66 loc = formatLocation((SAXParseException )e); 67 else 68 loc = ""; 69 String message; 70 if (e instanceof SAXException ) 71 message = formatMessage((SAXException )e); 72 else 73 message = formatMessage(e); 74 print(format("fatal", new Object [] { message, loc })); 75 } 76 77 public void print(String message) { 78 if (message.length() != 0) { 79 err.println(message); 80 err.flush(); 81 } 82 } 83 84 private String formatLocation(SAXParseException e) { 85 String systemId = e.getSystemId(); 86 int n = e.getLineNumber(); 87 Integer lineNumber = n >= 0 ? new Integer (n) : null; 88 n = e.getColumnNumber(); 89 Integer columnNumber = n >= 0 ? new Integer (n) : null; 90 if (systemId != null) { 91 systemId = UriOrFile.uriToUriOrFile(systemId); 92 if (lineNumber != null) { 93 if (columnNumber != null) 94 return format("locator_system_id_line_number_column_number", 95 new Object [] { systemId, lineNumber, columnNumber }); 96 else 97 return format("locator_system_id_line_number", 98 new Object [] { systemId, lineNumber }); 99 } 100 else 101 return format("locator_system_id", 102 new Object [] { systemId }); 103 } 104 else if (lineNumber != null) { 105 if (columnNumber != null) 106 return format("locator_line_number_column_number", 107 new Object [] { lineNumber, columnNumber }); 108 else 109 return format("locator_line_number", 110 new Object [] { lineNumber }); 111 } 112 else 113 return ""; 114 } 115 116 private String formatMessage(SAXException se) { 117 Exception e = se.getException(); 118 String detail = se.getMessage(); 119 if (e != null) { 120 String detail2 = e.getMessage(); 121 if (detail2 == detail || e.getClass().getName().equals(detail)) 123 return formatMessage(e); 124 else if (detail2 == null) 125 return format("exception", 126 new Object []{ e.getClass().getName(), detail }); 127 else 128 return format("tunnel_exception", 129 new Object [] { e.getClass().getName(), 130 detail, 131 detail2 }); 132 } 133 else { 134 if (detail == null) 135 detail = getString("no_detail"); 136 return detail; 137 } 138 } 139 140 private String formatMessage(Throwable e) { 141 String detail = e.getMessage(); 142 if (detail == null) 143 detail = getString("no_detail"); 144 if (e instanceof FileNotFoundException ) 145 return format("file_not_found", new Object [] { detail }); 146 return format("exception", 147 new Object [] { e.getClass().getName(), detail }); 148 } 149 } 150 | Popular Tags |