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.

AnalysisErrorSensor.java 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.rule;
  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.utils.log.Logger;
  31. import org.sonar.api.utils.log.Loggers;
  32. import org.sonar.xoo.Xoo;
  33. public class AnalysisErrorSensor implements Sensor {
  34. private static final Logger LOG = Loggers.get(AnalysisErrorSensor.class);
  35. private static final String ERROR_EXTENSION = ".error";
  36. @Override
  37. public void describe(SensorDescriptor descriptor) {
  38. descriptor
  39. .name("Xoo Analysis Error Sensor")
  40. .onlyOnLanguages(Xoo.KEY);
  41. }
  42. @Override
  43. public void execute(SensorContext context) {
  44. for (InputFile file : context.fileSystem().inputFiles(context.fileSystem().predicates().hasLanguages(Xoo.KEY))) {
  45. processFileError(file, context);
  46. }
  47. }
  48. private void processFileError(InputFile inputFile, SensorContext context) {
  49. Path ioFile = inputFile.file().toPath();
  50. Path errorFile = ioFile.resolveSibling(ioFile.getFileName() + ERROR_EXTENSION).toAbsolutePath();
  51. if (Files.exists(errorFile) && Files.isRegularFile(errorFile)) {
  52. LOG.debug("Processing " + errorFile.toString());
  53. try {
  54. List<String> lines = Files.readAllLines(errorFile, context.fileSystem().encoding());
  55. for (String line : lines) {
  56. if (StringUtils.isBlank(line) || line.startsWith("#")) {
  57. continue;
  58. }
  59. processLine(line, inputFile, context);
  60. }
  61. } catch (IOException e) {
  62. throw new IllegalStateException(e);
  63. }
  64. }
  65. }
  66. private void processLine(String fileLine, InputFile inputFile, SensorContext context) {
  67. String[] textPointer = fileLine.split(",");
  68. if (textPointer.length != 3) {
  69. throw new IllegalStateException("Invalid format in error file");
  70. }
  71. try {
  72. int line = Integer.parseInt(textPointer[0]);
  73. int lineOffset = Integer.parseInt(textPointer[1]);
  74. String msg = textPointer[2];
  75. context.newAnalysisError()
  76. .onFile(inputFile)
  77. .at(inputFile.newPointer(line, lineOffset))
  78. .message(msg)
  79. .save();
  80. } catch (NumberFormatException e) {
  81. throw new IllegalStateException("Invalid format in error file", e);
  82. }
  83. }
  84. }