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

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

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 org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;
import org.w3c.dom.Document;

import ch.hortis.sonar.model.Metrics;

/**
 * Chidamber and Kemerer metrics collector
 */
public class CKCollector extends BaseMetricCollector implements MetricsCollector {

  private Document ckjmDocument;
  private XPath ckjmXpath;
  
  public Collection collectMeasures() throws MojoExecutionException {
    
    List metrics = new ArrayList();
    try {
    
      Integer classesCount = ( (Number)ckjmXpath.evaluate("count(/ckjm/class)", ckjmDocument, XPathConstants.NUMBER ) ).intValue();
      
      Integer wmcCount = ( (Number)ckjmXpath.evaluate("sum(/ckjm/class/wmc/text())", ckjmDocument, XPathConstants.NUMBER ) ).intValue();
      metrics.add( new MetricData( Metrics.CK_WMC, scaleValue( wmcCount.doubleValue() / classesCount ) ) );
      
      Integer ditCount = ( (Number)ckjmXpath.evaluate("sum(/ckjm/class/dit/text())", ckjmDocument, XPathConstants.NUMBER ) ).intValue();
      metrics.add( new MetricData( Metrics.CK_DIT, scaleValue( ditCount.doubleValue() / classesCount ) ) );
      
      Integer nocCount = ( (Number)ckjmXpath.evaluate("sum(/ckjm/class/noc/text())", ckjmDocument, XPathConstants.NUMBER ) ).intValue();
      metrics.add( new MetricData( Metrics.CK_NOC, scaleValue( nocCount.doubleValue() / classesCount ) ) );
      
      Integer cboCount = ( (Number)ckjmXpath.evaluate("sum(/ckjm/class/cbo/text())", ckjmDocument, XPathConstants.NUMBER ) ).intValue();
      metrics.add( new MetricData( Metrics.CK_CBO, scaleValue( cboCount.doubleValue() / classesCount ) ) );
      
      Integer rfcCount = ( (Number)ckjmXpath.evaluate("sum(/ckjm/class/rfc/text())", ckjmDocument, XPathConstants.NUMBER ) ).intValue();
      metrics.add( new MetricData( Metrics.CK_RFC, scaleValue( rfcCount.doubleValue() / classesCount ) ) );
      
      Integer lcomCount = ( (Number)ckjmXpath.evaluate("sum(/ckjm/class/lcom/text())", ckjmDocument, XPathConstants.NUMBER ) ).intValue();
      metrics.add( new MetricData( Metrics.CK_LCOM, scaleValue( lcomCount.doubleValue() / classesCount ) ) );
      
      Integer caCount = ( (Number)ckjmXpath.evaluate("sum(/ckjm/class/ca/text())", ckjmDocument, XPathConstants.NUMBER ) ).intValue();
      metrics.add( new MetricData( Metrics.CK_CA, scaleValue( caCount.doubleValue() / classesCount ) ) );
      
      Integer npmCount = ( (Number)ckjmXpath.evaluate("sum(/ckjm/class/npm/text())", ckjmDocument, XPathConstants.NUMBER ) ).intValue();
      metrics.add( new MetricData( Metrics.CK_NPM, scaleValue( npmCount.doubleValue() / classesCount ) ) );
    } catch (Exception ex) {
      throw new MojoExecutionException("Error during CK report parsing", ex);
    }
    
    return metrics;
  }

  public void enableLogging( Log log ) {
  }

  public boolean initialize( MavenProject project ) throws MojoExecutionException {
    
    String filename = project.getBuild().getDirectory() + "/ckjm.xml";
    File ckjm = new File( filename );
    if ( ckjm.exists() ) {
      try {
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        ckjmDocument = builder.parse(ckjm);
        ckjmXpath = XPathFactory.newInstance().newXPath();
      } catch (Exception ex) {
        throw new MojoExecutionException("Error during CKJM reports parsing", ex);
      }
      return true;
    }
    return false;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy