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

ch.hortis.sonar.mvn.mc.JDependsCollector 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 org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;
import org.w3c.dom.Element;

import ch.hortis.sonar.model.Metrics;

import java.io.File;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * JDepends reports collector
 */
public class JDependsCollector extends XmlReportParser implements MetricsCollector {

  public void enableLogging( Log log ) {
  }

  public boolean initialize( MavenProject project ) throws MojoExecutionException {
    File report = findJDependsXmlReportFile( project );
    if (report!=null) {
      parseDocument( report );
      return true;
    }
    return false;
  }

  public Collection collectMeasures() throws MojoExecutionException {
    Collection metrics = new ArrayList();
    double cyclicDependencies = 0;
    double totalClasses = 0;
    double concreteClasses = 0;
    double abstractClasses = 0;
    double ca = 0;
    double ce = 0;
    double i = 0;
    double a = 0;
    double d = 0;
    double v = 0;

    Element packagesRoot = getChildElement( root, "Packages" );
    if ( packagesRoot != null ) {
      List packages = getChildElements( packagesRoot, "Package" );
      for (Element packageEl : packages) {

        if ( getChildElement( packageEl, "error" ) != null ) continue;

        Element statsEl = getChildElement( packageEl, "Stats" );
        if ( statsEl == null ) continue;
        try {
          totalClasses += parseNumber( getChildElementValue( statsEl, "TotalClasses" ) );
          concreteClasses += parseNumber( getChildElementValue( statsEl, "ConcreteClasses" ) );
          abstractClasses += parseNumber( getChildElementValue( statsEl, "AbstractClasses" ) );
          ca += parseNumber( getChildElementValue( statsEl, "Ca" ) );
          ce += parseNumber( getChildElementValue( statsEl, "Ce" ) );
          a += parseNumber( getChildElementValue( statsEl, "A" ) );
          i += parseNumber( getChildElementValue( statsEl, "I" ) );
          d += parseNumber( getChildElementValue( statsEl, "D" ) );
          v += parseNumber( getChildElementValue( statsEl, "V" ) );
        } catch ( ParseException ex ) {
          throw new MojoExecutionException("ParseException during Jdepends report parsing", ex );
        }
      }
    }

    Element cyclesRoot = getChildElement( root, "Cycles" );
    if ( cyclesRoot != null ) {
      List packages = getChildElements( cyclesRoot, "Package" );
      if ( packages.size() > 0 ) {
        for (Element packageEl : packages) {
          // has always 2 xml elems for 1 dependency
          cyclicDependencies += super.getChildElements( packageEl, "Package" ).size() / 2;
        }
      }
    }

    MetricData metric = new MetricData();
    metric.setMetric( Metrics.JDEPEND_TOTAL_CLASSES );
    metric.setValue( totalClasses );
    metrics.add( metric );

    metric = new MetricData();
    metric.setMetric( Metrics.JDEPEND_CONCRETE_CLASSES );
    metric.setValue( concreteClasses );
    metrics.add( metric );

    metric = new MetricData();
    metric.setMetric( Metrics.JDEPEND_ABSTRACT_CLASSES );
    metric.setValue( abstractClasses );
    metrics.add( metric );

    metric = new MetricData();
    metric.setMetric( Metrics.JDEPEND_AFFERENT_COUPLINGS );
    metric.setValue( ca );
    metrics.add( metric );

    metric = new MetricData();
    metric.setMetric( Metrics.JDEPEND_EFFERENT_COUPLINGS );
    metric.setValue( ce );
    metrics.add( metric );

    metric = new MetricData();
    metric.setMetric( Metrics.JDEPEND_ABSTRACTNESS );
    metric.setValue( a );

    metric = new MetricData();
    metric.setMetric( Metrics.JDEPEND_INSTABILITY );
    metric.setValue( i );
    metrics.add( metric );

    metric = new MetricData();
    metric.setMetric( Metrics.JDEPEND_DISTANCE );
    metric.setValue( d );
    metrics.add( metric );

    metric = new MetricData();
    metric.setMetric( Metrics.JDEPEND_VOLATILITY );
    metric.setValue( v );
    metrics.add( metric );

    metric = new MetricData();
    metric.setMetric( Metrics.JDEPEND_PACKAGE_DEPENDENCY_CYCLES );
    metric.setValue( cyclicDependencies );
    metrics.add( metric );

    return metrics;
  }

  private File findJDependsXmlReportFile( final MavenProject project )
      throws MojoExecutionException {
    String outputDirectory = project.getBuild().getDirectory();
    String tempFileName = "jdepend-report.xml";
    File result = new File( outputDirectory + "/" + tempFileName );
    if ( !result.exists() ) {
      result = null;
    }
    return result;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy