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 net.sourceforge.pmd.IRuleViolation;
023import net.sourceforge.pmd.Report;
024import org.sonar.api.batch.Sensor;
025import org.sonar.api.batch.SensorContext;
026import org.sonar.api.profiles.RulesProfile;
027import org.sonar.api.resources.Java;
028import org.sonar.api.resources.Project;
029import org.sonar.api.rules.Violation;
030import org.sonar.api.utils.XmlParserException;
031
032import java.util.Iterator;
033
034public class PmdSensor implements Sensor {
035  private final RulesProfile profile;
036  private final PmdExecutor executor;
037  private final PmdViolationToRuleViolation pmdViolationToRuleViolation;
038
039  public PmdSensor(RulesProfile profile, PmdExecutor executor, PmdViolationToRuleViolation pmdViolationToRuleViolation) {
040    this.profile = profile;
041    this.executor = executor;
042    this.pmdViolationToRuleViolation = pmdViolationToRuleViolation;
043  }
044
045  public boolean shouldExecuteOnProject(Project project) {
046    return (!project.getFileSystem().mainFiles(Java.KEY).isEmpty() && !profile.getActiveRulesByRepository(PmdConstants.REPOSITORY_KEY).isEmpty())
047      || (!project.getFileSystem().testFiles(Java.KEY).isEmpty() && !profile.getActiveRulesByRepository(PmdConstants.TEST_REPOSITORY_KEY).isEmpty());
048  }
049
050  public void analyse(Project project, SensorContext context) {
051    try {
052      Report report = executor.execute();
053      reportViolations(report.iterator(), context);
054    } catch (Exception e) {
055      throw new XmlParserException(e);
056    }
057  }
058
059  private void reportViolations(Iterator<IRuleViolation> violations, SensorContext context) {
060    while (violations.hasNext()) {
061      IRuleViolation pmdViolation = violations.next();
062
063      Violation violation = pmdViolationToRuleViolation.toViolation(pmdViolation, context);
064      if (null != violation) {
065        context.saveViolation(violation);
066      }
067    }
068  }
069
070  @Override
071  public String toString() {
072    return getClass().getSimpleName();
073  }
074}