ch.hortis.sonar.mvn.mc.CheckstyleCollector Maven / Gradle / Ivy
/*
* This program is copyright (c) 2007 Hortis-GRC SA.
*
* This file is part of Sonar.
* Sonar is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Sonar 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
package ch.hortis.sonar.mvn.mc;
import ch.hortis.sonar.model.Metrics;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
public class CheckstyleCollector implements MetricsCollector {
private Document document;
private XPath xpath;
public void enableLogging( Log log ) {
}
public boolean initialize( MavenProject project ) throws MojoExecutionException {
File report = findCheckstyleXmlReportFile( project );
boolean ok = false;
if (report!=null) {
ok = true;
try {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
document = builder.parse( report );
xpath = XPathFactory.newInstance().newXPath();
} catch (Exception ex) {
throw new MojoExecutionException( "Error during Checkstyle reports parsing", ex );
}
}
return ok;
}
public Collection collectMeasures() throws MojoExecutionException {
try {
ReportDataContainer container = new ReportDataContainer();
parseReport( xpath, document, container );
Collection metrics = new ArrayList();
metrics.add( container.buildMetrics( Metrics.CHECKSTYLE_ERRORS, container.errors ) );
metrics.add( container.buildMetrics( Metrics.CHECKSTYLE_WARNINGS, container.warnings ) );
metrics.add( container.buildMetrics( Metrics.CHECKSTYLE_INFO, container.infos ) );
return metrics;
} catch (Exception ex) {
throw new MojoExecutionException( "Error during Checkstyle reports parsing", ex );
}
}
private File findCheckstyleXmlReportFile( final MavenProject project ) throws MojoExecutionException {
String xmlOutputDirectory = project.getBuild().getDirectory() + "/checkstyle-result.xml";
File file = new File( xmlOutputDirectory );
if ( !file.exists() ) {
file = null;
}
return file;
}
private void parseReport( XPath xpath, Node root, ReportDataContainer container ) throws XPathExpressionException {
NodeList errors = (NodeList) xpath.evaluate( "//error[@severity='error']", root, XPathConstants.NODESET );
NodeList warnings = (NodeList) xpath.evaluate( "//error[@severity='warning']", root, XPathConstants.NODESET );
NodeList infos = (NodeList) xpath.evaluate( "//error[@severity='info']", root, XPathConstants.NODESET );
container.errors = errors.getLength();
container.infos = infos.getLength();
container.warnings = warnings.getLength();
}
private class ReportDataContainer {
private int errors;
private int warnings;
private int infos;
private MetricData buildMetrics( Metrics metric, Integer value ) {
MetricData metricData = new MetricData();
metricData.setMetric( metric );
metricData.setValue( value.doubleValue() );
return metricData;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy