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.

SymbolReferencesSensor.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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 com.google.common.base.Splitter;
  22. import java.io.File;
  23. import java.io.IOException;
  24. import java.util.Iterator;
  25. import java.util.List;
  26. import org.apache.commons.io.FileUtils;
  27. import org.apache.commons.lang.StringUtils;
  28. import org.sonar.api.batch.fs.InputFile;
  29. import org.sonar.api.batch.sensor.Sensor;
  30. import org.sonar.api.batch.sensor.SensorContext;
  31. import org.sonar.api.batch.sensor.SensorDescriptor;
  32. import org.sonar.api.batch.sensor.symbol.NewSymbol;
  33. import org.sonar.api.batch.sensor.symbol.NewSymbolTable;
  34. import org.sonar.api.utils.log.Logger;
  35. import org.sonar.api.utils.log.Loggers;
  36. import org.sonar.xoo.Xoo;
  37. import static java.lang.Integer.parseInt;
  38. /**
  39. * Parse files *.xoo.symbol
  40. */
  41. public class SymbolReferencesSensor implements Sensor {
  42. private static final Logger LOG = Loggers.get(SymbolReferencesSensor.class);
  43. private static final String SYMBOL_EXTENSION = ".symbol";
  44. private void processFileSymbol(InputFile inputFile, SensorContext context) {
  45. File ioFile = inputFile.file();
  46. File symbolFile = new File(ioFile.getParentFile(), ioFile.getName() + SYMBOL_EXTENSION);
  47. if (symbolFile.exists()) {
  48. LOG.debug("Processing " + symbolFile.getAbsolutePath());
  49. try {
  50. List<String> lines = FileUtils.readLines(symbolFile, context.fileSystem().encoding().name());
  51. int lineNumber = 0;
  52. NewSymbolTable symbolTable = context.newSymbolTable()
  53. .onFile(inputFile);
  54. for (String line : lines) {
  55. lineNumber++;
  56. if (StringUtils.isBlank(line) || line.startsWith("#")) {
  57. continue;
  58. }
  59. processLine(symbolFile, lineNumber, symbolTable, line);
  60. }
  61. symbolTable.save();
  62. } catch (IOException e) {
  63. throw new IllegalStateException(e);
  64. }
  65. }
  66. }
  67. private static void processLine(File symbolFile, int lineNumber, NewSymbolTable symbolTable, String line) {
  68. try {
  69. Iterator<String> split = Splitter.on(",").split(line).iterator();
  70. String[] symbolOffsets = split.next().split(":");
  71. if (symbolOffsets.length == 4) {
  72. int startLine = parseInt(symbolOffsets[0]);
  73. int startLineOffset = parseInt(symbolOffsets[1]);
  74. int endLine = parseInt(symbolOffsets[2]);
  75. int endLineOffset = parseInt(symbolOffsets[3]);
  76. NewSymbol s = symbolTable.newSymbol(startLine, startLineOffset, endLine, endLineOffset);
  77. parseReferences(s, split);
  78. } else {
  79. throw new IllegalStateException("Illegal number of elements separated by ':'. " +
  80. "Must be startLine:startLineOffset:endLine:endLineOffset");
  81. }
  82. } catch (Exception e) {
  83. throw new IllegalStateException("Error processing line " + lineNumber + " of file " + symbolFile.getAbsolutePath(), e);
  84. }
  85. }
  86. private static void parseReferences(NewSymbol s, Iterator<String> split) {
  87. while (split.hasNext()) {
  88. addReference(s, split.next());
  89. }
  90. }
  91. private static void addReference(NewSymbol s, String str) {
  92. String[] split = str.split(":");
  93. if (split.length == 4) {
  94. int startLine = parseInt(split[0]);
  95. int startLineOffset = parseInt(split[1]);
  96. int endLine = parseInt(split[2]);
  97. int endLineOffset = parseInt(split[3]);
  98. s.newReference(startLine, startLineOffset, endLine, endLineOffset);
  99. } else {
  100. throw new IllegalStateException("Illegal number of elements separated by ':'");
  101. }
  102. }
  103. @Override
  104. public void describe(SensorDescriptor descriptor) {
  105. descriptor
  106. .name("Xoo Symbol Reference Sensor")
  107. .onlyOnLanguages(Xoo.KEY);
  108. }
  109. @Override
  110. public void execute(SensorContext context) {
  111. for (InputFile file : context.fileSystem().inputFiles(context.fileSystem().predicates().hasLanguages(Xoo.KEY))) {
  112. processFileSymbol(file, context);
  113. }
  114. }
  115. }