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 com.google.common.collect.Maps;
23 import org.apache.commons.io.FilenameUtils;
24 import org.apache.commons.lang.StringUtils;
25 import org.codehaus.staxmate.in.SMHierarchicCursor;
26 import org.codehaus.staxmate.in.SMInputCursor;
27 import org.sonar.api.batch.SensorContext;
28 import org.sonar.api.measures.CoverageMeasuresBuilder;
29 import org.sonar.api.measures.Measure;
30 import org.sonar.api.resources.Resource;
31 import org.sonar.api.utils.StaxParser;
32 import org.sonar.api.utils.XmlParserException;
34 import javax.xml.stream.XMLStreamException;
36 import java.text.ParseException;
39 import static java.util.Locale.ENGLISH;
40 import static org.sonar.api.utils.ParsingUtils.parseNumber;
45 public abstract class AbstractCoberturaParser {
47 public void parseReport(File xmlFile, final SensorContext context) {
49 StaxParser parser = new StaxParser(new StaxParser.XmlStreamHandler() {
51 public void stream(SMHierarchicCursor rootCursor) throws XMLStreamException {
54 collectPackageMeasures(rootCursor.descendantElementCursor("package"), context);
55 } catch (ParseException e) {
56 throw new XMLStreamException(e);
60 parser.parse(xmlFile);
61 } catch (XMLStreamException e) {
62 throw new XmlParserException(e);
66 private void collectPackageMeasures(SMInputCursor pack, SensorContext context) throws ParseException, XMLStreamException {
67 while (pack.getNext() != null) {
68 Map<String, CoverageMeasuresBuilder> builderByFilename = Maps.newHashMap();
69 collectFileMeasures(pack.descendantElementCursor("class"), builderByFilename);
70 for (Map.Entry<String, CoverageMeasuresBuilder> entry : builderByFilename.entrySet()) {
71 String filename = sanitizeFilename(entry.getKey());
72 Resource file = getResource(filename);
73 if (fileExists(context, file)) {
74 for (Measure measure : entry.getValue().createMeasures()) {
75 System.out.println(file + " -> " + measure);
76 context.saveMeasure(file, measure);
83 private boolean fileExists(SensorContext context, Resource file) {
84 return context.getResource(file) != null;
87 private void collectFileMeasures(SMInputCursor clazz, Map<String, CoverageMeasuresBuilder> builderByFilename) throws ParseException, XMLStreamException {
88 while (clazz.getNext() != null) {
89 String fileName = clazz.getAttrValue("filename");
90 CoverageMeasuresBuilder builder = builderByFilename.get(fileName);
92 builder = CoverageMeasuresBuilder.create();
93 builderByFilename.put(fileName, builder);
95 collectFileData(clazz, builder);
99 private void collectFileData(SMInputCursor clazz, CoverageMeasuresBuilder builder) throws ParseException, XMLStreamException {
100 SMInputCursor line = clazz.childElementCursor("lines").advance().childElementCursor("line");
101 while (line.getNext() != null) {
102 int lineId = Integer.parseInt(line.getAttrValue("number"));
103 builder.setHits(lineId, (int) parseNumber(line.getAttrValue("hits"), ENGLISH));
105 String isBranch = line.getAttrValue("branch");
106 String text = line.getAttrValue("condition-coverage");
107 if (StringUtils.equals(isBranch, "true") && StringUtils.isNotBlank(text)) {
108 String[] conditions = StringUtils.split(StringUtils.substringBetween(text, "(", ")"), "/");
109 builder.setConditions(lineId, Integer.parseInt(conditions[1]), Integer.parseInt(conditions[0]));
114 private String sanitizeFilename(String s) {
115 String fileName = FilenameUtils.removeExtension(s);
116 fileName = fileName.replace('/', '.').replace('\\', '.');
120 protected abstract Resource getResource(String fileName);