Java HTML Encode HtmlEncode(String strInput)

Here you can find the source of HtmlEncode(String strInput)

Description

Html Encode

License

Open Source License

Declaration

public static String HtmlEncode(String strInput) 

Method Source Code

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

public class Main {

    public static String HtmlEncode(String strInput) {
        String input = strInput;/*from  w ww  .  jav a  2s.  c o m*/
        input = nullToEmpty(input);
        if ("".equals(input)) {
            return " ";
        } else {
            input = Replace(input, "\"", """);
            input = Replace(input, "<", "&lt;");
            input = Replace(input, ">", "&gt;");
            input = Replace(input, " ", "&nbsp;");
            input = Replace(input, "\r\n", "<br>");
            input = Replace(input, "\r", "<br>");
            input = Replace(input, "\n", "<br>");

            return input;
        }
    }

    public static String nullToEmpty(String str) {
        return (nullToEmpty(str, ""));
    }

    public static String nullToEmpty(String str, String defaultStr) {
        if (str == null || str == "")
            return defaultStr;
        else
            return str;
    }

    public static String nullToEmpty(Object obj) {
        if (obj == null)
            return "";
        else
            return obj.toString();
    }

    public static boolean equals(String str1, String str2) {
        if (str1 == null) {
            return str2 == null;
        }

        return str1.equals(str2);
    }

    public static String Replace(String str, String substr, String substitute) {
        str = nullToEmpty(str);
        String retstr = str;
        substr = nullToEmpty(substr);
        substitute = nullToEmpty(substitute);
        int substrlen = substr.length();
        for (int pos = retstr.indexOf(substr); pos != -1;) {
            String temp = retstr.substring(0, pos);
            temp = nullToEmpty(temp);
            temp = String.valueOf(temp) + String.valueOf(substitute);
            pos += substrlen;
            String half = retstr.substring(pos);
            half = nullToEmpty(half);
            if ("".equals(half)) {
                retstr = temp;
                break;
            }
            if (half.indexOf(substr) == -1)
                pos = -1;
            else
                pos = half.indexOf(substr) + temp.length();
            retstr = String.valueOf(temp) + String.valueOf(half);
        }

        return retstr;
    }
}

Related

  1. htmlEncode(String str)
  2. htmlEncode(String str)
  3. htmlEncode(String string)
  4. htmlEncode(String string)
  5. HTMLEncode(String string)
  6. htmlEncode(String strSrc)
  7. htmlEncode(String strSrc)
  8. htmlEncode(String text)
  9. htmlEncode(String txt)