2 * Sonar, open source software quality management tool.
3 * Copyright (C) 2008-2011 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * Sonar is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * Sonar is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Sonar; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
20 package org.sonar.plugins.cobertura.api;
22 import static java.util.Locale.ENGLISH;
23 import static org.sonar.api.utils.ParsingUtils.parseNumber;
24 import static org.sonar.api.utils.ParsingUtils.scaleValue;
26 import org.apache.commons.io.FilenameUtils;
27 import org.apache.commons.lang.StringUtils;
28 import org.codehaus.staxmate.in.SMHierarchicCursor;
29 import org.codehaus.staxmate.in.SMInputCursor;
30 import org.sonar.api.batch.SensorContext;
31 import org.sonar.api.measures.CoreMetrics;
32 import org.sonar.api.measures.Measure;
33 import org.sonar.api.measures.PersistenceMode;
34 import org.sonar.api.measures.PropertiesBuilder;
35 import org.sonar.api.resources.Resource;
36 import org.sonar.api.utils.StaxParser;
37 import org.sonar.api.utils.XmlParserException;
40 import java.text.ParseException;
41 import java.util.ArrayList;
42 import java.util.HashMap;
43 import java.util.List;
46 import javax.xml.stream.XMLStreamException;
51 public abstract class AbstractCoberturaParser {
52 public void parseReport(File xmlFile, final SensorContext context) {
54 StaxParser parser = new StaxParser(new StaxParser.XmlStreamHandler() {
56 public void stream(SMHierarchicCursor rootCursor) throws XMLStreamException {
59 collectPackageMeasures(rootCursor.descendantElementCursor("package"), context);
60 } catch (ParseException e) {
61 throw new XMLStreamException(e);
65 parser.parse(xmlFile);
66 } catch (XMLStreamException e) {
67 throw new XmlParserException(e);
71 private void collectPackageMeasures(SMInputCursor pack, SensorContext context) throws ParseException, XMLStreamException {
72 while (pack.getNext() != null) {
73 Map<String, FileData> fileDataPerFilename = new HashMap<String, FileData>();
74 collectFileMeasures(pack.descendantElementCursor("class"), fileDataPerFilename);
75 for (FileData cci : fileDataPerFilename.values()) {
76 if (isFileExist(context, cci.getFile())) {
77 for (Measure measure : cci.getMeasures()) {
78 context.saveMeasure(cci.getFile(), measure);
85 private boolean isFileExist(SensorContext context, Resource<?> file) {
86 return context.getResource(file) != null;
89 private void collectFileMeasures(SMInputCursor clazz, Map<String, FileData> dataPerFilename) throws ParseException, XMLStreamException {
90 while (clazz.getNext() != null) {
91 String fileName = FilenameUtils.removeExtension(clazz.getAttrValue("filename"));
92 fileName = fileName.replace('/', '.').replace('\\', '.');
93 FileData data = dataPerFilename.get(fileName);
95 data = new FileData(getResource(fileName));
96 dataPerFilename.put(fileName, data);
98 collectFileData(clazz, data);
102 private void collectFileData(SMInputCursor clazz, FileData data) throws ParseException, XMLStreamException {
103 SMInputCursor line = clazz.childElementCursor("lines").advance().childElementCursor("line");
104 while (line.getNext() != null) {
105 String lineId = line.getAttrValue("number");
106 data.addLine(lineId, (int) parseNumber(line.getAttrValue("hits"), ENGLISH));
108 String isBranch = line.getAttrValue("branch");
109 String text = line.getAttrValue("condition-coverage");
110 if (StringUtils.equals(isBranch, "true") && StringUtils.isNotBlank(text)) {
111 String[] conditions = StringUtils.split(StringUtils.substringBetween(text, "(", ")"), "/");
112 data.addConditionLine(lineId, Integer.parseInt(conditions[0]), Integer.parseInt(conditions[1]), StringUtils.substringBefore(text, " "));
117 private class FileData {
119 private int lines = 0;
120 private int conditions = 0;
121 private int coveredLines = 0;
122 private int coveredConditions = 0;
124 private Resource<?> file;
125 private PropertiesBuilder<String, Integer> lineHitsBuilder = new PropertiesBuilder<String, Integer>(CoreMetrics.COVERAGE_LINE_HITS_DATA);
126 private PropertiesBuilder<String, String> branchHitsBuilder = new PropertiesBuilder<String, String>(CoreMetrics.BRANCH_COVERAGE_HITS_DATA);
128 public void addLine(String lineId, int lineHits) {
133 lineHitsBuilder.add(lineId, lineHits);
136 public void addConditionLine(String lineId, int coveredConditions, int conditions, String label) {
137 this.conditions += conditions;
138 this.coveredConditions += coveredConditions;
139 branchHitsBuilder.add(lineId, label);
142 public FileData(Resource<?> file) {
146 public List<Measure> getMeasures() {
147 List<Measure> measures = new ArrayList<Measure>();
149 measures.add(new Measure(CoreMetrics.COVERAGE, calculateCoverage(coveredLines + coveredConditions, lines + conditions)));
151 measures.add(new Measure(CoreMetrics.LINE_COVERAGE, calculateCoverage(coveredLines, lines)));
152 measures.add(new Measure(CoreMetrics.LINES_TO_COVER, (double) lines));
153 measures.add(new Measure(CoreMetrics.UNCOVERED_LINES, (double) lines - coveredLines));
154 measures.add(lineHitsBuilder.build().setPersistenceMode(PersistenceMode.DATABASE));
156 if (conditions > 0) {
157 measures.add(new Measure(CoreMetrics.BRANCH_COVERAGE, calculateCoverage(coveredConditions, conditions)));
158 measures.add(new Measure(CoreMetrics.CONDITIONS_TO_COVER, (double) conditions));
159 measures.add(new Measure(CoreMetrics.UNCOVERED_CONDITIONS, (double) conditions - coveredConditions));
160 measures.add(branchHitsBuilder.build().setPersistenceMode(PersistenceMode.DATABASE));
166 public Resource<?> getFile() {
171 private double calculateCoverage(int coveredElements, int elements) {
173 return scaleValue(100.0 * ((double) coveredElements / (double) elements));
178 protected abstract Resource<?> getResource(String fileName);