org.sonar.plugins.groovy.cobertura.CoberturaReportParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sonar-groovy-plugin Show documentation
Show all versions of sonar-groovy-plugin Show documentation
Enables analysis of Groovy projects.
/*
* Sonar Groovy Plugin
* Copyright (C) 2010 SonarSource
* [email protected]
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package org.sonar.plugins.groovy.cobertura;
import org.sonar.api.resources.Directory;
import com.google.common.collect.Maps;
import org.apache.commons.lang.StringUtils;
import org.codehaus.staxmate.in.SMHierarchicCursor;
import org.codehaus.staxmate.in.SMInputCursor;
import org.sonar.api.batch.SensorContext;
import org.sonar.api.measures.CoverageMeasuresBuilder;
import org.sonar.api.measures.Measure;
import org.sonar.api.resources.Resource;
import org.sonar.api.utils.StaxParser;
import org.sonar.api.utils.XmlParserException;
import javax.xml.stream.XMLStreamException;
import java.io.File;
import java.text.ParseException;
import java.util.Map;
import static java.util.Locale.ENGLISH;
import static org.sonar.api.utils.ParsingUtils.parseNumber;
public class CoberturaReportParser {
private final SensorContext context;
public CoberturaReportParser(final SensorContext context) {
this.context = context;
}
/**
* Parse a Cobertura xml report and create measures accordingly
*/
public void parseReport(File xmlFile) {
try {
StaxParser parser = new StaxParser(new StaxParser.XmlStreamHandler() {
public void stream(SMHierarchicCursor rootCursor) throws XMLStreamException {
rootCursor.advance();
collectPackageMeasures(rootCursor.descendantElementCursor("package"));
}
});
parser.parse(xmlFile);
} catch (XMLStreamException e) {
throw new XmlParserException(e);
}
}
private void collectPackageMeasures(SMInputCursor pack) throws XMLStreamException {
while (pack.getNext() != null) {
Map builderByFilename = Maps.newHashMap();
collectFileMeasures(pack.descendantElementCursor("class"), builderByFilename);
for (Map.Entry entry : builderByFilename.entrySet()) {
org.sonar.api.resources.File file = new org.sonar.api.resources.File(entry.getKey());
if (fileExists(file)) {
for (Measure measure : entry.getValue().createMeasures()) {
context.saveMeasure(file, measure);
}
}
}
}
}
private boolean fileExists(Resource file) {
return context.getResource(file) != null;
}
private void collectFileMeasures(SMInputCursor clazz, Map builderByFilename) throws XMLStreamException {
while (clazz.getNext() != null) {
String fileName = clazz.getAttrValue("filename");
CoverageMeasuresBuilder builder = builderByFilename.get(fileName);
if (builder == null) {
builder = CoverageMeasuresBuilder.create();
builderByFilename.put(fileName, builder);
}
collectFileData(clazz, builder);
}
}
private void collectFileData(SMInputCursor clazz, CoverageMeasuresBuilder builder) throws XMLStreamException {
SMInputCursor line = clazz.childElementCursor("lines").advance().childElementCursor("line");
while (line.getNext() != null) {
int lineId = Integer.parseInt(line.getAttrValue("number"));
try {
builder.setHits(lineId, (int) parseNumber(line.getAttrValue("hits"), ENGLISH));
} catch (ParseException e) {
throw new XmlParserException(e);
}
String isBranch = line.getAttrValue("branch");
String text = line.getAttrValue("condition-coverage");
if (StringUtils.equals(isBranch, "true") && StringUtils.isNotBlank(text)) {
String[] conditions = StringUtils.split(StringUtils.substringBetween(text, "(", ")"), "/");
builder.setConditions(lineId, Integer.parseInt(conditions[1]), Integer.parseInt(conditions[0]));
}
}
}
}