package com.brilliance.isc.utils;

public class HtmlEscapeUtils {

    public static String escapeHtml(String input) {
        //不适用,可用commons-text包的StringEscapeUtils.escapeHtml4
        if (input == null || input.isEmpty()) {
            return "";
        }
        StringBuilder escaped = new StringBuilder();
        for (char c : input.toCharArray()) {
            switch (c) {
                case '<':
                    escaped.append("&lt;");
                    break;
                case '>':
                    escaped.append("&gt;");
                    break;
                case '&':
                    escaped.append("&amp;");
                    break;
                case '"':
                    escaped.append("&quot;");
                    break;
                case '\'':
                    escaped.append("&#39;");
                    break;
                case ' ':
                    escaped.append("&nbsp;");
                    break;
                default:
                    escaped.append(c);
                    break;
            }
        }
        return escaped.toString();
    }
}
