Java HTML Escape htmlEscape(String nonHTMLsrc)

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

Description

This method will take the input and escape characters that have an HTML entity representation.

License

Apache License

Parameter

Parameter Description
nonHTMLsrc String containing the text to make HTML-safe

Return

String containing new copy of string with ENTITIES escaped

Declaration

public static final String htmlEscape(String nonHTMLsrc) 

Method Source Code

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

public class Main {
    private static String entityMap;
    private static String[] quickEntities;

    /**/*from w  w  w. j  ava2s  . c om*/
      * This method will take the input and escape characters that have
      * an HTML entity representation.
      * It uses a quick string -> array mapping to avoid creating thousands of
      * temporary objects.
      * @param nonHTMLsrc String containing the text to make HTML-safe
      * @return String containing new copy of string with ENTITIES escaped
      */
    public static final String htmlEscape(String nonHTMLsrc) {
        if (nonHTMLsrc == null)
            return null;

        StringBuffer res = new StringBuffer();
        int l = nonHTMLsrc.length();
        int idx;
        char c;
        for (int i = 0; i < l; i++) {
            c = nonHTMLsrc.charAt(i);
            idx = entityMap.indexOf(c);
            if (idx == -1) {
                res.append(c);
            } else {
                res.append(quickEntities[idx]);
            }
        }
        return res.toString();
    }
}

Related

  1. htmlescape(String html)
  2. htmlEscape(String input)
  3. htmlEscape(String input)
  4. htmlEscape(String input)
  5. htmlEscape(String input)
  6. htmlEscape(String S)
  7. htmlEscape(String s)
  8. htmlEscape(String s)
  9. htmlescape(String s)