Package ch.hortis.sonar.mvn.mc

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

/*
* 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.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.Element;
import org.w3c.dom.NodeList;

import java.util.ArrayList;
import java.util.List;

public class CheckstyleCollector extends BaseMeasuresCollector {

  private XmlReportParser reportParser;
  private List<String> sourceDirs;

  public boolean initialize( MavenProject project ) throws MojoExecutionException {
    java.io.File report = findCheckstyleXmlReportFile( project );
    boolean ok = false;
    if ( report != null ) {
      ok = true;
      sourceDirs = project.getCompileSourceRoots();
      try {
        reportParser = new XmlReportParser();
        reportParser.parseDocument( report );
      } catch (Exception ex) {
        throw new MojoExecutionException( "Error during Checkstyle reports parsing", ex );
      }
    }
    return ok;
  }

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

  public List<RuleFailure> collectRuleFailures() throws MojoExecutionException {
    List<RuleFailure> failures = new ArrayList<RuleFailure>();
    NodeList files = reportParser.executeXPathNodeList( "/checkstyle/file" );
    if ( files != null ) {
      for (int i = 0; i < files.getLength(); i++) {
        Element element = (Element) files.item( i );
        String checkstyleName = element.getAttribute( "name" );

        List<Element> errors = reportParser.getChildElements( element, "error" );
        for (Element error : errors) {
          RuleFailure failure = new RuleFailure();
          String source = error.getAttribute( "source" );
          if ( rulesService.getRuleByPluginKey( source ) == null ) {
            continue;
          }
          failure.setRule( rulesService.getRuleByPluginKey( source ) );
          failure.setFile( getFile( checkstyleName ) );
          failure.setMessage( error.getAttribute( "message" ) );
          String line = error.getAttribute( "line" );
          if ( line != null && !"".equals( line ) ) {
            failure.addParameter( "line", Double.valueOf( line ) );
          }
          String column = error.getAttribute( "column" );
          if ( column != null && !"".equals( column ) ) {
            failure.addParameter( "column", Double.valueOf( column ) );
          }
          String severity = error.getAttribute( "severity" );
          if ( "error".equals( severity ) ) {
            failure.setLevel( RuleFailureLevel.ERROR );
          } else if ( "warning".equals( severity ) ) {
            failure.setLevel( RuleFailureLevel.WARNING );
          } else {
            failure.setLevel( RuleFailureLevel.INFO );
          }

          failures.add( failure );
        }
      }
    }
    return failures;
  }

  private java.io.File findCheckstyleXmlReportFile( final MavenProject project ) {
    String xmlOutputDirectory = project.getBuild().getDirectory() + "/checkstyle-result.xml";
    java.io.File file = new java.io.File( xmlOutputDirectory );
    if ( !file.exists() ) {
      file = null;
    }
    return file;
  }

  private File getFile( String checkstyleFilename ) {
    checkstyleFilename = checkstyleFilename.replace( '\\', '/' );
    String filename = StringUtils.substringAfterLast( checkstyleFilename, "/" );
    String namespace = StringUtils.substringBeforeLast( checkstyleFilename, filename );
    for (String sourceDir : sourceDirs) {
      sourceDir = sourceDir.replace( '\\', '/' );
      if ( !sourceDir.endsWith( "/" ) ) {
        sourceDir = sourceDir += "/";
      }
      if ( namespace.startsWith( sourceDir ) || namespace.contains( sourceDir )  ) {
        namespace = StringUtils.substringAfter( namespace, sourceDir ).replace( '/', '.');
        if ( namespace.endsWith( "." ) ) {
          namespace = StringUtils.chop( namespace );
        }
        break;
      }
    }

    return getFilesRepository().getFile( namespace, filename );
  }
}
TOP

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

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.