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 java.io.File;
27 import java.io.IOException;
28 import javax.xml.stream.XMLStreamException;
29 import javax.xml.stream.XMLStreamReader;
30
31 import org.eluder.coveralls.maven.plugin.ProcessingException;
32 import org.eluder.coveralls.maven.plugin.domain.Source;
33 import org.eluder.coveralls.maven.plugin.source.SourceCallback;
34 import org.eluder.coveralls.maven.plugin.source.SourceLoader;
35
36 public class JaCoCoParser extends AbstractXmlEventParser {
37
38 private String packageName;
39 private Source source;
40 private int branchId;
41
42 public JaCoCoParser(final File coverageFile, final SourceLoader sourceLoader) {
43 super(coverageFile, sourceLoader);
44 }
45
46 @Override
47 protected void onEvent(final XMLStreamReader xml, final SourceCallback callback) throws XMLStreamException, ProcessingException, IOException {
48 if (isStartElement(xml, "package")) {
49 this.packageName = xml.getAttributeValue(null, "name");
50 } else
51
52 if (isStartElement(xml, "sourcefile") && packageName != null) {
53 String sourceFile = this.packageName + "/" + xml.getAttributeValue(null, "name");
54 this.source = loadSource(sourceFile);
55 this.branchId = 0;
56 } else
57
58 if (isStartElement(xml, "line") && this.source != null) {
59 int ci = Integer.parseInt(xml.getAttributeValue(null, "ci"));
60 int cb = Integer.parseInt(xml.getAttributeValue(null, "cb"));
61 int mb = Integer.parseInt(xml.getAttributeValue(null, "mb"));
62 int nr = Integer.parseInt(xml.getAttributeValue(null, "nr"));
63
64
65 this.source.addCoverage(nr, (ci == 0 ? 0 : 1));
66
67
68
69 for (int b = 0; b < cb; b++) {
70 this.source.addBranchCoverage(nr, 0, this.branchId++, 1);
71 }
72 for (int b = 0; b < mb; b++) {
73 this.source.addBranchCoverage(nr, 0, this.branchId++, 0);
74 }
75 } else
76
77 if (isEndElement(xml, "sourcefile") && this.source != null) {
78 callback.onSource(this.source);
79 this.source = null;
80 } else
81
82 if (isEndElement(xml, "package")) {
83 this.packageName = null;
84 }
85 }
86
87 }