Java HTML Escape htmlEscape(String input)

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

Description

html Escape

License

Open Source License

Declaration

public static String htmlEscape(String input) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Iterator;
import java.util.Collection;

import java.util.HashMap;

public class Main {
    private static HashMap<Character, String> replacements = new HashMap<Character, String>();

    public static String htmlEscape(String input) {
        StringBuilder sb = new StringBuilder(input.length());
        for (char c : input.toCharArray()) {
            if (replacements.containsKey(c)) {
                sb.append(replacements.get(c));
            } else {
                sb.append(c);/*from w w w.j a  v  a2s .c  o m*/
            }
        }

        return sb.toString();

        //        return input.replace("&", "&amp;").replace(">", "&gt;").replace("<", "&lt;")
        //            .replace("'", "&apos;").replaceAll("\"", "&quot;");
    }

    /**
     * Provides a string representation of the elements in the given collection.
     *
     * @param c            A collection of elements.  It may be null.
     * @param prefix    String to be used as prefix.
     * @param sep        String to be used to separate elements in the collection.
     * @param suffix    String to be used as suffix.
     * @param <T>        The type of elements in the collection.
     *
     * @return String representation of the elements in the collection.
     */
    public static <T> String toString(Collection<T> c, String prefix, String sep, String suffix) {
        if (c == null || c.size() == 0)
            return prefix + suffix;
        Iterator<T> it = c.iterator();
        String result = prefix + it.next();
        while (it.hasNext())
            result += sep + it.next();
        return result + suffix;
    }

    public static <T> String toString(final Collection<T> a) {
        return toString(a, "", ",", "");
    }

    /**
     * Provides a string representation of the elements in the given array.
     *
     * @param <T>    The type of array elements.
     * @param array  An array of elements.  It may be null.
     * @param prefix String to be used as prefix.
     * @param sep    String to be used to separate array elements.
     * @param suffix String to be used as suffix.
     *
     * @return String representation of the elements in the array.
     */
    public static <T> String toString(T[] array, String prefix, String sep, String suffix) {
        if (array == null || array.length == 0)
            return prefix + suffix;
        String result = prefix + array[0];
        for (int i = 1; i < array.length; i++) {
            result += sep + array[i];
        }
        return result + suffix;
    }

    /**
     * Provides a string representation of the elements in the given array.
     *
     * @param <T>   The type of the array elements.
     * @param array An array of elements.
     *
     * @return string representation of elements in given array.
     */
    public static <T> String toString(final T[] array) {
        return toString(array, "", ",", "");
    }
}

Related

  1. htmlEscape(final String text)
  2. htmlEscape(Object obj)
  3. htmlEscape(String html)
  4. htmlescape(String html)
  5. htmlEscape(String input)
  6. htmlEscape(String input)
  7. htmlEscape(String input)
  8. htmlEscape(String nonHTMLsrc)
  9. htmlEscape(String S)