Package ch.hortis.sonar.mvn.mc

Source Code of ch.hortis.sonar.mvn.mc.PMDCollector

/*
* 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.File;
import ch.hortis.sonar.model.Metric;
import ch.hortis.sonar.model.Metrics;
import ch.hortis.sonar.model.ProjectMeasure;
import ch.hortis.sonar.model.Rule;
import ch.hortis.sonar.model.RuleFailure;
import ch.hortis.sonar.model.RuleFailureLevel;
import org.apache.commons.lang.StringUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
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.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class PMDCollector extends BaseMeasuresCollector {
  private java.io.File pmdFile;
  private Document pmdDocument;
  private XPath pmdXpath;
  private XmlReportParser parser;


  public boolean initialize( MavenProject project ) throws MojoExecutionException {
    pmdFile = findPMDReportFile( project, "pmd.xml" );
    boolean ok = false;
    try {
      DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
      if ( pmdFile != null ) {
        pmdDocument = builder.parse( pmdFile );
        pmdXpath = XPathFactory.newInstance().newXPath();
        parser = new XmlReportParser();
        parser.parseDocument( pmdFile );
        ok = true;
      }

    } catch (Exception ex) {
      throw new MojoExecutionException( "Error during PMD reports parsing", ex );
    }
    return ok;
  }

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

  public List<RuleFailure> collectRuleFailures() throws MojoExecutionException {
    ArrayList<RuleFailure> failures = new ArrayList<RuleFailure>();
    if ( pmdFile != null ) {
      NodeList files = parser.executeXPathNodeList( "/pmd/file" );
      for (int i = 0; i < files.getLength(); i++) {
        Element element = (Element) files.item( i );
        String name = element.getAttribute( "name" );
        String filename = StringUtils.substringAfterLast( name, "/" );
        if ( filename == null || "".equals( filename ) ) {
          filename = StringUtils.substringAfterLast( name, "\\" );
        }
        List<Element> violations = parser.getChildElements( element, "violation" );
        for (Element violation : violations) {
          RuleFailure failure = new RuleFailure();
          Rule rule = getRuleService().getRuleByPluginKey( violation.getAttribute( "rule" ) );
          if ( rule == null ) {
            continue;
          }
          failure.setRule( rule );

          File file = getFilesRepository().getFile( violation.getAttribute( "package" ), filename );
          failure.setFile( file );
          String line = violation.getAttribute( "line" );
          if ( line != null && !"".equals( line ) ) {
            failure.addParameter( "line", Double.valueOf( line ) );
          }
          failure.setMessage( violation.getFirstChild().getNodeValue() );
          int priority = Integer.parseInt( violation.getAttribute( "priority" ) );
          if ( priority <= 2 ) {
            failure.setLevel( RuleFailureLevel.ERROR );
          } else if ( priority == 3 || priority == 4 ) {
            failure.setLevel( RuleFailureLevel.WARNING );
          } else {
            failure.setLevel( RuleFailureLevel.INFO );
          }
          failures.add( failure );
        }
      }
    }
    return failures;
  }

  public List<ProjectMeasure> collectProjectMeasures() throws MojoExecutionException {
    List<ProjectMeasure> measures = new ArrayList<ProjectMeasure>();
    try {
      if ( pmdFile != null ) {
        measures.addAll( parsePMD( pmdXpath, pmdDocument ) );
      }

    } catch (Exception ex) {
      throw new MojoExecutionException( "Error during PMD reports parsing", ex );
    }
    return measures;
  }

  private Collection<ProjectMeasure> parsePMD( XPath xpath, Node root ) throws XPathExpressionException {
    return new ArrayList<ProjectMeasure>();
  }


  private java.io.File findPMDReportFile( final MavenProject project, String fileName ) {
    String pmdOutputDirectory = project.getBuild().getDirectory() + "/" + fileName;
    java.io.File file = new java.io.File( pmdOutputDirectory );
    if ( file.exists() ) {
      return file;
    }
    return null;
  }
}
TOP

Related Classes of ch.hortis.sonar.mvn.mc.PMDCollector

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.