From 591efdc554efe55cb0ab7f88e6d4c45b247987c3 Mon Sep 17 00:00:00 2001 From: Freddy Mallet Date: Sat, 23 Apr 2011 00:47:42 +0200 Subject: [PATCH] SONAR-2384 First implementation : Create a markdown dedicated to Sonar needs --- .../java/org/sonar/channel/RegexChannel.java | 42 ++++++++++++++ .../org/sonar/channel/RegexChannelTest.java | 58 +++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 sonar-channel/src/main/java/org/sonar/channel/RegexChannel.java create mode 100644 sonar-channel/src/test/java/org/sonar/channel/RegexChannelTest.java 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 extends Channel { + + 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 dispatcher = new ChannelDispatcher(new MyWordChannel(), new BlackholeChannel()); + StringBuilder output = new StringBuilder(); + dispatcher.consume(new CodeReader("my word"), output); + assertThat(output.toString(), is("my word")); + } + + private class MyWordChannel extends RegexChannel { + + public MyWordChannel() { + super("\\w++"); + } + + @Override + protected void consume(CharSequence token, StringBuilder output) { + output.append("" + token + ""); + } + } + + class BlackholeChannel extends Channel { + + @Override + public boolean consume(CodeReader code, StringBuilder output) { + output.append((char) code.pop()); + return true; + } + } + +} -- 2.39.5