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.

SyntaxHighlightingSensor.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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.xoo.lang;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.util.List;
  24. import org.apache.commons.io.FileUtils;
  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.highlighting.NewHighlighting;
  31. import org.sonar.api.batch.sensor.highlighting.TypeOfText;
  32. import org.sonar.api.utils.log.Logger;
  33. import org.sonar.api.utils.log.Loggers;
  34. import org.sonar.xoo.Xoo;
  35. import static java.lang.Integer.parseInt;
  36. /**
  37. * Parse files *.xoo.highlighting
  38. */
  39. public class SyntaxHighlightingSensor implements Sensor {
  40. private static final Logger LOG = Loggers.get(SyntaxHighlightingSensor.class);
  41. private static final String HIGHLIGHTING_EXTENSION = ".highlighting";
  42. private void processFileHighlighting(InputFile inputFile, SensorContext context) {
  43. File ioFile = inputFile.file();
  44. File highlightingFile = new File(ioFile.getParentFile(), ioFile.getName() + HIGHLIGHTING_EXTENSION);
  45. if (highlightingFile.exists()) {
  46. LOG.debug("Processing " + highlightingFile.getAbsolutePath());
  47. try {
  48. List<String> lines = FileUtils.readLines(highlightingFile, context.fileSystem().encoding().name());
  49. int lineNumber = 0;
  50. NewHighlighting highlighting = context.newHighlighting()
  51. .onFile(inputFile);
  52. for (String line : lines) {
  53. lineNumber++;
  54. if (StringUtils.isBlank(line) || line.startsWith("#")) {
  55. continue;
  56. }
  57. processLine(highlightingFile, lineNumber, highlighting, line);
  58. }
  59. highlighting.save();
  60. } catch (IOException e) {
  61. throw new IllegalStateException(e);
  62. }
  63. }
  64. }
  65. private static void processLine(File highlightingFile, int lineNumber, NewHighlighting highlighting, String line) {
  66. try {
  67. String[] split = line.split(":");
  68. if (split.length == 5) {
  69. int startLine = parseInt(split[0]);
  70. int startLineOffset = parseInt(split[1]);
  71. int endLine = parseInt(split[2]);
  72. int endLineOffset = parseInt(split[3]);
  73. highlighting.highlight(startLine, startLineOffset, endLine, endLineOffset, TypeOfText.forCssClass(split[4]));
  74. } else {
  75. throw new IllegalStateException("Illegal number of elements separated by ':'. " +
  76. "Must be startLine:startLineOffset:endLine:endLineOffset:class");
  77. }
  78. } catch (Exception e) {
  79. throw new IllegalStateException("Error processing line " + lineNumber + " of file " + highlightingFile.getAbsolutePath(), e);
  80. }
  81. }
  82. @Override
  83. public void describe(SensorDescriptor descriptor) {
  84. descriptor
  85. .name("Xoo Highlighting Sensor")
  86. .onlyOnLanguages(Xoo.KEY);
  87. }
  88. @Override
  89. public void execute(SensorContext context) {
  90. for (InputFile file : context.fileSystem().inputFiles(context.fileSystem().predicates().hasLanguages(Xoo.KEY))) {
  91. processFileHighlighting(file, context);
  92. }
  93. }
  94. }