You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EducationRuleSensor.java 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.education.sensors;
  21. import java.io.BufferedReader;
  22. import java.io.IOException;
  23. import java.io.InputStreamReader;
  24. import org.sonar.api.batch.fs.FileSystem;
  25. import org.sonar.api.batch.fs.InputFile;
  26. import org.sonar.api.batch.rule.ActiveRules;
  27. import org.sonar.api.batch.sensor.Sensor;
  28. import org.sonar.api.batch.sensor.SensorContext;
  29. import org.sonar.api.batch.sensor.SensorDescriptor;
  30. import org.sonar.api.batch.sensor.issue.NewIssue;
  31. import org.sonar.api.rule.RuleKey;
  32. import static org.sonar.education.EducationRulesDefinition.EDUCATION_KEY;
  33. import static org.sonar.education.EducationRulesDefinition.EDUCATION_RULE_REPOSITORY_KEY;
  34. public abstract class EducationRuleSensor implements Sensor {
  35. private final FileSystem fs;
  36. private final ActiveRules activeRules;
  37. private final String ruleKeyName;
  38. private final String tag;
  39. EducationRuleSensor(FileSystem fs, ActiveRules activeRules, String ruleKey) {
  40. this.fs = fs;
  41. this.activeRules = activeRules;
  42. this.ruleKeyName = ruleKey;
  43. this.tag = ruleKey;
  44. }
  45. @Override
  46. public void describe(SensorDescriptor descriptor) {
  47. descriptor
  48. .name("Education sensor for " + tag)
  49. .createIssuesForRuleRepository(EDUCATION_RULE_REPOSITORY_KEY);
  50. }
  51. @Override
  52. public void execute(SensorContext context) {
  53. RuleKey ruleKey = RuleKey.of(EDUCATION_RULE_REPOSITORY_KEY, ruleKeyName);
  54. if (activeRules.find(ruleKey) == null) {
  55. return;
  56. }
  57. for (InputFile inputFile : fs.inputFiles(fs.predicates().hasLanguage(EDUCATION_KEY))) {
  58. processFile(inputFile, context, ruleKey);
  59. }
  60. }
  61. protected String getDetectedContext() {
  62. return null;
  63. }
  64. protected void processFile(InputFile inputFile, SensorContext context, RuleKey ruleKey) {
  65. try {
  66. int[] lineCounter = {1};
  67. try (InputStreamReader isr = new InputStreamReader(inputFile.inputStream(), inputFile.charset());
  68. BufferedReader reader = new BufferedReader(isr)) {
  69. reader.lines().forEachOrdered(lineStr -> {
  70. int startIndex = -1;
  71. while ((startIndex = lineStr.indexOf(tag, startIndex + 1)) != -1) {
  72. NewIssue newIssue = context.newIssue();
  73. newIssue
  74. .forRule(ruleKey)
  75. .at(newIssue.newLocation()
  76. .on(inputFile)
  77. .at(inputFile.newRange(lineCounter[0], startIndex, lineCounter[0], startIndex + tag.length())))
  78. .setRuleDescriptionContextKey(getDetectedContext())
  79. .save();
  80. }
  81. lineCounter[0]++;
  82. });
  83. }
  84. } catch (IOException e) {
  85. throw new IllegalStateException("Fail to process " + inputFile, e);
  86. }
  87. }
  88. }