Java HTML Escape escapeHtml(String s)

Here you can find the source of escapeHtml(String s)

Description

escape Html

License

Apache License

Declaration

public static String escapeHtml(String s) 

Method Source Code

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

import java.util.Arrays;
import java.util.HashMap;

import java.util.Map;

public class Main {
    private static final Map<Character, String> ESCAPES = new HashMap<Character, String>() {
        private static final long serialVersionUID = 1285607660247157523L;

        {/*from   ww w  .ja va 2  s  .co m*/
            put('<', "&lt;");
            put('>', "&gt;");
            put('\'', "&apos;");
            put('"', "&quot;");
            put('&', "&amp;");
        }
    };

    public static String escapeHtml(String s) {
        if (s == null) {
            return null;
        }

        StringBuilder result = new StringBuilder();

        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            String nc = ESCAPES.get(c);
            if (nc != null) {
                result.append(nc);
            } else {
                result.append(c);
            }
        }

        return result.toString();
    }

    public static String toString(Object object, Class<?> objectClass) {
        if (null == object) {
            return "null";
        }
        final String toString = object.toString();
        if (isStringEmpty(toString)) {
            return "\"\"";
        } else if (String.class.equals(objectClass)) {
            return "\"" + toString + '\"';
        } else {
            return toString;
        }
    }

    /**
     * Returns the string representation of the specified object, transparently
     * handling null references and arrays.
     * 
     * @param obj
     *            the object
     * @return the string representation
     */
    public static String toString(Object obj) {
        String result;
        if (obj != null) {
            if (obj instanceof boolean[]) {
                result = Arrays.toString((boolean[]) obj);
            } else if (obj instanceof byte[]) {
                result = Arrays.toString((byte[]) obj);
            } else if (obj instanceof char[]) {
                result = Arrays.toString((char[]) obj);
            } else if (obj instanceof double[]) {
                result = Arrays.toString((double[]) obj);
            } else if (obj instanceof float[]) {
                result = Arrays.toString((float[]) obj);
            } else if (obj instanceof int[]) {
                result = Arrays.toString((int[]) obj);
            } else if (obj instanceof long[]) {
                result = Arrays.toString((long[]) obj);
            } else if (obj instanceof Object[]) {
                result = Arrays.deepToString((Object[]) obj);
            } else if (obj instanceof short[]) {
                result = Arrays.toString((short[]) obj);
            } else {
                result = obj.toString();
            }
        } else {
            result = "null";
        }
        return result;
    }

    public static boolean isStringEmpty(String s) {
        return s == null || "".equals(s);
    }
}

Related

  1. escapeHTML(String aText)
  2. escapeHTML(String s)
  3. escapeHTML(String s)
  4. escapeHtml(String s)
  5. escapeHTML(String text)
  6. escapeHTML(String text)