View Javadoc
1   /*
2    * The MIT License (MIT)
3    *
4    * Copyright (c) 2013 - 2023, Tapio Rautonen
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy
7    * of this software and associated documentation files (the "Software"), to deal
8    * in the Software without restriction, including without limitation the rights
9    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10   * copies of the Software, and to permit persons to whom the Software is
11   * furnished to do so, subject to the following conditions:
12   *
13   * The above copyright notice and this permission notice shall be included in
14   * all copies or substantial portions of the Software.
15   *
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22   * THE SOFTWARE.
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                  // Is "condition-coverage" attribute always here?
73                  if (value == null) {
74                      return;
75                  }
76  
77                  // C'mon Cobertura, human readable format for XML ?
78                  final String[] values = value // 50% (2/4)
79                          .replace(" ", "")     // 50%(2/4)
80                          .replace("%", "/")    // 50/(2/4)
81                          .replace("(", "")     // 50/2/4)
82                          .replace(")", "")     // 50/2/4
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                  // add branches. unfortunately, there is NO block number and
90                  // branch number will NOT be unique between coverage changes.
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 }