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 */ 020 package org.sonar.plugins.pmd; 021 022 import net.sourceforge.pmd.IRuleViolation; 023 import net.sourceforge.pmd.Report; 024 import org.sonar.api.CoreProperties; 025 import org.sonar.api.batch.Sensor; 026 import org.sonar.api.batch.SensorContext; 027 import org.sonar.api.profiles.RulesProfile; 028 import org.sonar.api.resources.Java; 029 import org.sonar.api.resources.JavaFile; 030 import org.sonar.api.resources.Project; 031 import org.sonar.api.resources.Resource; 032 import org.sonar.api.rules.Rule; 033 import org.sonar.api.rules.RuleFinder; 034 import org.sonar.api.rules.Violation; 035 import org.sonar.api.utils.XmlParserException; 036 037 import java.util.Iterator; 038 039 public class PmdSensor implements Sensor { 040 041 private RulesProfile profile; 042 private RuleFinder rulesFinder; 043 private PmdExecutor executor; 044 045 public PmdSensor(RulesProfile profile, RuleFinder rulesFinder, PmdExecutor executor) { 046 this.profile = profile; 047 this.rulesFinder = rulesFinder; 048 this.executor = executor; 049 } 050 051 public void analyse(Project project, SensorContext context) { 052 try { 053 Report report = executor.execute(); 054 analyseReport(report, project, context); 055 056 } catch (Exception e) { 057 // TOFIX 058 throw new XmlParserException(e); 059 } 060 } 061 062 private void analyseReport(Report report, Project project, SensorContext context) { 063 Iterator<IRuleViolation> pmdViolationIter = report.iterator(); 064 while (pmdViolationIter.hasNext()) { 065 IRuleViolation pmdViolation = pmdViolationIter.next(); 066 int lineId = pmdViolation.getBeginLine(); 067 String ruleKey = pmdViolation.getRule().getName(); 068 String message = pmdViolation.getDescription(); 069 String filename = pmdViolation.getFilename(); 070 Resource resource = JavaFile.fromAbsolutePath(filename, project.getFileSystem().getSourceDirs(), false); 071 // Save violations only for existing resources 072 if (context.getResource(resource) != null) { 073 Rule rule = rulesFinder.findByKey(CoreProperties.PMD_PLUGIN, ruleKey); 074 // Save violations only for enabled rules 075 if (rule != null) { 076 Violation violation = Violation.create(rule, resource).setLineId(lineId).setMessage(message); 077 context.saveViolation(violation); 078 } 079 } 080 } 081 } 082 083 public boolean shouldExecuteOnProject(Project project) { 084 return !project.getFileSystem().mainFiles(Java.KEY).isEmpty() && 085 !profile.getActiveRulesByRepository(PmdConstants.REPOSITORY_KEY).isEmpty(); 086 } 087 088 @Override 089 public String toString() { 090 return getClass().getSimpleName(); 091 } 092 }