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;
sb.append(options.getTableId());
}
sb.append("\"><tbody>");
+ sb.append("<tr id=\"" + lineId++ + "\"><td><pre>");
return sb.toString();
}
}
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() {
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;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
-import org.junit.Ignore;
import org.junit.Test;
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")));
*/
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
assertNotContains(tag, "</body>", "</html>");
}
+ @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));