001 package com.thaiopensource.xml.sax;
002
003 import java.util.ResourceBundle;
004 import java.text.MessageFormat;
005 import java.io.PrintWriter;
006 import java.io.Writer;
007 import java.io.OutputStream;
008 import java.io.FileNotFoundException;
009
010 import org.xml.sax.ErrorHandler;
011 import org.xml.sax.SAXParseException;
012 import org.xml.sax.SAXException;
013 import com.thaiopensource.util.UriOrFile;
014
015 public class ErrorHandlerImpl implements ErrorHandler {
016 private final PrintWriter err;
017
018 private final String bundleName
019 = "com.thaiopensource.xml.sax.resources.Messages";
020
021 private ResourceBundle bundle = null;
022
023 public ErrorHandlerImpl() {
024 this(System.err);
025 }
026
027 public ErrorHandlerImpl(OutputStream os) {
028 this.err = new PrintWriter(os);
029 }
030
031 public ErrorHandlerImpl(Writer w) {
032 this.err = new PrintWriter(w);
033 }
034
035 public void close() {
036 err.close();
037 }
038
039 private String getString(String key) {
040 if (bundle == null)
041 bundle = ResourceBundle.getBundle(bundleName);
042 return bundle.getString(key);
043 }
044
045 private String format(String key, Object[] args) {
046 return MessageFormat.format(getString(key), args);
047 }
048
049 public void warning(SAXParseException e) throws SAXParseException {
050 print(format("warning",
051 new Object[] { formatMessage(e), formatLocation(e) }));
052 }
053
054 public void error(SAXParseException e) {
055 print(format("error",
056 new Object[] { formatMessage(e), formatLocation(e) }));
057 }
058
059 public void fatalError(SAXParseException e) throws SAXParseException {
060 throw e;
061 }
062
063 public void printException(Throwable e) {
064 String loc;
065 if (e instanceof SAXParseException)
066 loc = formatLocation((SAXParseException)e);
067 else
068 loc = "";
069 String message;
070 if (e instanceof SAXException)
071 message = formatMessage((SAXException)e);
072 else
073 message = formatMessage(e);
074 print(format("fatal", new Object[] { message, loc }));
075 }
076
077 public void print(String message) {
078 if (message.length() != 0) {
079 err.println(message);
080 err.flush();
081 }
082 }
083
084 private String formatLocation(SAXParseException e) {
085 String systemId = e.getSystemId();
086 int n = e.getLineNumber();
087 Integer lineNumber = n >= 0 ? new Integer(n) : null;
088 n = e.getColumnNumber();
089 Integer columnNumber = n >= 0 ? new Integer(n) : null;
090 if (systemId != null) {
091 systemId = UriOrFile.uriToUriOrFile(systemId);
092 if (lineNumber != null) {
093 if (columnNumber != null)
094 return format("locator_system_id_line_number_column_number",
095 new Object[] { systemId, lineNumber, columnNumber });
096 else
097 return format("locator_system_id_line_number",
098 new Object[] { systemId, lineNumber });
099 }
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 // Crimson stupidity
122 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 }