From bcc445118956633eb4084581a47edbe5672cefb4 Mon Sep 17 00:00:00 2001 From: simonbrandhof Date: Thu, 5 May 2011 15:08:04 +0200 Subject: Reduce technical debt --- .../java/org/sonar/markdown/HtmlListChannel.java | 5 +-- .../java/org/sonar/markdown/MarkdownEngine.java | 42 +++++++++++++--------- 2 files changed, 28 insertions(+), 19 deletions(-) (limited to 'sonar-markdown') diff --git a/sonar-markdown/src/main/java/org/sonar/markdown/HtmlListChannel.java b/sonar-markdown/src/main/java/org/sonar/markdown/HtmlListChannel.java index 0f71e4cda31..956bd1978e5 100644 --- a/sonar-markdown/src/main/java/org/sonar/markdown/HtmlListChannel.java +++ b/sonar-markdown/src/main/java/org/sonar/markdown/HtmlListChannel.java @@ -34,6 +34,7 @@ class HtmlListChannel extends Channel { try { if (code.getColumnPosition() == 0 && listElement.consume(code, output)) { while (endOfLine.consume(code, output) && listElement.consume(code, output)) { + // consume input } output.append(""); return true; @@ -52,7 +53,7 @@ class HtmlListChannel extends Channel { @Override protected void consume(CharSequence token, MarkdownOutput output) { - if ( !pendingListConstruction) { + if (!pendingListConstruction) { output.append("
    "); pendingListConstruction = true; } @@ -75,7 +76,7 @@ class HtmlListChannel extends Channel { } } - private class EndOfLine extends RegexChannel { + private static final class EndOfLine extends RegexChannel { public EndOfLine() { super("(\r?\n)|(\r)"); diff --git a/sonar-markdown/src/main/java/org/sonar/markdown/MarkdownEngine.java b/sonar-markdown/src/main/java/org/sonar/markdown/MarkdownEngine.java index 73c639e8182..13c80ce39ff 100644 --- a/sonar-markdown/src/main/java/org/sonar/markdown/MarkdownEngine.java +++ b/sonar-markdown/src/main/java/org/sonar/markdown/MarkdownEngine.java @@ -19,37 +19,45 @@ */ package org.sonar.markdown; -import java.util.ArrayList; -import java.util.List; - import org.sonar.channel.Channel; import org.sonar.channel.ChannelDispatcher; import org.sonar.channel.CodeReader; +import java.util.Arrays; +import java.util.List; + /** * Entry point of the Markdown library */ -public class MarkdownEngine { +public final class MarkdownEngine { - private MarkdownOutput output; private ChannelDispatcher dispatcher; private MarkdownEngine() { - output = new MarkdownOutput(); - List markdownChannels = new ArrayList(); - markdownChannels.add(new HtmlUrlChannel()); - markdownChannels.add(new HtmlEndOfLineChannel()); - markdownChannels.add(new HtmlEmphasisChannel()); - markdownChannels.add(new HtmlListChannel()); - markdownChannels.add(new HtmlCodeChannel()); - markdownChannels.add(new IdentifierAndNumberChannel()); - markdownChannels.add(new BlackholeChannel()); + List markdownChannels = Arrays.asList( + new HtmlUrlChannel(), + new HtmlEndOfLineChannel(), + new HtmlEmphasisChannel(), + new HtmlListChannel(), + new HtmlCodeChannel(), + new IdentifierAndNumberChannel(), + new BlackholeChannel()); dispatcher = new ChannelDispatcher(markdownChannels); } + private String convert(String input) { + CodeReader reader = new CodeReader(input); + try { + MarkdownOutput output = new MarkdownOutput(); + dispatcher.consume(reader, output); + return output.toString(); + + } finally { + reader.close(); + } + } + public static String convertToHtml(String input) { - MarkdownEngine engine = new MarkdownEngine(); - engine.dispatcher.consume(new CodeReader(input), engine.output); - return engine.output.toString(); + return new MarkdownEngine().convert(input); } } -- cgit v1.2.3