001/*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2014 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube 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 * SonarQube 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 License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019 */
020package org.sonar.xoo.rule;
021
022import org.sonar.api.batch.Sensor;
023import org.sonar.api.batch.rule.ActiveRules;
024import org.sonar.api.component.ResourcePerspectives;
025import org.sonar.api.issue.Issuable;
026import org.sonar.api.resources.Directory;
027import org.sonar.api.resources.Project;
028import org.sonar.api.rule.RuleKey;
029import org.sonar.api.scan.filesystem.FileQuery;
030import org.sonar.api.scan.filesystem.ModuleFileSystem;
031import org.sonar.api.scan.filesystem.PathResolver;
032import org.sonar.xoo.Xoo;
033
034import java.io.File;
035
036public class DeprecatedResourceApiSensor implements Sensor {
037
038  public static final String RULE_KEY = "DeprecatedResourceApi";
039  private final ModuleFileSystem fileSystem;
040  private final ResourcePerspectives perspectives;
041  private final ActiveRules activeRules;
042
043  public DeprecatedResourceApiSensor(ModuleFileSystem fileSystem, ResourcePerspectives perspectives, ActiveRules activeRules) {
044    this.fileSystem = fileSystem;
045    this.perspectives = perspectives;
046    this.activeRules = activeRules;
047  }
048
049  @Override
050  public boolean shouldExecuteOnProject(Project project) {
051    return !fileSystem.files(FileQuery.onMain().onLanguage(Xoo.KEY)).isEmpty() && activeRules.find(RuleKey.of(XooRulesDefinition.XOO_REPOSITORY, RULE_KEY)) != null;
052  }
053
054  @Override
055  public void analyse(Project module, org.sonar.api.batch.SensorContext context) {
056    createIssueOnDir(new Directory(""));
057    File src = module.getFileSystem().getSourceDirs().get(0);
058
059    for (File f : fileSystem.files(FileQuery.onMain().onLanguage(Xoo.KEY))) {
060      String relativePathFromSourceDir = new PathResolver().relativePath(src, f);
061      org.sonar.api.resources.File sonarFile = new org.sonar.api.resources.File(relativePathFromSourceDir);
062      Issuable issuable = perspectives.as(Issuable.class, sonarFile);
063      issuable.addIssue(issuable.newIssueBuilder()
064        .ruleKey(RuleKey.of(XooRulesDefinition.XOO_REPOSITORY, RULE_KEY))
065        .message("Issue created using deprecated API")
066        .line(1)
067        .build());
068
069      sonarFile = context.getResource(sonarFile);
070      Directory parent = sonarFile.getParent();
071      createIssueOnDir(parent);
072    }
073
074  }
075
076  private Directory createIssueOnDir(Directory dir) {
077    Issuable issuable = perspectives.as(Issuable.class, dir);
078    issuable.addIssue(issuable.newIssueBuilder()
079      .ruleKey(RuleKey.of(XooRulesDefinition.XOO_REPOSITORY, RULE_KEY))
080      .message("Issue created using deprecated API")
081      .build());
082    return dir;
083  }
084
085}