All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ch.hortis.sonar.mvn.mc.ChangeLogCollector 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.XPathFactory;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;

/**
 * Maven changelog reports collector
 */
public class ChangeLogCollector implements MetricsCollector {

  private Document document;
  private XPath xpath;

  public void enableLogging( Log log ) {
  }

  public boolean initialize( MavenProject project ) throws MojoExecutionException {
    boolean ok = true;
    File report = findChangelogReportFile( project );
    if ( report == null ) {
      ok = false;
    } else {
      try {
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        document = builder.parse( report );
        xpath = XPathFactory.newInstance().newXPath();
      } catch (Exception ex) {
        throw new MojoExecutionException( "Error during Changelog reports parsing", ex );
      }
    }
    return ok;
  }

  public Collection collectMeasures() throws MojoExecutionException {
    CommitReportDataContainer projectContainer = new CommitReportDataContainer();
    try {
      NodeList commitEntry = (NodeList) xpath.evaluate( "//changelog/changeset/changelog-entry", document, XPathConstants.NODESET );
      for (int i = 0; i < commitEntry.getLength(); i++) {
        Node entry = commitEntry.item( i );
        NodeList files = (NodeList) xpath.evaluate( "file", entry, XPathConstants.NODESET );
        projectContainer.cumulate( 1, files.getLength() );
      }
    } catch (Exception ex) {
      throw new MojoExecutionException( "Error during Changelog reports parsing", ex );
    }
    return projectContainer.asMetrics();
  }

  private File findChangelogReportFile( final MavenProject project ) throws MojoExecutionException  {
    String outputDirectory = project.getBuild().getDirectory() + "/changelog.xml";
    File file = new File( outputDirectory );
    if ( !file.exists() ) {
      file = null;
    }
    return file;
  }

  private class CommitReportDataContainer {

    private double commits;
    private double fileCommits;

    private void cumulate( double commits, double fileCommits ) {
      this.commits += commits;
      this.fileCommits += fileCommits;
    }

    private Collection asMetrics() {
      Collection metrics = new ArrayList();
      MetricData metric = new MetricData();
      metric.setMetric( Metrics.CHANGELOG_COMMITS );
      metric.setValue( commits );
      metrics.add( metric );

      metric = new MetricData();
      metric.setMetric( Metrics.CHANGELOG_FILE_COMMITS );
      metric.setValue( fileCommits );
      metrics.add( metric );

      return metrics;
    }
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy