Java HTML Escape htmlEscape(String source)

Here you can find the source of htmlEscape(String source)

Description

Turns funky characters into HTML entity equivalents.

E.g.

License

Apache License

Declaration

public static String htmlEscape(String source) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.Map;
import java.util.HashMap;

public class Main {
    private static Map i2e = new HashMap();

    /**//from  w ww  .  ja  va  2  s  .c  om
     * Turns funky characters into HTML entity equivalents.<p>
     * E.g. <tt>"bread" & "butter"</tt> => <tt>&amp;quot;bread&amp;quot; &amp;amp; &amp;quot;butter&amp;quot;</tt>
     * <p>Update: supports nearly all HTML entities, including funky accents. See the source code for more detail.
     **/
    public static String htmlEscape(String source) {
        StringBuffer buf = new StringBuffer();
        if (source != null) {
            for (int i = 0; i < source.length(); ++i) {
                char ch = source.charAt(i);
                String entity = (String) i2e.get(new Integer((int) ch));
                if (entity == null) {
                    if (((int) ch) > 128)
                        buf.append("&#" + ((int) ch) + ";");
                    else
                        buf.append(ch);
                } else {
                    buf.append("&" + entity + ";");
                }
            }
        }
        return buf.toString();
    }
}

Related

  1. htmlEscape(String nonHTMLsrc)
  2. htmlEscape(String S)
  3. htmlEscape(String s)
  4. htmlEscape(String s)
  5. htmlescape(String s)
  6. htmlEscape(String str)
  7. htmlEscape(String str)
  8. htmlEscape(String str)
  9. htmlEscape(String string)