aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-channel/src
diff options
context:
space:
mode:
authorFreddy Mallet <freddy.mallet@gmail.com>2011-04-23 00:47:42 +0200
committerFreddy Mallet <freddy.mallet@gmail.com>2011-04-23 00:47:42 +0200
commit591efdc554efe55cb0ab7f88e6d4c45b247987c3 (patch)
treea9cc0a95bc853488b2fb498d07f1506dce2a7a47 /sonar-channel/src
parent84159b5d85a0b99790ba8ea90e463076d541ed5c (diff)
downloadsonarqube-591efdc554efe55cb0ab7f88e6d4c45b247987c3.tar.gz
sonarqube-591efdc554efe55cb0ab7f88e6d4c45b247987c3.zip
SONAR-2384 First implementation : Create a markdown dedicated to Sonar needs
Diffstat (limited to 'sonar-channel/src')
-rw-r--r--sonar-channel/src/main/java/org/sonar/channel/RegexChannel.java42
-rw-r--r--sonar-channel/src/test/java/org/sonar/channel/RegexChannelTest.java58
2 files changed, 100 insertions, 0 deletions
diff --git a/sonar-channel/src/main/java/org/sonar/channel/RegexChannel.java b/sonar-channel/src/main/java/org/sonar/channel/RegexChannel.java
new file mode 100644
index 00000000000..5b2a744a227
--- /dev/null
+++ b/sonar-channel/src/main/java/org/sonar/channel/RegexChannel.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2010 SonarSource SA
+ * All rights reserved
+ * mailto:contact AT sonarsource DOT com
+ */
+package org.sonar.channel;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public abstract class RegexChannel<OUTPUT> extends Channel<OUTPUT> {
+
+ private final StringBuilder tmpBuilder = new StringBuilder();
+ private final Matcher matcher;
+ private final String regex;
+
+ public RegexChannel(String regex) {
+ matcher = Pattern.compile(regex).matcher("");
+ this.regex = regex;
+ }
+
+ @Override
+ public final boolean consume(CodeReader code, OUTPUT output) {
+ try {
+ if (code.popTo(matcher, tmpBuilder) > 0) {
+ consume(tmpBuilder, output);
+ tmpBuilder.delete(0, tmpBuilder.length());
+ return true;
+ }
+ return false;
+ } catch (StackOverflowError e) {
+ throw new RuntimeException(
+ "The regular expression "
+ + regex
+ + " has led to a stack overflow error. "
+ + "This error is certainly due to an inefficient use of alternations. See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5050507",
+ e);
+ }
+ }
+
+ protected abstract void consume(CharSequence token, OUTPUT output);
+}
diff --git a/sonar-channel/src/test/java/org/sonar/channel/RegexChannelTest.java b/sonar-channel/src/test/java/org/sonar/channel/RegexChannelTest.java
new file mode 100644
index 00000000000..7fb5c3a11ab
--- /dev/null
+++ b/sonar-channel/src/test/java/org/sonar/channel/RegexChannelTest.java
@@ -0,0 +1,58 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2011 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.channel;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+
+import org.junit.Test;
+
+public class RegexChannelTest {
+
+ @Test
+ public void shouldMatch() {
+ ChannelDispatcher<StringBuilder> dispatcher = new ChannelDispatcher<StringBuilder>(new MyWordChannel(), new BlackholeChannel());
+ StringBuilder output = new StringBuilder();
+ dispatcher.consume(new CodeReader("my word"), output);
+ assertThat(output.toString(), is("<w>my</w> <w>word</w>"));
+ }
+
+ private class MyWordChannel extends RegexChannel<StringBuilder> {
+
+ public MyWordChannel() {
+ super("\\w++");
+ }
+
+ @Override
+ protected void consume(CharSequence token, StringBuilder output) {
+ output.append("<w>" + token + "</w>");
+ }
+ }
+
+ class BlackholeChannel extends Channel<StringBuilder> {
+
+ @Override
+ public boolean consume(CodeReader code, StringBuilder output) {
+ output.append((char) code.pop());
+ return true;
+ }
+ }
+
+}