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 org.eluder.coveralls.maven.plugin.ProcessingException;
27  import org.eluder.coveralls.maven.plugin.domain.Source;
28  import org.eluder.coveralls.maven.plugin.source.SourceCallback;
29  import org.eluder.coveralls.maven.plugin.source.SourceLoader;
30  
31  import javax.xml.stream.XMLStreamException;
32  import javax.xml.stream.XMLStreamReader;
33  import java.io.File;
34  import java.io.IOException;
35  
36  public class CloverParser extends AbstractXmlEventParser {
37  
38      private Source source;
39      private String packageName;
40  
41      public CloverParser(final File coverageFile, final SourceLoader sourceLoader) {
42          super(coverageFile, sourceLoader);
43      }
44  
45      @Override
46      protected void onEvent(final XMLStreamReader xml, final SourceCallback callback) throws XMLStreamException, ProcessingException, IOException {
47          if (isStartElement(xml, "package")) {
48              this.packageName = xml.getAttributeValue(null, "name");
49          } else if (isStartElement(xml, "file") && packageName != null) {
50              String sourceFile = getSourceFile(xml.getAttributeValue(null, "name"));
51              this.source = loadSource(sourceFile);
52          } else if (isStartElement(xml, "line") && this.source != null) {
53              // lines can be "method", "stmt", or "cond"
54              String type = xml.getAttributeValue(null, "type");
55              int coverage = 0;
56              if ("method".equals(type) || "stmt".equals(type)) {
57                  coverage = (Integer.parseInt(xml.getAttributeValue(null, "count")) == 0) ? 0 : 1;
58              } else if ("cond".equals(type)) {
59                  int falseCount = Integer.parseInt(xml.getAttributeValue(null, "falsecount"));
60                  int trueCount = Integer.parseInt(xml.getAttributeValue(null, "truecount"));
61                  coverage = (trueCount == 0 || falseCount == 0) ? 0 : 1;
62              }
63              int lineNumber = Integer.parseInt(xml.getAttributeValue(null, "num"));
64              this.source.addCoverage(lineNumber, coverage);
65          } else if (isEndElement(xml, "file") && this.source != null) {
66              callback.onSource(this.source);
67              this.source = null;
68          } else if (isEndElement(xml, "package")) {
69              this.packageName = null;
70          }
71      }
72  
73      private String getSourceFile(final String fileName) {
74          return this.packageName.replace('.', '/') + "/" + fileName;
75      }
76  }