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

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