filter For HTML Value - Java java.lang

Java examples for java.lang:String HTML

Description

filter For HTML Value

Demo Code

//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String _sContent = "java2s.com";
        System.out.println(filterForHTMLValue(_sContent));
    }/*from   w  ww .  ja v  a2 s .com*/

    private static final char[] DIGITS = { '0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

    public static String filterForHTMLValue(String _sContent) {
        if (_sContent == null)
            return "";

        // _sContent = replaceStr(_sContent,"</script>","<//script>");
        // _sContent = replaceStr(_sContent,"</SCRIPT>","<//SCRIPT>");

        char[] srcBuff = _sContent.toCharArray();
        int nLen = srcBuff.length;
        if (nLen == 0)
            return "";

        StringBuffer retBuff = new StringBuffer((int) (nLen * 1.8));

        for (int i = 0; i < nLen; i++) {
            char cTemp = srcBuff[i];
            switch (cTemp) {
            case '&': // ???& -->&amp;why: 2002-3-19
                // caohui@0515
                // ????unicode???
                if ((i + 1) < nLen) {
                    cTemp = srcBuff[i + 1];
                    if (cTemp == '#')
                        retBuff.append("&");
                    else
                        retBuff.append("&amp;");
                } else
                    retBuff.append("&amp;");
                break;
            case '<': // ???< --> &lt;
                retBuff.append("&lt;");
                break;
            case '>': // ???> --> &gt;
                retBuff.append("&gt;");
                break;
            case '\"': // ???" --> &quot;
                retBuff.append("&quot;");
                break;
            default:
                retBuff.append(cTemp);
            }// case
        }// end for

        return retBuff.toString();
    }

    public static String toString(byte[] data) {
        if (data == null) {
            return "null!";
        }
        int l = data.length;

        char[] out = new char[l << 1];

        // two characters form the hex value.
        for (int i = 0, j = 0; i < l; i++) {
            out[j++] = DIGITS[(0xF0 & data[i]) >>> 4];
            out[j++] = DIGITS[0x0F & data[i]];
        }

        return new String(out);
    }

    public static String toString(Object[] objs) {
        return toString(objs, false, ", ");
    }

    public static String toString(Object[] objs, boolean showOrder) {
        return toString(objs, showOrder, ",");
    }

    public static String toString(Object[] objs, boolean showOrder,
            String token) {
        if (objs == null) {
            return "null";
        }
        int len = objs.length;
        StringBuffer sb = new StringBuffer(10 * len);
        for (int i = 0; i < len; i++) {
            if (showOrder) {
                sb.append(i).append(':');
            }
            sb.append(objs[i]);
            if (i < len - 1) {
                sb.append(token);
            }
        }
        return sb.toString();
    }
}

Related Tutorials