@@ -28,11 +28,7 @@ import org.sonar.api.batch.sensor.SensorDescriptor; | |||
import org.sonar.batch.index.BatchComponentCache; | |||
import org.sonar.batch.protocol.output.BatchReportReader; | |||
import org.sonar.batch.report.ReportPublisher; | |||
import org.sonar.colorizer.CodeColorizer; | |||
/** | |||
* Execute deprecated {@link CodeColorizer} if necessary. | |||
*/ | |||
@Phase(name = Phase.Name.POST) | |||
public final class CodeColorizerSensor implements Sensor { | |||
@@ -20,18 +20,6 @@ | |||
package org.sonar.batch.source; | |||
import com.google.common.collect.Lists; | |||
import org.apache.commons.io.input.BOMInputStream; | |||
import org.apache.commons.lang.StringUtils; | |||
import org.slf4j.Logger; | |||
import org.slf4j.LoggerFactory; | |||
import org.sonar.api.batch.BatchSide; | |||
import org.sonar.api.batch.sensor.highlighting.NewHighlighting; | |||
import org.sonar.api.web.CodeColorizerFormat; | |||
import org.sonar.colorizer.CodeColorizer; | |||
import org.sonar.colorizer.Tokenizer; | |||
import javax.annotation.CheckForNull; | |||
import java.io.BufferedReader; | |||
import java.io.File; | |||
import java.io.FileInputStream; | |||
@@ -41,6 +29,16 @@ import java.nio.charset.Charset; | |||
import java.util.HashMap; | |||
import java.util.List; | |||
import java.util.Map; | |||
import javax.annotation.CheckForNull; | |||
import org.apache.commons.io.input.BOMInputStream; | |||
import org.apache.commons.lang.StringUtils; | |||
import org.slf4j.Logger; | |||
import org.slf4j.LoggerFactory; | |||
import org.sonar.api.batch.BatchSide; | |||
import org.sonar.api.batch.sensor.highlighting.NewHighlighting; | |||
import org.sonar.api.web.CodeColorizerFormat; | |||
import org.sonar.colorizer.JavaTokenizers; | |||
import org.sonar.colorizer.Tokenizer; | |||
/** | |||
* Central point for sonar-colorizer extensions | |||
@@ -77,7 +75,7 @@ public class CodeColorizers { | |||
// TODO can be dropped when Java plugin embed its own CodeColorizerFormat of (better) provides highlighting for tests | |||
// See SONARJAVA-830 | |||
if ("java".equals(language)) { | |||
tokenizers = CodeColorizer.Format.JAVA.getTokenizers(); | |||
tokenizers = JavaTokenizers.forHtml(); | |||
} else { | |||
return; | |||
} |
@@ -15,13 +15,15 @@ | |||
<description>Code syntax highlighter</description> | |||
<dependencies> | |||
<dependency> | |||
<groupId>com.google.guava</groupId> | |||
<artifactId>guava</artifactId> | |||
</dependency> | |||
<dependency> | |||
<groupId>org.codehaus.sonar</groupId> | |||
<artifactId>sonar-channel</artifactId> | |||
<exclusions> | |||
<exclusion> | |||
<groupId>commons-io</groupId> | |||
<artifactId>commons-io</artifactId> | |||
</exclusion> | |||
</exclusions> | |||
</dependency> | |||
<dependency> | |||
<groupId>com.google.code.findbugs</groupId> |
@@ -40,10 +40,6 @@ public class CodeColorizer { | |||
this.tokenizers = format.getTokenizers(); | |||
} | |||
public String toHtml(Reader code) { | |||
return toHtml(code, null); | |||
} | |||
public String toHtml(Reader code, @Nullable HtmlOptions options) { | |||
HtmlOptions opts = options == null ? HtmlOptions.DEFAULT : options; | |||
return new HtmlRenderer(opts).render(code, tokenizers); | |||
@@ -57,10 +53,6 @@ public class CodeColorizer { | |||
return new CodeColorizer(Format.GROOVY).toHtml(code, options); | |||
} | |||
public static String getCss() { | |||
return HtmlDecorator.getCss(); | |||
} | |||
public enum Format { | |||
JAVA(JavaTokenizers.forHtml()), GROOVY(GroovyTokenizers.forHtml()); | |||
@@ -19,8 +19,8 @@ | |||
*/ | |||
package org.sonar.colorizer; | |||
import com.google.common.collect.ImmutableSet; | |||
import java.util.Arrays; | |||
import java.util.HashSet; | |||
import java.util.Set; | |||
/** | |||
@@ -28,10 +28,14 @@ import java.util.Set; | |||
*/ | |||
@Deprecated | |||
public final class GroovyKeywords { | |||
private static final Set<String> KEYWORDS = ImmutableSet.of( | |||
"as", "assert", "break", "case", "catch", "class", "continue", "def", | |||
"default", "do", "else", "extends", "finally", "for", "if", "in", "implements", "import", "instanceof", "interface", "new", "package", | |||
"property", "return", "switch", "throw", "throws", "try", "while"); | |||
private static final Set<String> KEYWORDS = new HashSet<>(); | |||
static { | |||
KEYWORDS.addAll(Arrays.asList("as", "assert", "break", "case", "catch", "class", "continue", "def", | |||
"default", "do", "else", "extends", "finally", "for", "if", "in", "implements", "import", "instanceof", "interface", "new", "package", | |||
"property", "return", "switch", "throw", "throws", "try", "while")); | |||
} | |||
private GroovyKeywords() { | |||
} |
@@ -19,22 +19,14 @@ | |||
*/ | |||
package org.sonar.colorizer; | |||
import com.google.common.io.ByteStreams; | |||
import com.google.common.io.Closeables; | |||
import org.sonar.channel.CodeReader; | |||
import java.io.IOException; | |||
import java.io.InputStream; | |||
import java.nio.charset.StandardCharsets; | |||
/** | |||
* @deprecated since 4.5.2 replace by highlighting mechanism | |||
*/ | |||
@Deprecated | |||
public class HtmlDecorator extends Tokenizer { | |||
private static final String CSS_PATH = "/sonar-colorizer.css"; | |||
private HtmlOptions options; | |||
private int lineId; | |||
@@ -50,9 +42,7 @@ public class HtmlDecorator extends Tokenizer { | |||
StringBuilder sb = new StringBuilder(); | |||
if (options.isGenerateHtmlHeader()) { | |||
sb.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" " | |||
+ "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html><head><style type=\"text/css\">"); | |||
sb.append(getCss()); | |||
sb.append("</style></head><body>"); | |||
+ "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html><body>"); | |||
} | |||
sb.append("<table class=\"code\" id=\""); | |||
if (options.getTableId() != null) { | |||
@@ -94,18 +84,4 @@ public class HtmlDecorator extends Tokenizer { | |||
} | |||
return false; | |||
} | |||
public static String getCss() { | |||
InputStream input = null; | |||
try { | |||
input = HtmlRenderer.class.getResourceAsStream(CSS_PATH); | |||
return new String(ByteStreams.toByteArray(input), StandardCharsets.UTF_8); | |||
} catch (IOException e) { | |||
throw new SynhtaxHighlightingException("SonarQube Colorizer CSS file not found: " + CSS_PATH, e); | |||
} finally { | |||
Closeables.closeQuietly(input); | |||
} | |||
} | |||
} |
@@ -19,8 +19,9 @@ | |||
*/ | |||
package org.sonar.colorizer; | |||
import com.google.common.collect.ImmutableSet; | |||
import java.util.Arrays; | |||
import java.util.Collections; | |||
import java.util.HashSet; | |||
import java.util.Set; | |||
/** | |||
@@ -29,14 +30,14 @@ import java.util.Set; | |||
@Deprecated | |||
public final class JavaKeywords { | |||
private static final Set<String> KEYWORDS = ImmutableSet.of( | |||
private static final Set<String> KEYWORDS = Collections.unmodifiableSet(new HashSet<>(Arrays.asList( | |||
"abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", | |||
"class", "const", "continue", "default", | |||
"do", "double", "else", "enum", "extends", "false", "final", "finally", "float", "for", | |||
"goto", "if", "implements", "import", "instanceof", | |||
"int", "interface", "long", "native", "new", "null", "package", "private", | |||
"protected", "public", "return", "short", "static", "strictfp", | |||
"super", "switch", "synchronized", "this", "throw", "throws", "transient", "true", "try", "void", "volatile", "while"); | |||
"super", "switch", "synchronized", "this", "throw", "throws", "transient", "true", "try", "void", "volatile", "while"))); | |||
private JavaKeywords() { | |||
} |
@@ -29,17 +29,20 @@ import java.util.List; | |||
@Deprecated | |||
public final class JavaTokenizers { | |||
private static final List<Tokenizer> TOKENIZERS = Collections.unmodifiableList(Arrays.asList( | |||
new JavaAnnotationTokenizer("<span class=\"a\">", "</span>"), | |||
new LiteralTokenizer("<span class=\"s\">", "</span>"), | |||
new CDocTokenizer("<span class=\"cd\">", "</span>"), | |||
new JavadocTokenizer("<span class=\"j\">", "</span>"), | |||
new CppDocTokenizer("<span class=\"cppd\">", "</span>"), | |||
new JavaConstantTokenizer("<span class=\"c\">", "</span>"), | |||
new KeywordsTokenizer("<span class=\"k\">", "</span>", JavaKeywords.get()))); | |||
private JavaTokenizers() { | |||
// only static stuff | |||
} | |||
public static List<Tokenizer> forHtml() { | |||
return Collections.unmodifiableList(Arrays.asList( | |||
new JavaAnnotationTokenizer("<span class=\"a\">", "</span>"), | |||
new LiteralTokenizer("<span class=\"s\">", "</span>"), | |||
new CDocTokenizer("<span class=\"cd\">", "</span>"), | |||
new JavadocTokenizer("<span class=\"j\">", "</span>"), | |||
new CppDocTokenizer("<span class=\"cppd\">", "</span>"), | |||
new JavaConstantTokenizer("<span class=\"c\">", "</span>"), | |||
new KeywordsTokenizer("<span class=\"k\">", "</span>", JavaKeywords.get()))); | |||
return TOKENIZERS; | |||
} | |||
} |
@@ -78,12 +78,6 @@ public class CodeColorizerTest { | |||
assertContains(html, "<pre><span class=\"k\">class</span> Greet {</pre>"); | |||
} | |||
@Test | |||
public void getCss() { | |||
assertThat(CodeColorizer.getCss().length()).isGreaterThan(100); | |||
assertThat(CodeColorizer.getCss()).contains(".code"); | |||
} | |||
@Test | |||
public void mustBeThreadsafe() throws InterruptedException, ExecutionException, IOException { | |||
final int taskCount = 50; | |||
@@ -151,7 +145,7 @@ public class CodeColorizerTest { | |||
} | |||
private void assertHtml(String html) { | |||
assertContains(html, "<style", "<table class=\"code\"", "</html>"); | |||
assertContains(html, "<table class=\"code\"", "</html>"); | |||
} | |||
private void assertContains(String html, String... strings) { |
@@ -30,7 +30,7 @@ public class HtmlDecoratorTest { | |||
public void beginOfFileTag() { | |||
HtmlDecorator decorator = new HtmlDecorator(HtmlOptions.DEFAULT); | |||
String tag = decorator.getTagBeginOfFile(); | |||
assertContains(tag, "<html", "<style", "<table"); | |||
assertContains(tag, "<html", "<table"); | |||
} | |||
@Test | |||
@@ -50,7 +50,7 @@ public class HtmlDecoratorTest { | |||
String tag = decorator.getTagBeginOfFile(); | |||
assertNotContains(tag, "<html", "<style"); | |||
assertNotContains(tag, "<html", "<body"); | |||
assertContains(tag, "<table"); | |||
} | |||
@@ -94,12 +94,6 @@ public class HtmlDecoratorTest { | |||
); | |||
} | |||
@Test | |||
public void getCss() { | |||
assertThat(HtmlDecorator.getCss().length()).isGreaterThan(100); | |||
assertThat(HtmlDecorator.getCss()).contains(".code"); | |||
} | |||
public void assertContains(String html, String... strings) { | |||
for (String string : strings) { | |||
assertThat(html).contains(string); |
@@ -59,7 +59,6 @@ public class HtmlRendererTest { | |||
String html = new HtmlRenderer().render(new FileReader(java), Arrays.asList(javaKeywordTokenizer)); | |||
assertThat(html).contains("<html>"); | |||
assertThat(html).contains("<style"); | |||
assertThat(html).contains("<table class=\"code\""); | |||
assertThat(html).contains("public"); | |||
assertThat(html).contains("class"); |