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.Collectable;
import ch.hortis.sonar.model.Metrics;
import ch.hortis.sonar.model.ProjectMeasure;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ChangeLogCollector extends BaseMeasuresCollector {
  private Document document;
  private XPath xpath;

  public boolean initialize(MavenProject project) {
    boolean ok = false;
    File report = findFileFromBuildDirectory( project, "changelog.xml" );
    if ( report != null ) {
      ok=true;

      try {
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        document = builder.parse( report );
        xpath = XPathFactory.newInstance().newXPath();
      } catch (SAXException e) {
        throw new XmlParserException("can not parse the file " + report.getAbsolutePath(), e);
      } catch (IOException e) {
        throw new XmlParserException("can not parse the file " + report.getAbsolutePath(), e);
      } catch (ParserConfigurationException e) {
        throw new XmlParserException("can not instanciate the XML parser" + report.getAbsolutePath(), e);
      }
    }
    return ok;
  }


  public List collect() throws MojoExecutionException {
    ArrayList result = new ArrayList();
    result.addAll( collectProjectMeasures() );
    return result;
  }

  public List collectProjectMeasures() 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.asMeasures();
  }

  
  private class CommitReportDataContainer {
    private double commits;
    private double fileCommits;

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

    private List asMeasures() {
      List measures = new ArrayList();
      ProjectMeasure measure = new ProjectMeasure();
      measure.setMetric( loadMetric( Metrics.CHANGELOG_COMMITS ));
      measure.setValue( commits );
      measures.add( measure );

      measure = new ProjectMeasure();
      measure.setMetric( loadMetric( Metrics.CHANGELOG_FILE_COMMITS ) );
      measure.setValue( fileCommits );
      measures.add( measure );

      return measures;
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy