1
2
3
4
5
6
7
8 package org.dom4j.util;
9
10 import org.dom4j.DocumentHelper;
11 import org.dom4j.Element;
12 import org.dom4j.QName;
13
14 import org.xml.sax.ErrorHandler;
15 import org.xml.sax.SAXParseException;
16
17 /***
18 * <code>XMLErrorHandler</code> is a SAX {@link ErrorHandler}which turns the
19 * SAX parsing errors into XML so that the output can be formatted using XSLT or
20 * the errors can be included in a SOAP message.
21 *
22 * @author <a href="mailto:jstrachan@apache.org">James Strachan </a>
23 * @version $Revision: 1.7 $
24 */
25 public class XMLErrorHandler implements ErrorHandler {
26 protected static final QName ERROR_QNAME = QName.get("error");
27
28 protected static final QName FATALERROR_QNAME = QName.get("fatalError");
29
30 protected static final QName WARNING_QNAME = QName.get("warning");
31
32 /*** Stores the errors that occur during a SAX parse */
33 private Element errors;
34
35 /*** QName used for error elements */
36 private QName errorQName = ERROR_QNAME;
37
38 /*** QName used for fatalerror elements */
39 private QName fatalErrorQName = FATALERROR_QNAME;
40
41 /*** QName used for warning elements */
42 private QName warningQName = WARNING_QNAME;
43
44 public XMLErrorHandler() {
45 this.errors = DocumentHelper.createElement("errors");
46 }
47
48 public XMLErrorHandler(Element errors) {
49 this.errors = errors;
50 }
51
52 public void error(SAXParseException e) {
53 Element element = errors.addElement(errorQName);
54 addException(element, e);
55 }
56
57 public void fatalError(SAXParseException e) {
58 Element element = errors.addElement(fatalErrorQName);
59 addException(element, e);
60 }
61
62 public void warning(SAXParseException e) {
63 Element element = errors.addElement(warningQName);
64 addException(element, e);
65 }
66
67
68
69 public Element getErrors() {
70 return errors;
71 }
72
73 public void setErrors(Element errors) {
74 this.errors = errors;
75 }
76
77
78 public QName getErrorQName() {
79 return errorQName;
80 }
81
82 public void setErrorQName(QName errorQName) {
83 this.errorQName = errorQName;
84 }
85
86 public QName getFatalErrorQName() {
87 return fatalErrorQName;
88 }
89
90 public void setFatalErrorQName(QName fatalErrorQName) {
91 this.fatalErrorQName = fatalErrorQName;
92 }
93
94 public QName getWarningQName() {
95 return warningQName;
96 }
97
98 public void setWarningQName(QName warningQName) {
99 this.warningQName = warningQName;
100 }
101
102
103
104
105 /***
106 * Adds the given parse exception information to the given element instance
107 *
108 * @param element
109 * DOCUMENT ME!
110 * @param e
111 * DOCUMENT ME!
112 */
113 protected void addException(Element element, SAXParseException e) {
114 element.addAttribute("column", Integer.toString(e.getColumnNumber()));
115 element.addAttribute("line", Integer.toString(e.getLineNumber()));
116
117 String publicID = e.getPublicId();
118
119 if ((publicID != null) && (publicID.length() > 0)) {
120 element.addAttribute("publicID", publicID);
121 }
122
123 String systemID = e.getSystemId();
124
125 if ((systemID != null) && (systemID.length() > 0)) {
126 element.addAttribute("systemID", systemID);
127 }
128
129 element.addText(e.getMessage());
130 }
131 }
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168