1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 package junitx.ddtunit.data.processing.parser;
39
40 import junitx.ddtunit.DDTException;
41 import junitx.ddtunit.data.IDataSet;
42 import junitx.ddtunit.data.processing.Engine;
43
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.xml.sax.Attributes;
47 import org.xml.sax.Locator;
48 import org.xml.sax.SAXException;
49 import org.xml.sax.SAXParseException;
50 import org.xml.sax.ext.LexicalHandler;
51 import org.xml.sax.helpers.DefaultHandler;
52
53
54
55
56
57
58 class ContentHandler extends DefaultHandler implements LexicalHandler {
59 private static final String LF = System.getProperty("line.separator");
60
61 Logger log = LoggerFactory.getLogger(ContentHandler.class);
62
63 private Locator locator;
64
65 private String clusterId;
66
67 private String resourceName;
68
69 private int levelCount;
70
71 private Engine eventConsumer;
72
73
74
75
76
77
78 public ContentHandler(String resourceName, IDataSet clusterDataSet) {
79 super();
80 this.resourceName = resourceName;
81 this.eventConsumer = new Engine(clusterDataSet);
82 this.levelCount = 0;
83 }
84
85
86
87
88
89
90 public void setDocumentLocator(Locator aLocator) {
91 this.locator = aLocator;
92 }
93
94
95
96
97 public void startDocument() {
98 log.debug("Start Document...");
99 }
100
101
102
103
104 public void endDocument() {
105 log.debug("End Document...");
106 }
107
108
109
110
111
112
113
114
115
116 public void startElement(String namespaceURI, String localName,
117 String qName, Attributes attributes) {
118 try {
119 this.levelCount++;
120 this.eventConsumer.processStartElement(qName, attributes,
121 this.levelCount);
122 } catch (Exception ex) {
123 StringBuffer sb = new StringBuffer(ex.getClass().getName());
124 sb.append(ex.getMessage());
125 sb.append(LF).append("Resource \'").append(this.resourceName)
126 .append("\' line/column ").append(locator.getLineNumber())
127 .append("/").append(locator.getColumnNumber());
128 DDTException ddtEx = new DDTException(sb.toString(), ex);
129 ddtEx.setStackTrace(ex.getStackTrace());
130 log.warn(sb.toString(), ddtEx);
131 throw ddtEx;
132 }
133 }
134
135
136
137
138
139
140
141
142
143
144 public void endElement(String namespace, String localName, String qName)
145 throws SAXException {
146 try {
147 this.eventConsumer.processEndElement(qName, this.levelCount);
148 } catch (Exception ex) {
149 StringBuffer sb = new StringBuffer(ex.getClass().getName());
150 sb.append(ex.getMessage());
151 sb.append(LF).append("Resource \'").append(this.resourceName)
152 .append("\' line/column ").append(locator.getLineNumber())
153 .append("/").append(locator.getColumnNumber());
154 DDTException ddtEx = new DDTException(sb.toString(), ex);
155 ddtEx.setStackTrace(ex.getStackTrace());
156 log.warn(sb.toString(), ddtEx);
157 throw ddtEx;
158 } finally {
159 this.levelCount--;
160 }
161 }
162
163
164
165
166
167
168
169
170
171
172
173
174 public void characters(char[] buffer, int offset, int length)
175 throws SAXException {
176 this.eventConsumer.processCharacters(buffer, offset, length);
177 }
178
179 public void ignorableWhitespace(char[] buffer, int offset, int length) {
180 System.err.println(new String(buffer, offset, length));
181 }
182
183
184
185
186
187
188
189 public void error(SAXParseException e) throws SAXException {
190 log.error("Parse error detected", e);
191 throw e;
192 }
193
194 public void setClusterId(String clusterId) {
195 this.clusterId = clusterId;
196 this.eventConsumer.setClusterId(clusterId);
197 }
198
199 public String getClusterId() {
200 return clusterId;
201 }
202
203
204
205
206
207
208 public void endCDATA() throws SAXException {
209 this.eventConsumer.endCDATA();
210 }
211
212
213
214
215
216
217 public void endDTD() throws SAXException {
218 }
219
220
221
222
223
224
225 public void startCDATA() throws SAXException {
226 this.eventConsumer.startCDATA();
227 }
228
229
230
231
232
233
234 public void comment(char[] ch, int start, int length) throws SAXException {
235 }
236
237
238
239
240
241
242 public void endEntity(String name) throws SAXException {
243 }
244
245
246
247
248
249
250 public void startEntity(String name) throws SAXException {
251 }
252
253
254
255
256
257
258
259 public void startDTD(String name, String publicId, String systemId)
260 throws SAXException {
261 }
262 }