Java HTML Encode htmlSpecialChars(String handleStr)

Here you can find the source of htmlSpecialChars(String handleStr)

Description

htmlspecialchars, Change HTML special char in String

License

Apache License

Parameter

Parameter Description
handleStr a parameter

Return

String

Declaration

public static String htmlSpecialChars(String handleStr) 

Method Source Code

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

public class Main {
    public static boolean blnTextBox = true;

    /**//ww w  .  j a v a2  s .  co  m
     * htmlspecialchars, Change HTML special char in String
     *
     * @param handleStr
     * @return String
     */
    public static String htmlSpecialChars(String handleStr) {
        return htmlSpecialChars(handleStr, true);
    }

    public static String htmlSpecialChars(String handleStr, boolean seq) {
        String str = handleStr;

        if (seq) {
            str = replace(str, "&", "&");
            str = replace(str, "\"", """);
            str = replace(str, "<", "&lt;");
            str = replace(str, ">", "&gt;");
        } else {
            str = replace(str, "&amp;", "&");
            str = replace(str, "&quot;", "\"");
            str = replace(str, "&lt;", "<");
            str = replace(str, "&gt;", ">");
        }

        if (!blnTextBox)
            if (seq)
                str = replace(str, "\n", "<br>");
            else
                str = replace(str, "<br>", "\n");

        return str;
    }

    /**
     * replace, replace a string with another string in a string
     *
     * @param handleStr
     * @param pointStr
     * @param repStr
     * @return string,
     */
    public static String replace(String handleStr, String pointStr, String repStr) {
        String str = new String();
        int pos1, pos2;
        try {
            if (handleStr.length() > 0) {
                pos1 = handleStr.indexOf(pointStr);
                pos2 = 0;
                while (pos1 != -1) {
                    str += handleStr.substring(pos2, pos1);
                    str += repStr;
                    pos2 = pos1 + pointStr.length();
                    pos1 = handleStr.indexOf(pointStr, pos2);
                }
                str += handleStr.substring(pos2);
            }
        } catch (Exception error) {
            error.printStackTrace();
        }
        return str;
    }
}

Related

  1. htmlEntityEncode(String s)
  2. htmlize(CharSequence q)
  3. htmlize(String str)
  4. htmlize(String text)
  5. htmlize(StringBuffer sb)
  6. htmlSpecialChars(String html)
  7. htmlspecialchars(String input)
  8. htmlspecialchars(String s)
  9. htmlspecialchars(String str)