1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.xml.handler;
17
18 import java.io.PrintStream;
19 import java.io.PrintWriter;
20
21 import javax.xml.transform.ErrorListener;
22 import javax.xml.transform.SourceLocator;
23 import javax.xml.transform.TransformerException;
24
25 import org.xml.sax.ErrorHandler;
26 import org.xml.sax.SAXException;
27 import org.xml.sax.SAXParseException;
28
29
30
31
32 public class DefaultErrorHandler implements ErrorHandler, ErrorListener
33 {
34
35
36
37
38
39
40
41
42
43 private final PrintWriter messageWriter;
44
45
46
47
48
49
50
51
52
53
54 public DefaultErrorHandler(final PrintStream messageStream)
55 {
56 this(new PrintWriter(messageStream, true));
57 }
58
59
60
61
62 public DefaultErrorHandler()
63 {
64 this(new PrintWriter(System.err, true));
65 }
66
67
68
69
70
71
72 public DefaultErrorHandler(final PrintWriter messageWriter)
73 {
74 this.messageWriter = messageWriter;
75 }
76
77
78
79
80
81
82
83
84
85
86
87 @Override
88 public void warning(final TransformerException exception)
89 throws TransformerException
90 {
91 print(exception, "Transformer Warning");
92 }
93
94 @Override
95 public void error(final TransformerException exception)
96 throws TransformerException
97 {
98 print(exception, "Transformer Error");
99 }
100
101 @Override
102 public void fatalError(final TransformerException exception)
103 throws TransformerException
104 {
105 print(exception, "Fatal Transformer Error");
106 }
107
108 @Override
109 public void warning(final SAXParseException exception) throws SAXException
110 {
111 print(exception, "Parse Warning");
112 }
113
114 @Override
115 public void error(final SAXParseException exception) throws SAXException
116 {
117 print(exception, "Parse Error");
118 }
119
120 @Override
121 public void fatalError(final SAXParseException exception) throws SAXException
122 {
123 print(exception, "Fatal Parse Error");
124 }
125
126
127
128 private void print(final Throwable exception, final String type)
129 {
130 printLocation(exception);
131 messageWriter.println(type + ": " + exception.getMessage());
132 }
133
134 private void printLocation(final Throwable exception)
135 {
136 final SourceLocator locator = calcLocator(exception);
137 printMessage(locator);
138 }
139
140 private void printMessage(final SourceLocator locator)
141 {
142 if (null != locator)
143 {
144 final String publicId = locator.getPublicId();
145 final String systemId = locator.getSystemId();
146
147 final String id =
148 (null != publicId) ? publicId : (null != systemId) ? systemId
149 : "unknown XML ID";
150
151 messageWriter.print(id + ": line " + locator.getLineNumber()
152 + " / column " + locator.getColumnNumber());
153 }
154 else
155 {
156 messageWriter.print("(unknown location)");
157 }
158 }
159
160 @SuppressWarnings("restriction")
161 private SourceLocator calcLocator(final Throwable exception)
162 {
163 SourceLocator locator = null;
164 Throwable cause = exception;
165
166 do
167 {
168 if (cause instanceof SAXParseException)
169 {
170 locator =
171 new com.sun.org.apache.xml.internal.utils.SAXSourceLocator(
172 (SAXParseException) cause);
173 }
174 else if (cause instanceof TransformerException)
175 {
176 locator = handleTransformerException(locator, cause);
177 }
178
179 if (cause instanceof TransformerException)
180 {
181 cause = ((TransformerException) cause).getCause();
182 }
183 else if (cause instanceof com.sun.org.apache.xml.internal.utils.WrappedRuntimeException)
184 {
185 cause =
186 ((com.sun.org.apache.xml.internal.utils.WrappedRuntimeException) cause)
187 .getException();
188 }
189 else if (cause instanceof SAXException)
190 {
191 cause = ((SAXException) cause).getException();
192 }
193 else
194 {
195 cause = null;
196 }
197 }
198 while (null != cause);
199
200 return locator;
201 }
202
203 private SourceLocator handleTransformerException(final SourceLocator locator,
204 final Throwable cause)
205 {
206 final SourceLocator causeLocator =
207 ((TransformerException) cause).getLocator();
208 if (null != causeLocator)
209 {
210 return causeLocator;
211 }
212 return locator;
213 }
214
215
216
217 }