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 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              // jacoco does not count hits. this is why hits is always 0 or 1
65              this.source.addCoverage(nr, (ci == 0 ? 0 : 1));
66  
67              // add branches. unfortunately, there is NO block number and
68              // branch number will NOT be unique between coverage changes.
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  }