1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.eluder.coveralls.maven.plugin.parser;
25
26 import org.codehaus.plexus.util.ReaderFactory;
27 import org.codehaus.plexus.util.xml.XmlStreamReader;
28 import org.eluder.coveralls.maven.plugin.CoverageParser;
29 import org.eluder.coveralls.maven.plugin.ProcessingException;
30 import org.eluder.coveralls.maven.plugin.domain.Source;
31 import org.eluder.coveralls.maven.plugin.source.SourceCallback;
32 import org.eluder.coveralls.maven.plugin.source.SourceLoader;
33
34 import javax.xml.stream.FactoryConfigurationError;
35 import javax.xml.stream.XMLInputFactory;
36 import javax.xml.stream.XMLStreamConstants;
37 import javax.xml.stream.XMLStreamException;
38 import javax.xml.stream.XMLStreamReader;
39 import java.io.File;
40 import java.io.IOException;
41 import java.io.Reader;
42
43 public abstract class AbstractXmlEventParser implements CoverageParser {
44
45 private final File coverageFile;
46 private final SourceLoader sourceLoader;
47
48 protected AbstractXmlEventParser(final File coverageFile, final SourceLoader sourceLoader) {
49 this.coverageFile = coverageFile;
50 this.sourceLoader = sourceLoader;
51 }
52
53 @Override
54 public final void parse(final SourceCallback callback) throws ProcessingException, IOException {
55 XMLStreamReader xml = null;
56 try (XmlStreamReader reader = ReaderFactory.newXmlReader(coverageFile)) {
57 xml = createEventReader(reader);
58 while (xml.hasNext()) {
59 xml.next();
60 onEvent(xml, callback);
61 }
62 } catch (XMLStreamException ex) {
63 throw new ProcessingException(ex);
64 } finally {
65 close(xml);
66 }
67 }
68
69 @Override
70 public final File getCoverageFile() {
71 return coverageFile;
72 }
73
74 protected XMLStreamReader createEventReader(final Reader reader) throws ProcessingException {
75 try {
76 XMLInputFactory xmlif = XMLInputFactory.newInstance();
77 xmlif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
78 xmlif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, false);
79 xmlif.setProperty(XMLInputFactory.IS_VALIDATING, false);
80 return xmlif.createXMLStreamReader(reader);
81 } catch (FactoryConfigurationError ex) {
82 throw new IllegalArgumentException(ex);
83 } catch (XMLStreamException ex) {
84 throw new ProcessingException(ex);
85 }
86 }
87
88 private void close(final XMLStreamReader xml) throws ProcessingException {
89 if (xml != null) {
90 try {
91 xml.close();
92 } catch (XMLStreamException ex) {
93 throw new ProcessingException(ex);
94 }
95 }
96 }
97
98 protected abstract void onEvent(final XMLStreamReader xml, SourceCallback callback) throws XMLStreamException, ProcessingException, IOException;
99
100 protected final Source loadSource(final String sourceFile) throws IOException {
101 return sourceLoader.load(sourceFile);
102 }
103
104 protected final boolean isStartElement(final XMLStreamReader xml, final String name) {
105 return (XMLStreamConstants.START_ELEMENT == xml.getEventType() && xml.getLocalName().equals(name));
106 }
107
108 protected final boolean isEndElement(final XMLStreamReader xml, final String name) {
109 return (XMLStreamConstants.END_ELEMENT == xml.getEventType() && xml.getLocalName().equals(name));
110 }
111 }