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.

SignificantCodeSensor.java 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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.xoo.lang;
  21. import java.io.IOException;
  22. import java.nio.file.Files;
  23. import java.nio.file.Path;
  24. import java.util.List;
  25. import org.apache.commons.lang.StringUtils;
  26. import org.sonar.api.batch.fs.InputFile;
  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.code.NewSignificantCode;
  31. import org.sonar.api.utils.log.Logger;
  32. import org.sonar.api.utils.log.Loggers;
  33. import org.sonar.xoo.Xoo;
  34. public class SignificantCodeSensor implements Sensor {
  35. private static final Logger LOG = Loggers.get(SignificantCodeSensor.class);
  36. private static final String FILE_EXTENSION = ".significantCode";
  37. @Override
  38. public void describe(SensorDescriptor descriptor) {
  39. descriptor
  40. .name("Xoo Significant Code Ranges Sensor")
  41. .onlyOnLanguages(Xoo.KEY);
  42. }
  43. @Override
  44. public void execute(SensorContext context) {
  45. for (InputFile file : context.fileSystem().inputFiles(context.fileSystem().predicates().hasLanguages(Xoo.KEY))) {
  46. processSignificantCodeFile(file, context);
  47. }
  48. }
  49. private void processSignificantCodeFile(InputFile inputFile, SensorContext context) {
  50. Path ioFile = inputFile.path();
  51. Path significantCodeFile = ioFile.resolveSibling(ioFile.getFileName() + FILE_EXTENSION).toAbsolutePath();
  52. if (Files.exists(significantCodeFile) && Files.isRegularFile(significantCodeFile)) {
  53. LOG.debug("Processing " + significantCodeFile.toString());
  54. try {
  55. List<String> lines = Files.readAllLines(significantCodeFile, context.fileSystem().encoding());
  56. NewSignificantCode significantCode = context.newSignificantCode()
  57. .onFile(inputFile);
  58. for (String line : lines) {
  59. if (StringUtils.isBlank(line) || line.startsWith("#")) {
  60. continue;
  61. }
  62. processLine(line, inputFile, significantCode);
  63. }
  64. significantCode.save();
  65. } catch (IOException e) {
  66. throw new IllegalStateException(e);
  67. }
  68. }
  69. }
  70. private void processLine(String fileLine, InputFile inputFile, NewSignificantCode significantCode) {
  71. String[] textPointer = fileLine.split(",");
  72. if (textPointer.length != 3) {
  73. throw new IllegalStateException("Invalid format in error file");
  74. }
  75. try {
  76. int line = Integer.parseInt(textPointer[0]);
  77. int startLineOffset = Integer.parseInt(textPointer[1]);
  78. int endLineOffset = Integer.parseInt(textPointer[2]);
  79. significantCode.addRange(inputFile.newRange(line, startLineOffset, line, endLineOffset));
  80. } catch (NumberFormatException e) {
  81. throw new IllegalStateException("Invalid format in error file", e);
  82. }
  83. }
  84. }