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 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 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 == 3) {
  69. int startOffset = parseInt(split[0]);
  70. int endOffset = parseInt(split[1]);
  71. highlighting.highlight(startOffset, endOffset, TypeOfText.forCssClass(split[2]));
  72. } else if (split.length == 5) {
  73. int startLine = parseInt(split[0]);
  74. int startLineOffset = parseInt(split[1]);
  75. int endLine = parseInt(split[2]);
  76. int endLineOffset = parseInt(split[3]);
  77. highlighting.highlight(startLine, startLineOffset, endLine, endLineOffset, TypeOfText.forCssClass(split[4]));
  78. } else {
  79. throw new IllegalStateException("Illegal number of elements separated by ':'. " +
  80. "Must either be startOffset:endOffset:class (offset in whole file) or startLine:startLineOffset:endLine:endLineOffset:class");
  81. }
  82. } catch (Exception e) {
  83. throw new IllegalStateException("Error processing line " + lineNumber + " of file " + highlightingFile.getAbsolutePath(), e);
  84. }
  85. }
  86. @Override
  87. public void describe(SensorDescriptor descriptor) {
  88. descriptor
  89. .name("Xoo Highlighting Sensor")
  90. .onlyOnLanguages(Xoo.KEY);
  91. }
  92. @Override
  93. public void execute(SensorContext context) {
  94. for (InputFile file : context.fileSystem().inputFiles(context.fileSystem().predicates().hasLanguages(Xoo.KEY))) {
  95. processFileHighlighting(file, context);
  96. }
  97. }
  98. }