summaryrefslogtreecommitdiffstats
path: root/sonar-core/src
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2014-01-27 12:58:11 +0100
committerJulien Lancelot <julien.lancelot@sonarsource.com>2014-01-27 12:58:24 +0100
commit9a78242133b1c0ebd76d7d5e638893428237798a (patch)
tree2c5353cf5e96b4517891c47e9d58c3c2a722b39e /sonar-core/src
parentdf04c42a7d286b89cf9fa602b4aa03af0125dae9 (diff)
downloadsonarqube-9a78242133b1c0ebd76d7d5e638893428237798a.tar.gz
sonarqube-9a78242133b1c0ebd76d7d5e638893428237798a.zip
SourcesShowWS : Add from and to parameter
Diffstat (limited to 'sonar-core/src')
-rw-r--r--sonar-core/src/main/java/org/sonar/core/source/HtmlSourceDecorator.java10
-rw-r--r--sonar-core/src/main/java/org/sonar/core/source/HtmlTextDecorator.java33
-rw-r--r--sonar-core/src/test/java/org/sonar/core/source/HtmlSourceDecoratorTest.java15
-rw-r--r--sonar-core/src/test/java/org/sonar/core/source/HtmlTextDecoratorTest.java100
4 files changed, 141 insertions, 17 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/source/HtmlSourceDecorator.java b/sonar-core/src/main/java/org/sonar/core/source/HtmlSourceDecorator.java
index 6956e297fa4..4ac16549bf2 100644
--- a/sonar-core/src/main/java/org/sonar/core/source/HtmlSourceDecorator.java
+++ b/sonar-core/src/main/java/org/sonar/core/source/HtmlSourceDecorator.java
@@ -48,13 +48,13 @@ public class HtmlSourceDecorator implements ServerComponent {
}
@CheckForNull
- public List<String> getDecoratedSourceAsHtml(String componentKey) {
+ public List<String> getDecoratedSourceAsHtml(String componentKey, @Nullable Integer from, @Nullable Integer to) {
SqlSession session = mybatis.openSession();
try {
Collection<SnapshotDataDto> snapshotDataEntries = snapshotDataDao.selectSnapshotDataByComponentKey(componentKey, highlightingDataTypes(), session);
if (!snapshotDataEntries.isEmpty()) {
String snapshotSource = snapshotSourceDao.selectSnapshotSourceByComponentKey(componentKey, session);
- return decorate(snapshotSource, snapshotDataEntries);
+ return decorate(snapshotSource, snapshotDataEntries, from, to);
}
return null;
} finally {
@@ -68,14 +68,14 @@ public class HtmlSourceDecorator implements ServerComponent {
if (!snapshotDataEntries.isEmpty()) {
String snapshotSource = snapshotSourceDao.selectSnapshotSource(snapshotId);
if (snapshotSource != null) {
- return decorate(snapshotSource, snapshotDataEntries);
+ return decorate(snapshotSource, snapshotDataEntries, null, null);
}
}
return null;
}
@CheckForNull
- private List<String> decorate(@Nullable String snapshotSource, Collection<SnapshotDataDto> snapshotDataEntries) {
+ private List<String> decorate(@Nullable String snapshotSource, Collection<SnapshotDataDto> snapshotDataEntries, @Nullable Integer from, @Nullable Integer to) {
if (snapshotSource != null) {
DecorationDataHolder decorationDataHolder = new DecorationDataHolder();
for (SnapshotDataDto snapshotDataEntry : snapshotDataEntries) {
@@ -83,7 +83,7 @@ public class HtmlSourceDecorator implements ServerComponent {
}
HtmlTextDecorator textDecorator = new HtmlTextDecorator();
- return textDecorator.decorateTextWithHtml(snapshotSource, decorationDataHolder);
+ return textDecorator.decorateTextWithHtml(snapshotSource, decorationDataHolder, from, to);
}
return null;
}
diff --git a/sonar-core/src/main/java/org/sonar/core/source/HtmlTextDecorator.java b/sonar-core/src/main/java/org/sonar/core/source/HtmlTextDecorator.java
index bc8a245dac3..d186f1c5bc0 100644
--- a/sonar-core/src/main/java/org/sonar/core/source/HtmlTextDecorator.java
+++ b/sonar-core/src/main/java/org/sonar/core/source/HtmlTextDecorator.java
@@ -23,6 +23,8 @@ import com.google.common.collect.Lists;
import com.google.common.io.Closeables;
import org.slf4j.LoggerFactory;
+import javax.annotation.Nullable;
+
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
@@ -46,12 +48,16 @@ class HtmlTextDecorator {
static final String ENCODED_AMPERSAND = "&amp;";
List<String> decorateTextWithHtml(String text, DecorationDataHolder decorationDataHolder) {
+ return decorateTextWithHtml(text, decorationDataHolder, null, null);
+ }
+
+ List<String> decorateTextWithHtml(String text, DecorationDataHolder decorationDataHolder, @Nullable Integer from, @Nullable Integer to) {
StringBuilder currentHtmlLine = new StringBuilder();
List<String> decoratedHtmlLines = Lists.newArrayList();
+ int currentLine = 1;
BufferedReader stringBuffer = null;
-
try {
stringBuffer = new BufferedReader(new StringReader(text));
@@ -60,7 +66,8 @@ class HtmlTextDecorator {
while (charsReader.readNextChar()) {
if (shouldStartNewLine(charsReader)) {
- decoratedHtmlLines.add(currentHtmlLine.toString());
+ addLine(decoratedHtmlLines, currentHtmlLine.toString(), currentLine, from, to);
+ currentLine++;
currentHtmlLine = new StringBuilder();
if (shouldReopenPendingTags(charsReader)) {
reopenCurrentSyntaxTags(charsReader, currentHtmlLine);
@@ -86,10 +93,13 @@ class HtmlTextDecorator {
closeCurrentSyntaxTags(charsReader, currentHtmlLine);
if (shouldStartNewLine(charsReader)) {
- decoratedHtmlLines.add(currentHtmlLine.toString());
- decoratedHtmlLines.add("");
+ addLine(decoratedHtmlLines, currentHtmlLine.toString(), currentLine, from, to);
+ currentLine++;
+ addLine(decoratedHtmlLines, "", currentLine, from, to);
+ currentLine++;
} else if (currentHtmlLine.length() > 0) {
- decoratedHtmlLines.add(currentHtmlLine.toString());
+ addLine(decoratedHtmlLines, currentHtmlLine.toString(), currentLine, from, to);
+ currentLine++;
}
} catch (IOException exception) {
@@ -103,6 +113,13 @@ class HtmlTextDecorator {
return decoratedHtmlLines;
}
+ private void addLine(List<String> decoratedHtmlLines, String line, int currentLine, @Nullable Integer from, @Nullable Integer to) {
+ if ((from == null || currentLine >= from)
+ && (to == null || to >= currentLine)) {
+ decoratedHtmlLines.add(line);
+ }
+ }
+
private char[] normalize(char currentChar) {
char[] normalizedChars;
if (currentChar == HTML_OPENING) {
@@ -112,7 +129,7 @@ class HtmlTextDecorator {
} else if (currentChar == AMPERSAND) {
normalizedChars = ENCODED_AMPERSAND.toCharArray();
} else {
- normalizedChars = new char[] {currentChar};
+ normalizedChars = new char[]{currentChar};
}
return normalizedChars;
}
@@ -158,7 +175,7 @@ class HtmlTextDecorator {
}
private void closeCompletedTags(CharactersReader charactersReader, int numberOfTagsToClose,
- StringBuilder decoratedText) {
+ StringBuilder decoratedText) {
for (int i = 0; i < numberOfTagsToClose; i++) {
injectClosingHtml(decoratedText);
charactersReader.removeLastOpenTag();
@@ -166,7 +183,7 @@ class HtmlTextDecorator {
}
private void openNewTags(CharactersReader charactersReader, Collection<String> tagsToOpen,
- StringBuilder decoratedText) {
+ StringBuilder decoratedText) {
for (String tagToOpen : tagsToOpen) {
injectOpeningHtmlForRule(tagToOpen, decoratedText);
charactersReader.registerOpenTag(tagToOpen);
diff --git a/sonar-core/src/test/java/org/sonar/core/source/HtmlSourceDecoratorTest.java b/sonar-core/src/test/java/org/sonar/core/source/HtmlSourceDecoratorTest.java
index 5673ab6d125..cd623f77ded 100644
--- a/sonar-core/src/test/java/org/sonar/core/source/HtmlSourceDecoratorTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/source/HtmlSourceDecoratorTest.java
@@ -64,7 +64,7 @@ public class HtmlSourceDecoratorTest extends AbstractDaoTestCase {
@Test
public void highlight_syntax_with_html_from_component() throws Exception {
- List<String> decoratedSource = sourceDecorator.getDecoratedSourceAsHtml("org.apache.struts:struts:Dispatcher");
+ List<String> decoratedSource = sourceDecorator.getDecoratedSourceAsHtml("org.apache.struts:struts:Dispatcher", null, null);
assertThat(decoratedSource).containsExactly(
"<span class=\"cppd\">/*</span>",
@@ -77,6 +77,13 @@ public class HtmlSourceDecoratorTest extends AbstractDaoTestCase {
}
@Test
+ public void highlight_syntax_with_html_from_component_on_given_lines() throws Exception {
+ assertThat(sourceDecorator.getDecoratedSourceAsHtml("org.apache.struts:struts:Dispatcher", null, 2)).hasSize(2);
+ assertThat(sourceDecorator.getDecoratedSourceAsHtml("org.apache.struts:struts:Dispatcher", 2, null)).hasSize(5);
+ assertThat(sourceDecorator.getDecoratedSourceAsHtml("org.apache.struts:struts:Dispatcher", 1, 2)).hasSize(2);
+ }
+
+ @Test
public void mark_symbols_with_html() throws Exception {
List<String> decoratedSource = sourceDecorator.getDecoratedSourceAsHtml(12L);
@@ -92,7 +99,7 @@ public class HtmlSourceDecoratorTest extends AbstractDaoTestCase {
@Test
public void mark_symbols_with_html_from_component() throws Exception {
- List<String> decoratedSource = sourceDecorator.getDecoratedSourceAsHtml("org.apache.struts:struts:VelocityManager");
+ List<String> decoratedSource = sourceDecorator.getDecoratedSourceAsHtml("org.apache.struts:struts:VelocityManager", null, null);
assertThat(decoratedSource).containsExactly(
"/*",
@@ -125,7 +132,7 @@ public class HtmlSourceDecoratorTest extends AbstractDaoTestCase {
@Test
public void decorate_source_with_multiple_decoration_strategies_from_component() throws Exception {
- List<String> decoratedSource = sourceDecorator.getDecoratedSourceAsHtml("org.apache.struts:struts:DebuggingInterceptor");
+ List<String> decoratedSource = sourceDecorator.getDecoratedSourceAsHtml("org.apache.struts:struts:DebuggingInterceptor", null, null);
assertThat(decoratedSource).containsExactly(
"<span class=\"cppd\">/*</span>",
@@ -162,7 +169,7 @@ public class HtmlSourceDecoratorTest extends AbstractDaoTestCase {
HtmlSourceDecorator sourceDecorator = new HtmlSourceDecorator(mock(MyBatis.class), snapshotSourceDao, snapshotDataDao);
- sourceDecorator.getDecoratedSourceAsHtml("org.apache.struts:struts:DebuggingInterceptor");
+ sourceDecorator.getDecoratedSourceAsHtml("org.apache.struts:struts:DebuggingInterceptor", null, null);
verify(snapshotDataDao, times(1)).selectSnapshotDataByComponentKey(eq("org.apache.struts:struts:DebuggingInterceptor"), eq(Lists.newArrayList("highlight_syntax", "symbol")),
any(SqlSession.class));
diff --git a/sonar-core/src/test/java/org/sonar/core/source/HtmlTextDecoratorTest.java b/sonar-core/src/test/java/org/sonar/core/source/HtmlTextDecoratorTest.java
index 3eb437e2b90..59e53b5ba0a 100644
--- a/sonar-core/src/test/java/org/sonar/core/source/HtmlTextDecoratorTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/source/HtmlTextDecoratorTest.java
@@ -301,4 +301,104 @@ public class HtmlTextDecoratorTest {
""
);
}
+
+ @Test
+ public void begin_from_given_line() throws Exception {
+
+ String javadocWithHtml =
+ "/**\n" +
+ " * Provides a basic framework to sequentially read any kind of character stream in order to feed a generic OUTPUT.\n" +
+ " * \n" +
+ " * This framework can used for instance in order to :\n" +
+ " * <ul>\n" +
+ " * <li>Create a lexer in charge to generate a list of tokens from a character stream</li>\n" +
+ " * <li>Create a source code syntax highligther in charge to decorate a source code with HTML tags</li>\n" +
+ " * <li>Create a javadoc generator</li>\n" +
+ " * <li>...</li>\n" +
+ " * </ul>\n" +
+ " */\n";
+
+ DecorationDataHolder decorationData = new DecorationDataHolder();
+ decorationData.loadSyntaxHighlightingData("0,453,cppd;");
+
+ HtmlTextDecorator htmlTextDecorator = new HtmlTextDecorator();
+ List<String> htmlOutput = htmlTextDecorator.decorateTextWithHtml(javadocWithHtml, decorationData, 4, null);
+ assertThat(htmlOutput).hasSize(9);
+
+ // Begin since line 4
+ assertThat(htmlOutput).containsExactly(
+ "<span class=\"cppd\"> * This framework can used for instance in order to :</span>",
+ "<span class=\"cppd\"> * &lt;ul&gt;</span>",
+ "<span class=\"cppd\"> * &lt;li&gt;Create a lexer in charge to generate a list of tokens from a character stream&lt;/li&gt;</span>",
+ "<span class=\"cppd\"> * &lt;li&gt;Create a source code syntax highligther in charge to decorate a source code with HTML tags&lt;/li&gt;</span>",
+ "<span class=\"cppd\"> * &lt;li&gt;Create a javadoc generator&lt;/li&gt;</span>",
+ "<span class=\"cppd\"> * &lt;li&gt;...&lt;/li&gt;</span>",
+ "<span class=\"cppd\"> * &lt;/ul&gt;</span>",
+ "<span class=\"cppd\"> */</span>",
+ "");
+ }
+
+ @Test
+ public void end_to_given_line() throws Exception {
+
+ String javadocWithHtml =
+ "/**\n" +
+ " * Provides a basic framework to sequentially read any kind of character stream in order to feed a generic OUTPUT.\n" +
+ " * \n" +
+ " * This framework can used for instance in order to :\n" +
+ " * <ul>\n" +
+ " * <li>Create a lexer in charge to generate a list of tokens from a character stream</li>\n" +
+ " * <li>Create a source code syntax highligther in charge to decorate a source code with HTML tags</li>\n" +
+ " * <li>Create a javadoc generator</li>\n" +
+ " * <li>...</li>\n" +
+ " * </ul>\n" +
+ " */\n";
+
+ DecorationDataHolder decorationData = new DecorationDataHolder();
+ decorationData.loadSyntaxHighlightingData("0,453,cppd;");
+
+ HtmlTextDecorator htmlTextDecorator = new HtmlTextDecorator();
+ List<String> htmlOutput = htmlTextDecorator.decorateTextWithHtml(javadocWithHtml, decorationData, null, 4);
+ assertThat(htmlOutput).hasSize(4);
+
+ // End at line 4
+ assertThat(htmlOutput).containsExactly(
+ "<span class=\"cppd\">/**</span>",
+ "<span class=\"cppd\"> * Provides a basic framework to sequentially read any kind of character stream in order to feed a generic OUTPUT.</span>",
+ "<span class=\"cppd\"> * </span>",
+ "<span class=\"cppd\"> * This framework can used for instance in order to :</span>");
+ }
+
+ @Test
+ public void return_code_from_given_lint_given_end_line() throws Exception {
+
+ String javadocWithHtml =
+ "/**\n" +
+ " * Provides a basic framework to sequentially read any kind of character stream in order to feed a generic OUTPUT.\n" +
+ " * \n" +
+ " * This framework can used for instance in order to :\n" +
+ " * <ul>\n" +
+ " * <li>Create a lexer in charge to generate a list of tokens from a character stream</li>\n" +
+ " * <li>Create a source code syntax highligther in charge to decorate a source code with HTML tags</li>\n" +
+ " * <li>Create a javadoc generator</li>\n" +
+ " * <li>...</li>\n" +
+ " * </ul>\n" +
+ " */\n";
+
+ DecorationDataHolder decorationData = new DecorationDataHolder();
+ decorationData.loadSyntaxHighlightingData("0,453,cppd;");
+
+ HtmlTextDecorator htmlTextDecorator = new HtmlTextDecorator();
+ List<String> htmlOutput = htmlTextDecorator.decorateTextWithHtml(javadocWithHtml, decorationData, 4, 8);
+ assertThat(htmlOutput).hasSize(5);
+
+ // Begin at line 4 and finish at line 8
+ assertThat(htmlOutput).containsExactly(
+ "<span class=\"cppd\"> * This framework can used for instance in order to :</span>",
+ "<span class=\"cppd\"> * &lt;ul&gt;</span>",
+ "<span class=\"cppd\"> * &lt;li&gt;Create a lexer in charge to generate a list of tokens from a character stream&lt;/li&gt;</span>",
+ "<span class=\"cppd\"> * &lt;li&gt;Create a source code syntax highligther in charge to decorate a source code with HTML tags&lt;/li&gt;</span>",
+ "<span class=\"cppd\"> * &lt;li&gt;Create a javadoc generator&lt;/li&gt;</span>"
+ );
+ }
}