package com.inspect.base.core.utils.html;

import com.inspect.base.core.utils.StringUtils;

public class EscapeUtil {
    public static final String RE_HTML_MARK = "(<[^<]*?>)|(<[\\s]*?/[^<]*?>)|(<[^<]*?/[\\s]*?>)";
    private static final char[][] TEXT = new char[64][];

    public static String escape(String text) {
        return encode(text);
    }

    public static String unescape(String content) {
        return decode(content);
    }

    public static String clean(String content) {
        return (new HTMLFilter()).filter(content);
    }

    private static String encode(String text) {
        if (StringUtils.isEmpty(text)) {
            return "";
        } else {
            StringBuilder tmp = new StringBuilder(text.length() * 6);

            for (int i = 0; i < text.length(); ++i) {
                char c = text.charAt(i);
                if (c < 256) {
                    tmp.append("%");
                    if (c < 16) {
                        tmp.append("0");
                    }

                    tmp.append(Integer.toString(c, 16));
                } else {
                    tmp.append("%u");
                    if (c <= 4095) {
                        tmp.append("0");
                    }

                    tmp.append(Integer.toString(c, 16));
                }
            }

            return tmp.toString();
        }
    }

    public static String decode(String content) {
        if (StringUtils.isEmpty(content)) {
            return content;
        } else {
            StringBuilder tmp = new StringBuilder(content.length());
            int lastPos = 0;

            while (lastPos < content.length()) {
                int pos = content.indexOf("%", lastPos);
                if (pos == lastPos) {
                    char ch;
                    if (content.charAt(pos + 1) == 117) {
                        ch = (char) Integer.parseInt(content.substring(pos + 2, pos + 6), 16);
                        tmp.append(ch);
                        lastPos = pos + 6;
                    } else {
                        ch = (char) Integer.parseInt(content.substring(pos + 1, pos + 3), 16);
                        tmp.append(ch);
                        lastPos = pos + 3;
                    }
                } else if (pos == -1) {
                    tmp.append(content.substring(lastPos));
                    lastPos = content.length();
                } else {
                    tmp.append(content.substring(lastPos, pos));
                    lastPos = pos;
                }
            }

            return tmp.toString();
        }
    }

    public static void main(String[] args) {
        String html = "<script>alert(1);</script>";
        String escape = escape(html);
        System.out.println("clean: " + clean(html));
        System.out.println("escape: " + escape);
        System.out.println("unescape: " + unescape(escape));
    }

    static {
        for (int i = 0; i < 64; ++i) {
            TEXT[i] = new char[]{(char) i};
        }

        TEXT[39] = "&#039;".toCharArray();
        TEXT[34] = "&#34;".toCharArray();
        TEXT[38] = "&#38;".toCharArray();
        TEXT[60] = "&#60;".toCharArray();
        TEXT[62] = "&#62;".toCharArray();
    }
}
