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