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.util.EnumSet;
28 * Removes formatting codes from IRC messages.
30 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
32 public class MessageCleaner {
34 /** The kind of attributes to clear. */
35 public enum Attributes {
46 /** A default instance. */
47 private static final MessageCleaner DEFAULT_INSTANCE = new MessageCleaner();
50 * Cleans all formatting from the given line.
53 * The line to remove all formatting from
54 * @return The given line with all formatting removed
56 public String clean(String line) {
57 return clean(line, EnumSet.allOf(Attributes.class));
61 * Cleans the given formattings from the given line.
64 * The line to remove all formatting from
66 * The attributes to remove
67 * @return The given line with all given formatting removed
69 public String clean(String line, Set<Attributes> attributes) {
70 StringBuilder clean = new StringBuilder(line.length());
73 for (char c : line.toCharArray()) {
74 if ((c == 2) && (attributes.contains(Attributes.bold))) {
77 if ((c == 3) && (attributes.contains(Attributes.color))) {
81 if ((c == 15) && (attributes.contains(Attributes.clear))) {
84 if ((c == 22) && attributes.contains(reverse)) {
87 if ((c == 29) && attributes.contains(italics)) {
90 if ((c == 31) && attributes.contains(underline)) {
93 if (inColorCode > 0) {
94 if (inColorCode == 1) {
95 if ((c < '0') || (c > '9')) {
105 } else if (inColorCode == 2) {
110 if ((c < '0') || (c > '5')) {
116 } else if (inColorCode == 3) {
123 } else if (inColorCode == 4) {
127 } else if ((c < '0') || (c > '9')) {
133 } else if (inColorCode == 5) {
135 if ((c >= '0') && (c <= '5')) {
138 } else if (inColorCode == 8) {
142 } else if (c == ',') {
158 return clean.toString();
166 * Returns the default instance.
168 * @return The default instance
170 public static MessageCleaner getDefaultInstance() {
171 return DEFAULT_INSTANCE;