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

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

public class JavaNCSSCollector extends XmlReportParser implements MetricsCollector {

  public void enableLogging( Log log ) {
  }

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

  public Collection collectMeasures() throws MojoExecutionException {
    Collection metrics = new ArrayList();
    try {
      metrics.add( new MetricData( Metrics.NCSS_PACKAGES, parseNumber( executeXPath( "count(/javancss/packages/package)" ) ) ) );
      metrics.add( new MetricData( Metrics.NCSS_NCSS, parseNumber( executeXPath( "/javancss/packages/total/ncss" ) ) ) );
      metrics.add( new MetricData( Metrics.NCSS_CYCLOMATIC_COMPLEXITY, parseNumber( executeXPath( "sum(/javancss/functions/function/ccn/text())" ) ) ) );
      metrics.add( new MetricData( Metrics.NCSS_CLASSES, parseNumber( executeXPath( "/javancss/packages/total/classes" ) ) ) );
      metrics.add( new MetricData( Metrics.NCSS_FUNCTIONS, parseNumber( executeXPath( "/javancss/packages/total/functions" ) ) ) );
    } catch ( ParseException ex ) {
      throw new MojoExecutionException("ParseException during JavaNCSS report parsing", ex );
    }
    return metrics;
  }

  private File findJavaNCSSXmlReportFile( final MavenProject project ) {
    String xmlOutputDirectory = project.getBuild().getDirectory();
    String tempFileName = "javancss-raw-report.xml";

    File result = new File( xmlOutputDirectory + "/" + tempFileName );
    if ( !result.exists() ) {
      result = null;
    }
    return result;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy