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.

DefaultCpdTokens.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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.api.batch.sensor.cpd.internal;
  21. import java.nio.file.Paths;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24. import org.sonar.api.CoreProperties;
  25. import org.sonar.api.batch.fs.InputFile;
  26. import org.sonar.api.batch.fs.TextRange;
  27. import org.sonar.api.batch.fs.internal.PathPattern;
  28. import org.sonar.api.batch.sensor.cpd.NewCpdTokens;
  29. import org.sonar.api.batch.sensor.internal.DefaultStorable;
  30. import org.sonar.api.batch.sensor.internal.SensorStorage;
  31. import org.sonar.api.config.Configuration;
  32. import static com.google.common.base.Preconditions.checkState;
  33. import static java.util.Collections.unmodifiableList;
  34. import static java.util.Objects.requireNonNull;
  35. public class DefaultCpdTokens extends DefaultStorable implements NewCpdTokens {
  36. private final Configuration config;
  37. private final List<TokensLine> result = new ArrayList<>();
  38. private InputFile inputFile;
  39. private int startLine = Integer.MIN_VALUE;
  40. private int startIndex = 0;
  41. private int currentIndex = 0;
  42. private StringBuilder sb = new StringBuilder();
  43. private TextRange lastRange;
  44. private boolean excluded;
  45. public DefaultCpdTokens(Configuration config, SensorStorage storage) {
  46. super(storage);
  47. this.config = config;
  48. }
  49. @Override
  50. public DefaultCpdTokens onFile(InputFile inputFile) {
  51. this.inputFile = requireNonNull(inputFile, "file can't be null");
  52. String[] cpdExclusions = config.getStringArray(CoreProperties.CPD_EXCLUSIONS);
  53. for (PathPattern cpdExclusion : PathPattern.create(cpdExclusions)) {
  54. if (cpdExclusion.match(inputFile.path(), Paths.get(inputFile.relativePath()))) {
  55. this.excluded = true;
  56. }
  57. }
  58. return this;
  59. }
  60. public InputFile inputFile() {
  61. return inputFile;
  62. }
  63. @Override
  64. public NewCpdTokens addToken(int startLine, int startLineOffset, int endLine, int endLineOffset, String image) {
  65. checkInputFileNotNull();
  66. TextRange newRange;
  67. try {
  68. newRange = inputFile.newRange(startLine, startLineOffset, endLine, endLineOffset);
  69. } catch (Exception e) {
  70. throw new IllegalArgumentException("Unable to register token in file " + inputFile, e);
  71. }
  72. return addToken(newRange, image);
  73. }
  74. @Override
  75. public DefaultCpdTokens addToken(TextRange range, String image) {
  76. requireNonNull(range, "Range should not be null");
  77. requireNonNull(image, "Image should not be null");
  78. checkInputFileNotNull();
  79. if (excluded) {
  80. return this;
  81. }
  82. checkState(lastRange == null || lastRange.end().compareTo(range.start()) <= 0,
  83. "Tokens of file %s should be provided in order.\nPrevious token: %s\nLast token: %s", inputFile, lastRange, range);
  84. String value = image;
  85. int line = range.start().line();
  86. if (line != startLine) {
  87. addNewTokensLine(result, startIndex, currentIndex, startLine, sb);
  88. startIndex = currentIndex + 1;
  89. startLine = line;
  90. }
  91. currentIndex++;
  92. sb.append(value);
  93. lastRange = range;
  94. return this;
  95. }
  96. public List<TokensLine> getTokenLines() {
  97. return unmodifiableList(new ArrayList<>(result));
  98. }
  99. private static void addNewTokensLine(List<TokensLine> result, int startUnit, int endUnit, int startLine, StringBuilder sb) {
  100. if (sb.length() != 0) {
  101. result.add(new TokensLine(startUnit, endUnit, startLine, sb.toString()));
  102. sb.setLength(0);
  103. }
  104. }
  105. @Override
  106. protected void doSave() {
  107. checkState(inputFile != null, "Call onFile() first");
  108. if (excluded) {
  109. return;
  110. }
  111. addNewTokensLine(result, startIndex, currentIndex, startLine, sb);
  112. storage.store(this);
  113. }
  114. private void checkInputFileNotNull() {
  115. checkState(inputFile != null, "Call onFile() first");
  116. }
  117. }