--- /dev/null
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+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> 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;
+ }
+
+}
--- /dev/null
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+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."));
+ }
+
+}