diff options
6 files changed, 157 insertions, 7 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/source/CharactersReader.java b/sonar-core/src/main/java/org/sonar/core/source/CharactersReader.java index fca2c657582..907a38c54d8 100644 --- a/sonar-core/src/main/java/org/sonar/core/source/CharactersReader.java +++ b/sonar-core/src/main/java/org/sonar/core/source/CharactersReader.java @@ -30,7 +30,7 @@ import java.util.Deque; */ public class CharactersReader { - private static final int END_OF_STREAM = -1; + public static final int END_OF_STREAM = -1; private final BufferedReader stringBuffer; private final Deque<String> openTags; diff --git a/sonar-core/src/main/java/org/sonar/core/source/HtmlTextWrapper.java b/sonar-core/src/main/java/org/sonar/core/source/HtmlTextWrapper.java index f738998fe86..ce2a9ad4880 100644 --- a/sonar-core/src/main/java/org/sonar/core/source/HtmlTextWrapper.java +++ b/sonar-core/src/main/java/org/sonar/core/source/HtmlTextWrapper.java @@ -29,6 +29,8 @@ import java.io.StringReader; import java.util.Collection; import java.util.Collections; +import static org.sonar.core.source.CharactersReader.END_OF_STREAM; + /** * @since 3.6 */ @@ -40,7 +42,7 @@ public class HtmlTextWrapper { public static final char CR_END_OF_LINE = '\r'; public static final char LF_END_OF_LINE = '\n'; - public String wrapTextWithHtml(String text, HighlightingContext context) throws IOException { + public String wrapTextWithHtml(String text, HighlightingContext context) { StringBuilder decoratedText = new StringBuilder(); @@ -73,6 +75,12 @@ public class HtmlTextWrapper { decoratedText.append((char) charsReader.getCurrentValue()); } + + if(shouldClosePendingTags(charsReader)) { + closeCurrentSyntaxTags(charsReader, decoratedText); + decoratedText.append(CLOSE_TABLE_LINE); + } + } catch (IOException exception) { String errorMsg = "An exception occurred while highlighting the syntax of one of the project's files"; LoggerFactory.getLogger(HtmlTextWrapper.class).error(errorMsg); @@ -92,16 +100,17 @@ public class HtmlTextWrapper { return context.getLowerBoundsDefinitions().get(currentIndex); } - public boolean shouldClosePendingTags(CharactersReader context) { + private boolean shouldClosePendingTags(CharactersReader context) { return context.getCurrentValue() == CR_END_OF_LINE - || (context.getCurrentValue() == LF_END_OF_LINE && context.getPreviousValue() != CR_END_OF_LINE); + || (context.getCurrentValue() == LF_END_OF_LINE && context.getPreviousValue() != CR_END_OF_LINE) + || (context.getCurrentValue() == END_OF_STREAM && context.getPreviousValue() != LF_END_OF_LINE); } - public boolean shouldReopenPendingTags(CharactersReader context) { + private boolean shouldReopenPendingTags(CharactersReader context) { return context.getPreviousValue() == LF_END_OF_LINE && context.getCurrentValue() != LF_END_OF_LINE; } - public boolean shouldStartNewLine(CharactersReader context) { + private boolean shouldStartNewLine(CharactersReader context) { return context.getPreviousValue() == LF_END_OF_LINE || context.getCurrentIndex() == 0; } diff --git a/sonar-core/src/main/java/org/sonar/core/source/SyntaxHighlighter.java b/sonar-core/src/main/java/org/sonar/core/source/SyntaxHighlighter.java new file mode 100644 index 00000000000..4913de6810c --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/source/SyntaxHighlighter.java @@ -0,0 +1,51 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ + +package org.sonar.core.source; + +import org.sonar.core.persistence.MyBatis; +import org.sonar.core.source.jdbc.SnapshotDataDao; +import org.sonar.core.source.jdbc.SnapshotSourceDao; + +/** + * @since 3.6 + */ +public class SyntaxHighlighter { + + private final SnapshotSourceDao snapshotSourceDao; + private final SnapshotDataDao snapshotDataDao; + + public SyntaxHighlighter(MyBatis myBatis) { + snapshotSourceDao = new SnapshotSourceDao(myBatis); + snapshotDataDao = new SnapshotDataDao(myBatis); + } + + public String getHighlightedSourceAsHtml(long snapshotId) { + + String snapshotSource = snapshotSourceDao.selectSnapshotSource(snapshotId); + String highlightingRules = snapshotDataDao.selectSnapshotData(snapshotId); + + HighlightingContext highlightingContext = HighlightingContext.buildFrom(highlightingRules); + + HtmlTextWrapper textWrapper = new HtmlTextWrapper(); + + return textWrapper.wrapTextWithHtml(snapshotSource, highlightingContext); + } +} diff --git a/sonar-core/src/test/java/org/sonar/core/source/HtmlTextWrapperTest.java b/sonar-core/src/test/java/org/sonar/core/source/HtmlTextWrapperTest.java index 4d3e3d680ba..56f926dda90 100644 --- a/sonar-core/src/test/java/org/sonar/core/source/HtmlTextWrapperTest.java +++ b/sonar-core/src/test/java/org/sonar/core/source/HtmlTextWrapperTest.java @@ -38,7 +38,7 @@ public class HtmlTextWrapperTest { HtmlTextWrapper htmlTextWrapper = new HtmlTextWrapper(); String htmlOutput = htmlTextWrapper.wrapTextWithHtml(packageDeclaration, context); - assertThat(htmlOutput).isEqualTo("<tr><td><span class=\"k\">package</span> org.sonar.core.source;"); + assertThat(htmlOutput).isEqualTo("<tr><td><span class=\"k\">package</span> org.sonar.core.source;</td></tr>"); } @Test @@ -133,4 +133,30 @@ public class HtmlTextWrapperTest { "<tr><td>}</td></tr>" + CR_END_OF_LINE + LF_END_OF_LINE ); } + + @Test + public void should_close_tags_at_end_of_file() throws Exception { + + String classDeclarationSample = + "/*" + LF_END_OF_LINE + + " * Header" + LF_END_OF_LINE + + " */" + LF_END_OF_LINE + + LF_END_OF_LINE + + "public class HelloWorld {" + LF_END_OF_LINE + + "}"; + + HighlightingContext context = HighlightingContext.buildFrom("0,16,cppd;18,25,k;25,31,k;"); + + HtmlTextWrapper htmlTextWrapper = new HtmlTextWrapper(); + String htmlOutput = htmlTextWrapper.wrapTextWithHtml(classDeclarationSample, context); + + assertThat(htmlOutput).isEqualTo( + "<tr><td><span class=\"cppd\">/*</span></td></tr>" + LF_END_OF_LINE + + "<tr><td><span class=\"cppd\"> * Header</span></td></tr>" + LF_END_OF_LINE + + "<tr><td><span class=\"cppd\"> */</span></td></tr>" + LF_END_OF_LINE + + "<tr><td></td></tr>" + LF_END_OF_LINE + + "<tr><td><span class=\"k\">public </span><span class=\"k\">class </span>HelloWorld {</td></tr>" + LF_END_OF_LINE + + "<tr><td>}</td></tr>" + ); + } } diff --git a/sonar-core/src/test/java/org/sonar/core/source/SyntaxHighlighterTest.java b/sonar-core/src/test/java/org/sonar/core/source/SyntaxHighlighterTest.java new file mode 100644 index 00000000000..d1fb7111c81 --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/core/source/SyntaxHighlighterTest.java @@ -0,0 +1,54 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ + +package org.sonar.core.source; + +import org.junit.Before; +import org.junit.Test; +import org.sonar.core.persistence.AbstractDaoTestCase; + +import static org.fest.assertions.Assertions.assertThat; +import static org.sonar.core.source.HtmlTextWrapper.LF_END_OF_LINE; + +public class SyntaxHighlighterTest extends AbstractDaoTestCase { + + @Before + public void setUpDatasets() { + setupData("shared"); + } + + @Test + public void should_highlight_source_with_html() throws Exception { + + SyntaxHighlighter highlighter = new SyntaxHighlighter(getMyBatis()); + + String highlightedSource = highlighter.getHighlightedSourceAsHtml(11L); + + assertThat(highlightedSource).isEqualTo( + "<tr><td><span class=\"cppd\">/*</span></td></tr>" + LF_END_OF_LINE + + "<tr><td><span class=\"cppd\"> * Header</span></td></tr>" + LF_END_OF_LINE + + "<tr><td><span class=\"cppd\"> */</span></td></tr>" + LF_END_OF_LINE + + "<tr><td></td></tr>" + LF_END_OF_LINE + + "<tr><td><span class=\"k\">public </span><span class=\"k\">class </span>HelloWorld {</td></tr>" + LF_END_OF_LINE + + "<tr><td>}</td></tr>" + ); + } + +} diff --git a/sonar-core/src/test/resources/org/sonar/core/source/SyntaxHighlighterTest/shared.xml b/sonar-core/src/test/resources/org/sonar/core/source/SyntaxHighlighterTest/shared.xml new file mode 100644 index 00000000000..086fd88f9e4 --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/source/SyntaxHighlighterTest/shared.xml @@ -0,0 +1,10 @@ +<dataset> + + <projects id="1" kee="org.apache.struts:struts" enabled="[true]"/> + + <snapshots id="11" project_id="1" islast="[true]" /> + + <snapshot_data id="101" resource_id="1" snapshot_id="11" data="0,16,cppd;18,25,k;25,31,k;" data_type="highlight_syntax" /> + + <snapshot_sources id="101" snapshot_id="11" data="/* * Header */ public class HelloWorld { }" /> +</dataset>
\ No newline at end of file |