summaryrefslogtreecommitdiffstats
path: root/EssentialsUpdate/src/f00f/net/irc/martyr/util/IRCStringUtils.java
blob: 3b4fa6075bd3c13a21b403c7eb47442bf6d47123 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package f00f.net.irc.martyr.util;

import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
import java.util.ArrayList;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

/**
 * @author Daniel Henninger
 */
public class IRCStringUtils
{

    /**
     * Returns the message with all control characters stripped from it.
     *
     * @param msg Message to remove control chars from.
     * @return Stripped form of message.
     */
    public static String stripControlChars(String msg)
    {
        Pattern pa = Pattern.compile("\u0003\\p{Digit}\\p{Digit}");
        Matcher ma = pa.matcher(msg);
        Pattern pb = Pattern.compile("\\p{Cntrl}");
        Matcher mb = pb.matcher(ma.replaceAll(""));
        return mb.replaceAll("");
    }

    /**
     * Returns the message with all formatting characters converted into associated html characters.
     *
     * TODO: Should actually parse colors.
     * @param msg Message to convert to HTML format.
     * @return Message in HTML format.
     */
    public static String convertToHTML(String msg)
    {
        CharacterIterator ci = new StringCharacterIterator(msg);
        String htmlStr = "";
        ArrayList<String> formatList = new ArrayList<String>();
        for (char c = ci.first(); c != CharacterIterator.DONE; c = ci.next()) {
            if (c == '\u0002') {
                if (formatList.contains("</b>")) {
                    formatList.remove("</b>");
                    htmlStr += "</b>";
                }
                else {
                    formatList.add("</b>");
                    htmlStr += "<b>";
                }
            }
            else if (c == '\u001F') {
                if (formatList.contains("</u>")) {
                    formatList.remove("</u>");
                    htmlStr += "</u>";
                }
                else {
                    formatList.add("</u>");
                    htmlStr += "<u>";
                }
            }
            else if (c == '\u0016') {
                if (formatList.contains("</i>")) {
                    formatList.remove("</i>");
                    htmlStr += "</i>";
                }
                else {
                    formatList.add("</i>");
                    htmlStr += "<i>";
                }
            }
            else if (c == '\u000F' || c == '\u0015') {
                for (String f : formatList) {
                    htmlStr += f;
                }
                formatList.clear();
            }
            else {
                htmlStr += c;
            }
        }
        for (String f : formatList) {
            htmlStr += f;
        }
        formatList.clear();
        return stripControlChars(htmlStr);
    }

}