View Javadoc
1   /*
2    * The MIT License (MIT)
3    *
4    * Copyright (c) 2013-2026 The Coveralls Maven Plugin Project Contributors:
5    *     https://github.com/hazendaz/coveralls-maven-plugin/graphs/contributors
6    *
7    * Permission is hereby granted, free of charge, to any person obtaining a copy
8    * of this software and associated documentation files (the "Software"), to deal
9    * in the Software without restriction, including without limitation the rights
10   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11   * copies of the Software, and to permit persons to whom the Software is
12   * furnished to do so, subject to the following conditions:
13   *
14   * The above copyright notice and this permission notice shall be included in
15   * all copies or substantial portions of the Software.
16   *
17   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23   * THE SOFTWARE.
24   */
25  package org.eluder.coveralls.maven.plugin.parser;
26  
27  import java.io.File;
28  import java.io.IOException;
29  
30  import javax.xml.stream.XMLStreamException;
31  import javax.xml.stream.XMLStreamReader;
32  
33  import org.eluder.coveralls.maven.plugin.ProcessingException;
34  import org.eluder.coveralls.maven.plugin.domain.Source;
35  import org.eluder.coveralls.maven.plugin.source.SourceCallback;
36  import org.eluder.coveralls.maven.plugin.source.SourceLoader;
37  
38  /**
39   * The Class CloverParser.
40   */
41  public class CloverParser extends AbstractXmlEventParser {
42  
43      /** The source. */
44      private Source source;
45  
46      /** The package name. */
47      private String packageName;
48  
49      /**
50       * Instantiates a new clover parser.
51       *
52       * @param coverageFile
53       *            the coverage file
54       * @param sourceLoader
55       *            the source loader
56       */
57      public CloverParser(final File coverageFile, final SourceLoader sourceLoader) {
58          super(coverageFile, sourceLoader);
59      }
60  
61      @Override
62      protected void onEvent(final XMLStreamReader xml, final SourceCallback callback)
63              throws XMLStreamException, ProcessingException, IOException {
64          if (this.isStartElement(xml, "package")) {
65              this.packageName = xml.getAttributeValue(null, "name");
66          } else if (this.isStartElement(xml, "file") && this.packageName != null) {
67              final var sourceFile = this.getSourceFile(xml.getAttributeValue(null, "name"));
68              this.source = this.loadSource(sourceFile);
69          } else if (this.isStartElement(xml, "line") && this.source != null) {
70              // lines can be "method", "stmt", or "cond"
71              final var type = xml.getAttributeValue(null, "type");
72              var coverage = 0;
73              if ("method".equals(type) || "stmt".equals(type)) {
74                  coverage = Integer.parseInt(xml.getAttributeValue(null, "count")) == 0 ? 0 : 1;
75              } else if ("cond".equals(type)) {
76                  final var falseCount = Integer.parseInt(xml.getAttributeValue(null, "falsecount"));
77                  final var trueCount = Integer.parseInt(xml.getAttributeValue(null, "truecount"));
78                  coverage = trueCount == 0 || falseCount == 0 ? 0 : 1;
79              }
80              final var lineNumber = Integer.parseInt(xml.getAttributeValue(null, "num"));
81              this.source.addCoverage(lineNumber, coverage);
82          } else if (this.isEndElement(xml, "file") && this.source != null) {
83              callback.onSource(this.source);
84              this.source = null;
85          } else if (this.isEndElement(xml, "package")) {
86              this.packageName = null;
87          }
88      }
89  
90      /**
91       * Gets the source file.
92       *
93       * @param fileName
94       *            the file name
95       *
96       * @return the source file
97       */
98      private String getSourceFile(final String fileName) {
99          return this.packageName.replace('.', '/') + "/" + fileName;
100     }
101 }