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

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

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 ch.hortis.sonar.model.Collectable;
import ch.hortis.sonar.model.File;
import ch.hortis.sonar.model.Rule;
import ch.hortis.sonar.model.RuleFailure;
import ch.hortis.sonar.model.RuleFailureLevel;

public class PMDCollector extends BaseMeasuresCollector {
  private java.io.File pmdFile;
  private XmlReportParser parser;


  public boolean initialize(MavenProject project) {
    pmdFile = findFileFromBuildDirectory(project, "pmd.xml");
    boolean ok = false;
    if (pmdFile != null) {
      parser = new XmlReportParser();
      parser.parse(pmdFile);
      ok = true;
    }
    return ok;
  }

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

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

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy