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.findbugs;
021    
022    import org.sonar.api.CoreProperties;
023    import org.sonar.api.batch.Sensor;
024    import org.sonar.api.batch.SensorContext;
025    import org.sonar.api.profiles.RulesProfile;
026    import org.sonar.api.resources.JavaFile;
027    import org.sonar.api.resources.Project;
028    import org.sonar.api.rules.Rule;
029    import org.sonar.api.rules.RuleFinder;
030    import org.sonar.api.rules.Violation;
031    import org.sonar.api.utils.Logs;
032    
033    import java.io.File;
034    import java.util.List;
035    
036    public class FindbugsSensor implements Sensor {
037      private RulesProfile profile;
038      private RuleFinder ruleFinder;
039      private FindbugsExecutor executor;
040    
041      public FindbugsSensor(RulesProfile profile, RuleFinder ruleFinder, FindbugsExecutor executor) {
042        this.profile = profile;
043        this.ruleFinder = ruleFinder;
044        this.executor = executor;
045      }
046    
047      public boolean shouldExecuteOnProject(Project project) {
048        return project.getFileSystem().hasJavaSourceFiles()
049            && !profile.getActiveRulesByRepository(FindbugsConstants.REPOSITORY_KEY).isEmpty();
050      }
051    
052      public void analyse(Project project, SensorContext context) {
053        if (project.getReuseExistingRulesConfig()) {
054          Logs.INFO.warn("Reusing existing Findbugs configuration not supported any more.");
055        }
056        File report = getFindbugsReportFile(project);
057        if (report == null) {
058          report = executor.execute();
059        }
060        FindbugsXmlReportParser reportParser = new FindbugsXmlReportParser(report);
061        List<FindbugsXmlReportParser.Violation> fbViolations = reportParser.getViolations();
062        for (FindbugsXmlReportParser.Violation fbViolation : fbViolations) {
063          Rule rule = ruleFinder.findByKey(FindbugsConstants.REPOSITORY_KEY, fbViolation.getType());
064          if (rule != null) { // ignore violations from report, if rule not activated in Sonar
065            JavaFile resource = new JavaFile(fbViolation.getSonarJavaFileKey());
066            if (context.getResource(resource) != null) {
067              Violation violation = Violation.create(rule, resource).setLineId(fbViolation.getStart()).setMessage(fbViolation.getLongMessage());
068              context.saveViolation(violation);
069            }
070          } else {
071            Logs.INFO.warn("Findbugs rule '{}' not active in Sonar.", fbViolation.getType());
072          }
073        }
074      }
075    
076      protected final File getFindbugsReportFile(Project project) {
077        if (project.getConfiguration().getString(CoreProperties.FINDBUGS_REPORT_PATH) != null) {
078          return new File(project.getConfiguration().getString(CoreProperties.FINDBUGS_REPORT_PATH));
079        }
080        return null;
081      }
082    
083      @Override
084      public String toString() {
085        return getClass().getSimpleName();
086      }
087    }