Ignore leading zeros when filtering color codes
[xudocci.git] / src / main / java / net / pterodactylus / irc / util / MessageCleaner.java
1 /*
2  * XdccDownloader - MessageCleaner.java - Copyright © 2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.irc.util;
19
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;
23
24 import java.util.EnumSet;
25 import java.util.Set;
26
27 /**
28  * Removes formatting codes from IRC messages.
29  *
30  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
31  */
32 public class MessageCleaner {
33
34         /** The kind of attributes to clear. */
35         public enum Attributes {
36
37                 bold,
38                 color,
39                 clear,
40                 reverse,
41                 underline,
42                 italics
43
44         }
45
46         /** A default instance. */
47         private static final MessageCleaner DEFAULT_INSTANCE = new MessageCleaner();
48
49         /**
50          * Cleans all formatting from the given line.
51          *
52          * @param line
53          *              The line to remove all formatting from
54          * @return The given line with all formatting removed
55          */
56         public String clean(String line) {
57                 return clean(line, EnumSet.allOf(Attributes.class));
58         }
59
60         /**
61          * Cleans the given formattings from the given line.
62          *
63          * @param line
64          *              The line to remove all formatting from
65          * @param attributes
66          *              The attributes to remove
67          * @return The given line with all given formatting removed
68          */
69         public String clean(String line, Set<Attributes> attributes) {
70                 StringBuilder clean = new StringBuilder(line.length());
71
72                 int inColorCode = 0;
73                 for (char c : line.toCharArray()) {
74                         if ((c == 2) && (attributes.contains(Attributes.bold))) {
75                                 continue;
76                         }
77                         if ((c == 3) && (attributes.contains(Attributes.color))) {
78                                 inColorCode = 1;
79                                 continue;
80                         }
81                         if ((c == 15) && (attributes.contains(Attributes.clear))) {
82                                 continue;
83                         }
84                         if ((c == 22) && attributes.contains(reverse)) {
85                                 continue;
86                         }
87                         if ((c == 29) && attributes.contains(italics)) {
88                                 continue;
89                         }
90                         if ((c == 31) && attributes.contains(underline)) {
91                                 continue;
92                         }
93                         if (inColorCode > 0) {
94                                 if (inColorCode == 1) {
95                                         if ((c < '0') || (c > '9')) {
96                                                 inColorCode = 0;
97                                         } else if (c == '0') {
98                                                 continue;
99                                         } else {
100                                                 if (c == '9') {
101                                                         inColorCode = 8;
102                                                 } else {
103                                                         ++inColorCode;
104                                                 }
105                                                 continue;
106                                         }
107                                 } else if (inColorCode == 2) {
108                                         if (c == ',') {
109                                                 inColorCode = 4;
110                                                 continue;
111                                         }
112                                         if ((c < '0') || (c > '5')) {
113                                                 inColorCode = 0;
114                                         } else {
115                                                 ++inColorCode;
116                                                 continue;
117                                         }
118                                 } else if (inColorCode == 3) {
119                                         if (c == ',') {
120                                                 ++inColorCode;
121                                                 continue;
122                                         } else {
123                                                 inColorCode = 0;
124                                         }
125                                 } else if (inColorCode == 4) {
126                                         if (c == '9') {
127                                                 inColorCode = 9;
128                                                 continue;
129                                         } else if ((c < '0') || (c > '9')) {
130                                                 inColorCode = 0;
131                                         } else {
132                                                 ++inColorCode;
133                                                 continue;
134                                         }
135                                 } else if (inColorCode == 5) {
136                                         inColorCode = 0;
137                                         if ((c >= '0') && (c <= '5')) {
138                                                 continue;
139                                         }
140                                 } else if (inColorCode == 8) {
141                                         if (c == '9') {
142                                                 inColorCode = 3;
143                                                 continue;
144                                         } else if (c == ',') {
145                                                 inColorCode = 4;
146                                                 continue;
147                                         } else {
148                                                 inColorCode = 0;
149                                         }
150                                 } else {
151                                         inColorCode = 0;
152                                         if (c == '9') {
153                                                 continue;
154                                         }
155                                 }
156                         }
157                         clean.append(c);
158                 }
159
160                 return clean.toString();
161         }
162
163         //
164         // STATIC METHODS
165         //
166
167         /**
168          * Returns the default instance.
169          *
170          * @return The default instance
171          */
172         public static MessageCleaner getDefaultInstance() {
173                 return DEFAULT_INSTANCE;
174         }
175
176 }