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     */
020    package org.sonar.xoo.rule;
021    
022    import org.apache.commons.io.FileUtils;
023    import org.sonar.api.batch.fs.FilePredicates;
024    import org.sonar.api.batch.fs.FileSystem;
025    import org.sonar.api.batch.fs.InputFile;
026    import org.sonar.api.batch.fs.InputFile.Type;
027    import org.sonar.api.batch.sensor.Sensor;
028    import org.sonar.api.batch.sensor.SensorContext;
029    import org.sonar.api.batch.sensor.SensorDescriptor;
030    import org.sonar.api.rule.RuleKey;
031    import org.sonar.xoo.Xoo;
032    
033    import java.io.File;
034    import java.io.IOException;
035    
036    public class RandomAccessSensor implements Sensor {
037    
038      private static final String SONAR_XOO_RANDOM_ACCESS_ISSUE_PATHS = "sonar.xoo.randomAccessIssue.paths";
039      public static final String RULE_KEY = "RandomAccessIssue";
040    
041      @Override
042      public void describe(SensorDescriptor descriptor) {
043        descriptor
044          .name("One Issue Per File with Random Access")
045          .workOnLanguages(Xoo.KEY)
046          .createIssuesForRuleRepositories(XooRulesDefinition.XOO_REPOSITORY);
047      }
048    
049      @Override
050      public void execute(SensorContext context) {
051        if (!context.settings().hasKey(SONAR_XOO_RANDOM_ACCESS_ISSUE_PATHS)) {
052          return;
053        }
054        File f = new File(context.settings().getString(SONAR_XOO_RANDOM_ACCESS_ISSUE_PATHS));
055        FileSystem fs = context.fileSystem();
056        FilePredicates p = fs.predicates();
057        try {
058          for (String path : FileUtils.readLines(f)) {
059            createIssues(fs.inputFile(p.and(p.hasPath(path), p.hasType(Type.MAIN), p.hasLanguage(Xoo.KEY))), context);
060          }
061        } catch (IOException e) {
062          throw new IllegalStateException(e);
063        }
064      }
065    
066      private void createIssues(InputFile file, SensorContext context) {
067        RuleKey ruleKey = RuleKey.of(XooRulesDefinition.XOO_REPOSITORY, RULE_KEY);
068        context.addIssue(context.issueBuilder()
069          .ruleKey(ruleKey)
070          .onFile(file)
071          .atLine(1)
072          .message("This issue is generated on each file")
073          .build());
074      }
075    }