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

ch.hortis.sonar.mvn.mc.CheckstyleCollector 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.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 sourceDirs;

  public boolean initialize(MavenProject project) {
    java.io.File report = findFileFromBuildDirectory(project, "checkstyle-result.xml");
    boolean ok = false;
    if (report != null) {
      ok = true;
      sourceDirs = project.getCompileSourceRoots();
      reportParser = new XmlReportParser();
      reportParser.parse(report);
    }
    return ok;
  }

  public List collect() throws MojoExecutionException {
    return collectRuleFailures();
  }

  public List collectRuleFailures() throws MojoExecutionException {
    List failures = new ArrayList(4096);
    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 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 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);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy