public static String htmlEntityEncode(String s) {
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if ((c >= 'a' && c <= 'z') ||
                    (c >= 'A' && c <= 'Z') ||
                    (c >= '0' && c <= '9')) {
                buf.append(c);
            } else {
                buf.append("&#" + (int)c + ";");
            }
        }
        return buf.toString();
    }