1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
/*
* SonarQube
* Copyright (C) 2009-2017 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.scanner.source;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.sensor.highlighting.NewHighlighting;
import org.sonar.api.batch.sensor.highlighting.TypeOfText;
import org.sonar.colorizer.HtmlCodeBuilder;
public class HighlightingCodeBuilder extends HtmlCodeBuilder {
private static final Logger LOG = LoggerFactory.getLogger(HighlightingCodeBuilder.class);
private int currentOffset = 0;
private static final Pattern START_TAG_PATTERN = Pattern.compile("<span class=\"(.+)\">");
private static final Pattern END_TAG_PATTERN = Pattern.compile("</span>");
private int startOffset = -1;
private String cssClass;
private final NewHighlighting highlighting;
public HighlightingCodeBuilder(NewHighlighting highlighting) {
this.highlighting = highlighting;
}
@Override
public Appendable append(CharSequence csq) {
for (int i = 0; i < csq.length(); i++) {
append(csq.charAt(i));
}
return this;
}
@Override
public Appendable append(char c) {
currentOffset++;
return this;
}
@Override
public void appendWithoutTransforming(String htmlTag) {
if (startOffset == -1) {
Matcher startMatcher = START_TAG_PATTERN.matcher(htmlTag);
if (startMatcher.matches()) {
startOffset = currentOffset;
cssClass = startMatcher.group(1);
} else {
LOG.warn("Expected to match highlighting start html tag but was: {}", htmlTag);
}
} else {
Matcher endMatcher = END_TAG_PATTERN.matcher(htmlTag);
if (endMatcher.matches()) {
highlighting.highlight(startOffset, currentOffset, TypeOfText.forCssClass(cssClass));
startOffset = -1;
} else {
LOG.warn("Expected to match highlighting end html tag but was: {}", htmlTag);
}
}
}
@Override
public String toString() {
throw new UnsupportedOperationException();
}
@Override
public StringBuilder getColorizedCode() {
throw new UnsupportedOperationException();
}
}
|