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.apache.commons.io.FileUtils; 023import org.sonar.api.batch.fs.FilePredicates; 024import org.sonar.api.batch.fs.FileSystem; 025import org.sonar.api.batch.fs.InputFile; 026import org.sonar.api.batch.fs.InputFile.Type; 027import org.sonar.api.batch.sensor.Sensor; 028import org.sonar.api.batch.sensor.SensorContext; 029import org.sonar.api.batch.sensor.SensorDescriptor; 030import org.sonar.api.rule.RuleKey; 031import org.sonar.xoo.Xoo; 032 033import java.io.File; 034import java.io.IOException; 035 036public 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 .onlyOnLanguages(Xoo.KEY) 046 .createIssuesForRuleRepositories(XooRulesDefinition.XOO_REPOSITORY) 047 .requireProperty(SONAR_XOO_RANDOM_ACCESS_ISSUE_PATHS); 048 } 049 050 @Override 051 public void execute(SensorContext context) { 052 File f = new File(context.settings().getString(SONAR_XOO_RANDOM_ACCESS_ISSUE_PATHS)); 053 FileSystem fs = context.fileSystem(); 054 FilePredicates p = fs.predicates(); 055 try { 056 for (String path : FileUtils.readLines(f)) { 057 createIssues(fs.inputFile(p.and(p.hasPath(path), p.hasType(Type.MAIN), p.hasLanguage(Xoo.KEY))), context); 058 } 059 } catch (IOException e) { 060 throw new IllegalStateException(e); 061 } 062 } 063 064 private void createIssues(InputFile file, SensorContext context) { 065 RuleKey ruleKey = RuleKey.of(XooRulesDefinition.XOO_REPOSITORY, RULE_KEY); 066 context.newIssue() 067 .forRule(ruleKey) 068 .onFile(file) 069 .atLine(1) 070 .message("This issue is generated on each file") 071 .save(); 072 } 073}