From b138564a95720fa1238256b219acb4c28019dae9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 8 Apr 2013 07:04:56 +0200 Subject: [PATCH] Add message cleaner. --- .../net/pterodactylus/irc/util/MessageCleaner.java | 171 +++++++++++++++++++++ .../pterodactylus/irc/util/MessageCleanerTest.java | 94 +++++++++++ 2 files changed, 265 insertions(+) create mode 100644 src/main/java/net/pterodactylus/irc/util/MessageCleaner.java create mode 100644 src/test/java/net/pterodactylus/irc/util/MessageCleanerTest.java diff --git a/src/main/java/net/pterodactylus/irc/util/MessageCleaner.java b/src/main/java/net/pterodactylus/irc/util/MessageCleaner.java new file mode 100644 index 0000000..6d0e3a2 --- /dev/null +++ b/src/main/java/net/pterodactylus/irc/util/MessageCleaner.java @@ -0,0 +1,171 @@ +/* + * XdccDownloader - MessageCleaner.java - Copyright © 2013 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.irc.util; + +import java.io.IOException; +import java.io.StringReader; +import java.util.EnumSet; +import java.util.Set; + +/** + * Removes formatting codes from IRC messages. + * + * @author David ‘Bombe’ Roden + */ +public class MessageCleaner { + + /** The kind of attributes to clear. */ + public enum Attributes { + + bold, + color, + clear + + } + + /** A default instance. */ + private static final MessageCleaner DEFAULT_INSTANCE = new MessageCleaner(); + + /** + * Cleans all formatting from the given line. + * + * @param line + * The line to remove all formatting from + * @return The given line with all formatting removed + */ + public String clean(String line) { + return clean(line, EnumSet.allOf(Attributes.class)); + } + + /** + * Cleans the given formattings from the given line. + * + * @param line + * The line to remove all formatting from + * @param attributes + * The attributes to remove + * @return The given line with all given formatting removed + */ + public String clean(String line, Set attributes) { + StringBuilder clean = new StringBuilder(line.length()); + + StringReader reader = new StringReader(line); + + try { + int inColorCode = 0; + while (true) { + int r = reader.read(); + if (r == -1) { + break; + } + char c = (char) r; + if ((c == 2) && (attributes.contains(Attributes.bold))) { + continue; + } + if ((c == 3) && (attributes.contains(Attributes.color))) { + inColorCode = 1; + continue; + } + if ((c == 15) && (attributes.contains(Attributes.clear))) { + continue; + } + if (inColorCode > 0) { + if (inColorCode == 1) { + if ((c < '0') || (c > '9')) { + inColorCode = 0; + } else { + if (c == '9') { + inColorCode = 8; + } else { + ++inColorCode; + } + continue; + } + } else if (inColorCode == 2) { + if (c == ',') { + inColorCode = 4; + continue; + } + if ((c < '0') || (c > '5')) { + inColorCode = 0; + } else { + ++inColorCode; + continue; + } + } else if (inColorCode == 3) { + if (c == ',') { + ++inColorCode; + continue; + } else { + inColorCode = 0; + } + } else if (inColorCode == 4) { + if (c == '9') { + inColorCode = 9; + continue; + } else if ((c < '0') || (c > '9')) { + inColorCode = 0; + } else { + ++inColorCode; + continue; + } + } else if (inColorCode == 5) { + inColorCode = 0; + if ((c >= '0') && (c <= '5')) { + continue; + } + } else if (inColorCode == 8) { + if (c == '9') { + inColorCode = 3; + continue; + } else if (c == ',') { + inColorCode = 4; + continue; + } else { + inColorCode = 0; + } + } else if (inColorCode == 9) { + inColorCode = 0; + if (c == '9') { + continue; + } + } + } + clean.append(c); + } + } catch (IOException ioe1) { + /* StringReader will never throw. */ + } + + return clean.toString(); + } + + // + // STATIC METHODS + // + + /** + * Returns the default instance. + * + * @return The default instance + */ + public static MessageCleaner getDefaultInstance() { + return DEFAULT_INSTANCE; + } + +} diff --git a/src/test/java/net/pterodactylus/irc/util/MessageCleanerTest.java b/src/test/java/net/pterodactylus/irc/util/MessageCleanerTest.java new file mode 100644 index 0000000..6ab7b0a --- /dev/null +++ b/src/test/java/net/pterodactylus/irc/util/MessageCleanerTest.java @@ -0,0 +1,94 @@ +/* + * XdccDownloader - MessageCleanerTest.java - Copyright © 2013 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.irc.util; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; + +import java.util.EnumSet; + +import net.pterodactylus.irc.util.MessageCleaner.Attributes; + +import org.testng.annotations.Test; + +/** + * Tests for {@link MessageCleaner}. + * + * @author David ‘Bombe’ Roden + */ +public class MessageCleanerTest { + + /** Tests removal of {@link Attributes#bold} formatting. */ + @Test + public void testRemoveBold() { + MessageCleaner messageCleander = MessageCleaner.getDefaultInstance(); + String clean; + + clean = messageCleander.clean("This contains \u0002bold\u0002 characters.", EnumSet.of(Attributes.bold)); + assertThat(clean, is("This contains bold characters.")); + + clean = messageCleander.clean("This contains \u0002bold\u0002 and \u00034color\u0003 characters.", EnumSet.of(Attributes.bold)); + assertThat(clean, is("This contains bold and \u00034color\u0003 characters.")); + } + + /** Tests removal of {@link Attributes#color} formatting. */ + @Test + public void testRemoveColors() { + MessageCleaner messageCleander = MessageCleaner.getDefaultInstance(); + String clean; + + clean = messageCleander.clean("This contains \u0002bold\u0002 characters.", EnumSet.of(Attributes.color)); + assertThat(clean, is("This contains \u0002bold\u0002 characters.")); + + clean = messageCleander.clean("This contains \u0002bold\u0002 and \u00034color\u0003 characters.", EnumSet.of(Attributes.color)); + assertThat(clean, is("This contains \u0002bold\u0002 and color characters.")); + + clean = messageCleander.clean("This contains \u00034,12colorful \u00039,18shit and \u000328stuff\u0003 and characters.", EnumSet.of(Attributes.color)); + assertThat(clean, is("This contains colorful 8shit and 8stuff and characters.")); + } + + /** Tests removal of {@link Attributes#clear} formatting. */ + @Test + public void testRemoveClear() { + MessageCleaner messageCleander = MessageCleaner.getDefaultInstance(); + String clean; + + clean = messageCleander.clean("This contains \u0002bold\u0002 and \u00034color\u0003 characters.", EnumSet.of(Attributes.clear)); + assertThat(clean, is("This contains \u0002bold\u0002 and \u00034color\u0003 characters.")); + + clean = messageCleander.clean("This contains \u0002bold\u0002 and \u00034color\u0003 and \u000fclear characters.", EnumSet.of(Attributes.clear)); + assertThat(clean, is("This contains \u0002bold\u0002 and \u00034color\u0003 and clear characters.")); + } + + /** Tests removal of all formatting. */ + @Test + public void testRemoveAll() { + MessageCleaner messageCleander = MessageCleaner.getDefaultInstance(); + String clean; + + clean = messageCleander.clean("This contains \u0002bold\u0002 and \u00034color\u0003 characters."); + assertThat(clean, is("This contains bold and color characters.")); + + clean = messageCleander.clean("This contains \u00034,12colorful \u00039,18shit and \u000328stuff\u0003 and characters."); + assertThat(clean, is("This contains colorful 8shit and 8stuff and characters.")); + + clean = messageCleander.clean("This contains \u0002bold\u0002 and \u00034color\u0003 and \u000fclear characters."); + assertThat(clean, is("This contains bold and color and clear characters.")); + } + +} -- 2.7.4