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 CoberturaParser extends AbstractXmlEventParser {
37
38 protected Source source;
39 protected boolean inMethods;
40 private int branchId;
41
42 public CoberturaParser(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, "class")) {
49 source = loadSource(xml.getAttributeValue(null, "filename"));
50 String className = xml.getAttributeValue(null, "name");
51 int classifierPosition = className.indexOf('$');
52 if (classifierPosition > 0) {
53 source.setClassifier(className.substring(classifierPosition + 1));
54 }
55 this.branchId = 0;
56 } else
57
58 if (isStartElement(xml, "methods") && source != null) {
59 inMethods = true;
60 } else
61
62 if (isEndElement(xml, "methods") && source != null) {
63 inMethods = false;
64 } else
65
66 if (isStartElement(xml, "line") && !inMethods && source != null) {
67 final int nr = Integer.parseInt(xml.getAttributeValue(null, "number"));
68 source.addCoverage(nr, Integer.valueOf(xml.getAttributeValue(null, "hits")));
69 if (Boolean.parseBoolean(xml.getAttributeValue(null, "branch"))) {
70 final String value = xml.getAttributeValue(null, "condition-coverage");
71
72
73 if (value == null) {
74 return;
75 }
76
77
78 final String[] values = value
79 .replace(" ", "")
80 .replace("%", "/")
81 .replace("(", "")
82 .replace(")", "")
83 .split("/", -1);
84
85 final int cb = Integer.parseInt(values[1]);
86 final int tb = Integer.parseInt(values[2]);
87 final int mb = tb - cb;
88
89
90
91 for (int b = 0; b < cb; b++) {
92 this.source.addBranchCoverage(nr, 0, this.branchId++, 1);
93 }
94 for (int b = 0; b < mb; b++) {
95 this.source.addBranchCoverage(nr, 0, this.branchId++, 0);
96 }
97 }
98 } else
99
100 if (isEndElement(xml, "class") && source != null) {
101 callback.onSource(source);
102 source = null;
103 }
104 }
105
106 }