001 /*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2014 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * SonarQube is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
019 */
020 package org.sonar.batch.highlighting;
021
022 import com.google.common.collect.Sets;
023 import org.sonar.api.batch.sensor.highlighting.TypeOfText;
024
025 import com.google.common.annotations.VisibleForTesting;
026 import com.google.common.collect.Ordering;
027
028 import javax.annotation.Nullable;
029
030 import java.util.Iterator;
031 import java.util.Set;
032
033 public class SyntaxHighlightingDataBuilder {
034
035 private Set<SyntaxHighlightingRule> syntaxHighlightingRuleSet;
036
037 public SyntaxHighlightingDataBuilder() {
038 syntaxHighlightingRuleSet = Sets.newTreeSet(new Ordering<SyntaxHighlightingRule>() {
039 @Override
040 public int compare(@Nullable SyntaxHighlightingRule left,
041 @Nullable SyntaxHighlightingRule right) {
042 int result = left.getStartPosition() - right.getStartPosition();
043 if (result == 0) {
044 result = left.getEndPosition() - right.getEndPosition();
045 }
046 return result;
047 }
048 });
049 }
050
051 @VisibleForTesting
052 public Set<SyntaxHighlightingRule> getSyntaxHighlightingRuleSet() {
053 return syntaxHighlightingRuleSet;
054 }
055
056 public SyntaxHighlightingDataBuilder registerHighlightingRule(int startOffset, int endOffset, TypeOfText typeOfText) {
057 SyntaxHighlightingRule syntaxHighlightingRule = SyntaxHighlightingRule.create(startOffset, endOffset,
058 typeOfText);
059 this.syntaxHighlightingRuleSet.add(syntaxHighlightingRule);
060 return this;
061 }
062
063 public SyntaxHighlightingData build() {
064 checkOverlappingBoudaries();
065 return new SyntaxHighlightingData(syntaxHighlightingRuleSet);
066 }
067
068 private void checkOverlappingBoudaries() {
069 if (syntaxHighlightingRuleSet.size() > 1) {
070 Iterator<SyntaxHighlightingRule> it = syntaxHighlightingRuleSet.iterator();
071 SyntaxHighlightingRule previous = it.next();
072 while (it.hasNext()) {
073 SyntaxHighlightingRule current = it.next();
074 if (previous.getEndPosition() > current.getStartPosition() && !(previous.getEndPosition() >= current.getEndPosition())) {
075 String errorMsg = String.format("Cannot register highlighting rule for characters from %s to %s as it " +
076 "overlaps at least one existing rule", current.getStartPosition(), current.getEndPosition());
077 throw new IllegalStateException(errorMsg);
078 }
079 previous = current;
080 }
081 }
082 }
083
084 }