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 package org.eluder.coveralls.maven.plugin.parser;
26
27 import java.io.BufferedInputStream;
28 import java.io.File;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.nio.file.Files;
32
33 import javax.xml.stream.FactoryConfigurationError;
34 import javax.xml.stream.XMLInputFactory;
35 import javax.xml.stream.XMLStreamConstants;
36 import javax.xml.stream.XMLStreamException;
37 import javax.xml.stream.XMLStreamReader;
38
39 import org.eluder.coveralls.maven.plugin.CoverageParser;
40 import org.eluder.coveralls.maven.plugin.ProcessingException;
41 import org.eluder.coveralls.maven.plugin.domain.Source;
42 import org.eluder.coveralls.maven.plugin.source.SourceCallback;
43 import org.eluder.coveralls.maven.plugin.source.SourceLoader;
44
45
46
47
48 public abstract class AbstractXmlEventParser implements CoverageParser {
49
50
51 private final File coverageFile;
52
53
54 private final SourceLoader sourceLoader;
55
56
57
58
59
60
61
62
63
64 protected AbstractXmlEventParser(final File coverageFile, final SourceLoader sourceLoader) {
65 this.coverageFile = coverageFile;
66 this.sourceLoader = sourceLoader;
67 }
68
69 @Override
70 public final void parse(final SourceCallback callback) throws ProcessingException, IOException {
71 XMLStreamReader xml = null;
72 try (var is = Files.newInputStream(this.coverageFile.toPath());
73 var bis = new BufferedInputStream(is)) {
74 xml = this.createEventReader(bis);
75 while (xml.hasNext()) {
76 xml.next();
77 this.onEvent(xml, callback);
78 }
79 } catch (final XMLStreamException e) {
80 throw new ProcessingException(e);
81 } finally {
82 this.close(xml);
83 }
84 }
85
86 @Override
87 public final File getCoverageFile() {
88 return this.coverageFile;
89 }
90
91
92
93
94
95
96
97
98
99
100
101
102 protected XMLStreamReader createEventReader(final InputStream inputStream) throws ProcessingException {
103 try {
104 final var xmlif = XMLInputFactory.newInstance();
105 xmlif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
106 xmlif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, false);
107 xmlif.setProperty(XMLInputFactory.IS_VALIDATING, false);
108 xmlif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
109 return xmlif.createXMLStreamReader(inputStream);
110 } catch (final FactoryConfigurationError e) {
111 throw new IllegalArgumentException(e);
112 } catch (final XMLStreamException e) {
113 throw new ProcessingException(e);
114 }
115 }
116
117
118
119
120
121
122
123
124
125
126 private void close(final XMLStreamReader xml) throws ProcessingException {
127 if (xml != null) {
128 try {
129 xml.close();
130 } catch (final XMLStreamException e) {
131 throw new ProcessingException(e);
132 }
133 }
134 }
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151 protected abstract void onEvent(final XMLStreamReader xml, SourceCallback callback)
152 throws XMLStreamException, ProcessingException, IOException;
153
154
155
156
157
158
159
160
161
162
163
164
165 protected final Source loadSource(final String sourceFile) throws IOException {
166 return this.sourceLoader.load(sourceFile);
167 }
168
169
170
171
172
173
174
175
176
177
178
179 protected final boolean isStartElement(final XMLStreamReader xml, final String name) {
180 return XMLStreamConstants.START_ELEMENT == xml.getEventType() && xml.getLocalName().equals(name);
181 }
182
183
184
185
186
187
188
189
190
191
192
193 protected final boolean isEndElement(final XMLStreamReader xml, final String name) {
194 return XMLStreamConstants.END_ELEMENT == xml.getEventType() && xml.getLocalName().equals(name);
195 }
196 }