diff options
3 files changed, 46 insertions, 38 deletions
diff --git a/sonar-colorizer/src/main/java/org/sonar/colorizer/HtmlDecorator.java b/sonar-colorizer/src/main/java/org/sonar/colorizer/HtmlDecorator.java index 611a57d1a4c..0d0b4386118 100644 --- a/sonar-colorizer/src/main/java/org/sonar/colorizer/HtmlDecorator.java +++ b/sonar-colorizer/src/main/java/org/sonar/colorizer/HtmlDecorator.java @@ -31,9 +31,9 @@ public class HtmlDecorator extends Tokenizer { private HtmlOptions options; private int lineId = 1; - private boolean checked = false; - private boolean beginOfLine = true; - private boolean endOfLine = false; + + private static final int LF = (int) '\n'; + private static final int CR = (int) '\r'; public HtmlDecorator(HtmlOptions options) { this.options = options; @@ -53,6 +53,7 @@ public class HtmlDecorator extends Tokenizer { sb.append(options.getTableId()); } sb.append("\"><tbody>"); + sb.append("<tr id=\"" + lineId++ + "\"><td><pre>"); return sb.toString(); } @@ -66,46 +67,28 @@ public class HtmlDecorator extends Tokenizer { } public String getTagBefore() { - StringBuilder sb = new StringBuilder(); - if (beginOfLine) { - sb.append("<tr id=\""); - sb.append(lineId++); - sb.append("\"><td><pre>"); - } - return sb.toString(); + return "<tr id=\"" + lineId++ + "\"><td><pre>"; } public String getTagAfter() { - if (endOfLine) { - return "</pre></td></tr>"; - } - return ""; - } - - private boolean hasNextToken(CodeReader code) { - if (checked) { - checked = false; - return false; - } - int lastChar = code.lastChar(); - beginOfLine = (lastChar == -1 || lastChar == (int) '\n'); - - int peek = code.peek(); - endOfLine = (peek == (int) '\n' || peek == -1); - - checked = beginOfLine || endOfLine; - return checked; + return "</pre></td></tr>"; } @Override public boolean consume(CodeReader code, HtmlCodeBuilder codeBuilder) { - if (hasNextToken(code)) { - codeBuilder.appendWithoutTransforming(getTagBefore()); + int currentChar = code.peek(); + + if (currentChar == LF) { codeBuilder.appendWithoutTransforming(getTagAfter()); + codeBuilder.appendWithoutTransforming(getTagBefore()); + } + + if (currentChar == LF || currentChar == CR) { + code.pop(); return true; - } else { - return false; } + + return false; } public static String getCss() { diff --git a/sonar-colorizer/src/test/java/org/sonar/colorizer/CodeColorizerTest.java b/sonar-colorizer/src/test/java/org/sonar/colorizer/CodeColorizerTest.java index ee2d73617ea..9740e95b700 100644 --- a/sonar-colorizer/src/test/java/org/sonar/colorizer/CodeColorizerTest.java +++ b/sonar-colorizer/src/test/java/org/sonar/colorizer/CodeColorizerTest.java @@ -26,7 +26,11 @@ import static org.hamcrest.number.OrderingComparisons.greaterThan; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; -import java.io.*; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.io.Reader; +import java.io.StringReader; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -37,7 +41,6 @@ import java.util.concurrent.Future; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; -import org.junit.Ignore; import org.junit.Test; public class CodeColorizerTest { @@ -53,7 +56,6 @@ public class CodeColorizerTest { } @Test - @Ignore("see http://jira.codehaus.org/browse/SONAR-2115") public void shouldSupportWindowsEndOfLines() throws IOException { StringBuilder windowsFile = new StringBuilder(); List<String> lines = FileUtils.readLines(FileUtils.toFile(getClass().getResource("/org/sonar/colorizer/samples/Sample.java"))); diff --git a/sonar-colorizer/src/test/java/org/sonar/colorizer/HtmlDecoratorTest.java b/sonar-colorizer/src/test/java/org/sonar/colorizer/HtmlDecoratorTest.java index c5b1d1283a9..a49dafe025b 100644 --- a/sonar-colorizer/src/test/java/org/sonar/colorizer/HtmlDecoratorTest.java +++ b/sonar-colorizer/src/test/java/org/sonar/colorizer/HtmlDecoratorTest.java @@ -19,12 +19,15 @@ */ package org.sonar.colorizer; +import static org.hamcrest.Matchers.is; import static org.hamcrest.core.IsNot.not; import static org.hamcrest.number.OrderingComparisons.greaterThan; import static org.junit.Assert.assertThat; -import org.junit.Test; import static org.junit.internal.matchers.StringContains.containsString; +import org.junit.Test; +import org.sonar.channel.CodeReader; + public class HtmlDecoratorTest { @Test @@ -74,12 +77,32 @@ public class HtmlDecoratorTest { } @Test + public void shouldAddTagsBetweenEachLine() { + HtmlOptions options = new HtmlOptions().setGenerateTable(true).setGenerateHtmlHeader(false); + HtmlDecorator decorator = new HtmlDecorator(options); + CodeReader code = new CodeReader("\n\r\n"); + HtmlCodeBuilder output = new HtmlCodeBuilder(); + + output.appendWithoutTransforming(decorator.getTagBeginOfFile()); + assertThat(decorator.consume(code, output), is(true)); + assertThat(decorator.consume(code, output), is(true)); + assertThat(decorator.consume(code, output), is(true)); + output.appendWithoutTransforming(decorator.getTagEndOfFile()); + + assertThat(output.toString(), is( + "<table class=\"code\" id=\"\"><tbody>" + + "<tr id=\"1\"><td><pre></pre></td></tr>" + + "<tr id=\"2\"><td><pre></pre></td></tr>" + + "<tr id=\"3\"><td><pre></pre></td></tr>" + + "</tbody></table>")); + } + + @Test public void getCss() { assertThat(HtmlDecorator.getCss().length(), greaterThan(100)); assertThat(HtmlDecorator.getCss(), containsString(".code")); } - public void assertContains(String html, String... strings) { for (String string : strings) { assertThat(html, containsString(string)); |