KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thaiopensource > xml > sax > ErrorHandlerImpl


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