public static String htmlescape(String s1) {
        if (s1 == null) return "";

        int len = s1.length();
        // about 20% guess
        StringBuilder buf = new StringBuilder((int) (len * 1.2));
        int i;

        for (i = 0; i < len; ++i) {
            char ch = s1.charAt(i);
            String entity = i2e.get((int) ch);

            if (entity == null) {
                if (((int) ch) > 128) buf.append("&#").append((int) ch).append(";");
                else buf.append(ch);
            } else {
                buf.append("&").append(entity).append(";");
            }
        }
        return buf.toString();
    }
Static HTML escaping stuff

@author Bjorn Borud
vespajlib/src/main/java/com/yahoo/text/HTML.java