001/* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2008-2012 SonarSource 004 * mailto:contact AT sonarsource DOT com 005 * 006 * Sonar is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 3 of the License, or (at your option) any later version. 010 * 011 * Sonar is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with Sonar; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 019 */ 020package org.sonar.plugins.pmd; 021 022import com.google.common.collect.ImmutableList; 023import com.google.common.collect.Iterables; 024import net.sourceforge.pmd.IRuleViolation; 025import org.sonar.api.BatchExtension; 026import org.sonar.api.batch.SensorContext; 027import org.sonar.api.resources.JavaFile; 028import org.sonar.api.resources.ProjectFileSystem; 029import org.sonar.api.resources.Resource; 030import org.sonar.api.rules.Rule; 031import org.sonar.api.rules.RuleFinder; 032import org.sonar.api.rules.Violation; 033 034import java.io.File; 035import java.util.List; 036 037public class PmdViolationToRuleViolation implements BatchExtension { 038 private final ProjectFileSystem projectFileSystem; 039 private final RuleFinder ruleFinder; 040 041 public PmdViolationToRuleViolation(ProjectFileSystem projectFileSystem, RuleFinder ruleFinder) { 042 this.projectFileSystem = projectFileSystem; 043 this.ruleFinder = ruleFinder; 044 } 045 046 public Violation toViolation(IRuleViolation pmdViolation, SensorContext context) { 047 Resource resource = findResourceFor(pmdViolation); 048 if (context.getResource(resource) == null) { 049 return null; // Save violations only for existing resources 050 } 051 052 Rule rule = findRuleFor(pmdViolation); 053 if (rule == null) { 054 return null; // Save violations only for enabled rules 055 } 056 057 int lineId = pmdViolation.getBeginLine(); 058 String message = pmdViolation.getDescription(); 059 060 return Violation.create(rule, resource).setLineId(lineId).setMessage(message); 061 } 062 063 private Resource findResourceFor(IRuleViolation violation) { 064 List<File> allSources = ImmutableList.copyOf(Iterables.concat(projectFileSystem.getSourceDirs(), projectFileSystem.getTestDirs())); 065 066 return JavaFile.fromAbsolutePath(violation.getFilename(), allSources, true); 067 } 068 069 private Rule findRuleFor(IRuleViolation violation) { 070 String ruleKey = violation.getRule().getName(); 071 Rule rule = ruleFinder.findByKey(PmdConstants.REPOSITORY_KEY, ruleKey); 072 if (rule != null) { 073 return rule; 074 } 075 return ruleFinder.findByKey(PmdConstants.TEST_REPOSITORY_KEY, ruleKey); 076 } 077}