2 * XdccDownloader - MessageCleaner.java - Copyright © 2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.irc.util;
20 import static net.pterodactylus.irc.util.MessageCleaner.Attributes.italics;
21 import static net.pterodactylus.irc.util.MessageCleaner.Attributes.reverse;
22 import static net.pterodactylus.irc.util.MessageCleaner.Attributes.underline;
24 import java.io.IOException;
25 import java.io.StringReader;
26 import java.util.EnumSet;
30 * Removes formatting codes from IRC messages.
32 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34 public class MessageCleaner {
36 /** The kind of attributes to clear. */
37 public enum Attributes {
48 /** A default instance. */
49 private static final MessageCleaner DEFAULT_INSTANCE = new MessageCleaner();
52 * Cleans all formatting from the given line.
55 * The line to remove all formatting from
56 * @return The given line with all formatting removed
58 public String clean(String line) {
59 return clean(line, EnumSet.allOf(Attributes.class));
63 * Cleans the given formattings from the given line.
66 * The line to remove all formatting from
68 * The attributes to remove
69 * @return The given line with all given formatting removed
71 public String clean(String line, Set<Attributes> attributes) {
72 StringBuilder clean = new StringBuilder(line.length());
74 StringReader reader = new StringReader(line);
79 int r = reader.read();
84 if ((c == 2) && (attributes.contains(Attributes.bold))) {
87 if ((c == 3) && (attributes.contains(Attributes.color))) {
91 if ((c == 15) && (attributes.contains(Attributes.clear))) {
94 if ((c == 22) && attributes.contains(reverse)) {
97 if ((c == 29) && attributes.contains(italics)) {
100 if ((c == 31) && attributes.contains(underline)) {
103 if (inColorCode > 0) {
104 if (inColorCode == 1) {
105 if ((c < '0') || (c > '9')) {
115 } else if (inColorCode == 2) {
120 if ((c < '0') || (c > '5')) {
126 } else if (inColorCode == 3) {
133 } else if (inColorCode == 4) {
137 } else if ((c < '0') || (c > '9')) {
143 } else if (inColorCode == 5) {
145 if ((c >= '0') && (c <= '5')) {
148 } else if (inColorCode == 8) {
152 } else if (c == ',') {
167 } catch (IOException ioe1) {
168 /* StringReader will never throw. */
171 return clean.toString();
179 * Returns the default instance.
181 * @return The default instance
183 public static MessageCleaner getDefaultInstance() {
184 return DEFAULT_INSTANCE;