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.

DecorationDataHolder.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.server.source;
  21. import com.google.common.collect.Lists;
  22. import java.util.Arrays;
  23. import java.util.Iterator;
  24. import java.util.List;
  25. class DecorationDataHolder {
  26. private static final String ENTITY_SEPARATOR = ";";
  27. private static final String FIELD_SEPARATOR = ",";
  28. private static final String SYMBOL_PREFIX = "sym-";
  29. private static final String HIGHLIGHTABLE = "sym";
  30. private List<OpeningHtmlTag> openingTagsEntries;
  31. private int openingTagsIndex;
  32. private List<Integer> closingTagsOffsets;
  33. private int closingTagsIndex;
  34. DecorationDataHolder() {
  35. openingTagsEntries = Lists.newArrayList();
  36. closingTagsOffsets = Lists.newArrayList();
  37. }
  38. void loadSymbolReferences(String symbolsReferences) {
  39. String[] symbols = symbolsReferences.split(ENTITY_SEPARATOR);
  40. for (String symbol : symbols) {
  41. String[] symbolFields = symbol.split(FIELD_SEPARATOR);
  42. int declarationStartOffset = Integer.parseInt(symbolFields[0]);
  43. int declarationEndOffset = Integer.parseInt(symbolFields[1]);
  44. int symbolLength = declarationEndOffset - declarationStartOffset;
  45. String[] symbolOccurrences = Arrays.copyOfRange(symbolFields, 2, symbolFields.length);
  46. loadSymbolOccurrences(declarationStartOffset, symbolLength, symbolOccurrences);
  47. }
  48. }
  49. void loadLineSymbolReferences(String symbolsReferences) {
  50. String[] symbols = symbolsReferences.split(ENTITY_SEPARATOR);
  51. for (String symbol : symbols) {
  52. String[] symbolFields = symbol.split(FIELD_SEPARATOR);
  53. int startOffset = Integer.parseInt(symbolFields[0]);
  54. int endOffset = Integer.parseInt(symbolFields[1]);
  55. int symbolLength = endOffset - startOffset;
  56. int symbolId = Integer.parseInt(symbolFields[2]);
  57. loadSymbolOccurrences(symbolId, symbolLength, new String[] { Integer.toString(startOffset) });
  58. }
  59. }
  60. void loadSyntaxHighlightingData(String syntaxHighlightingRules) {
  61. String[] rules = syntaxHighlightingRules.split(ENTITY_SEPARATOR);
  62. for (String rule : rules) {
  63. String[] ruleFields = rule.split(FIELD_SEPARATOR);
  64. int startOffset = Integer.parseInt(ruleFields[0]);
  65. int endOffset = Integer.parseInt(ruleFields[1]);
  66. if (startOffset < endOffset) {
  67. insertAndPreserveOrder(new OpeningHtmlTag(startOffset, ruleFields[2]), openingTagsEntries);
  68. insertAndPreserveOrder(endOffset, closingTagsOffsets);
  69. }
  70. }
  71. }
  72. List<OpeningHtmlTag> getOpeningTagsEntries() {
  73. return openingTagsEntries;
  74. }
  75. OpeningHtmlTag getCurrentOpeningTagEntry() {
  76. return openingTagsIndex < openingTagsEntries.size() ? openingTagsEntries.get(openingTagsIndex) : null;
  77. }
  78. void nextOpeningTagEntry() {
  79. openingTagsIndex++;
  80. }
  81. List<Integer> getClosingTagsOffsets() {
  82. return closingTagsOffsets;
  83. }
  84. int getCurrentClosingTagOffset() {
  85. return closingTagsIndex < closingTagsOffsets.size() ? closingTagsOffsets.get(closingTagsIndex) : -1;
  86. }
  87. void nextClosingTagOffset() {
  88. closingTagsIndex++;
  89. }
  90. private void loadSymbolOccurrences(int declarationStartOffset, int symbolLength, String[] symbolOccurrences) {
  91. for (String symbolOccurrence : symbolOccurrences) {
  92. int occurrenceStartOffset = Integer.parseInt(symbolOccurrence);
  93. int occurrenceEndOffset = occurrenceStartOffset + symbolLength;
  94. insertAndPreserveOrder(new OpeningHtmlTag(occurrenceStartOffset, SYMBOL_PREFIX + declarationStartOffset + " " + HIGHLIGHTABLE), openingTagsEntries);
  95. insertAndPreserveOrder(occurrenceEndOffset, closingTagsOffsets);
  96. }
  97. }
  98. private static void insertAndPreserveOrder(OpeningHtmlTag newEntry, List<OpeningHtmlTag> openingHtmlTags) {
  99. int insertionIndex = 0;
  100. Iterator<OpeningHtmlTag> tagIterator = openingHtmlTags.iterator();
  101. while (tagIterator.hasNext() && tagIterator.next().getStartOffset() <= newEntry.getStartOffset()) {
  102. insertionIndex++;
  103. }
  104. openingHtmlTags.add(insertionIndex, newEntry);
  105. }
  106. private static void insertAndPreserveOrder(int newOffset, List<Integer> orderedOffsets) {
  107. int insertionIndex = 0;
  108. Iterator<Integer> entriesIterator = orderedOffsets.iterator();
  109. while (entriesIterator.hasNext() && entriesIterator.next() <= newOffset) {
  110. insertionIndex++;
  111. }
  112. orderedOffsets.add(insertionIndex, newOffset);
  113. }
  114. }