Java HTML Encode toHtmlValue(String s)

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

Description

Description of the Method

License

Open Source License

Parameter

Parameter Description
s Description of Parameter

Return

Description of the Returned Value

Declaration

public static String toHtmlValue(String s) 

Method Source Code

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

public class Main {
    /**/*from w w  w. ja  v  a 2s.co  m*/
     * Description of the Method
     *
     * @param s Description of Parameter
     * @return Description of the Returned Value
     */
    public static String toHtmlValue(String s) {
        if (s != null) {
            String htmlReady = s.trim();
            htmlReady = replace(htmlReady, "&", "&");
            htmlReady = replace(htmlReady, "\"", """);
            htmlReady = replace(htmlReady, "<", "&lt;");
            htmlReady = replace(htmlReady, ">", "&gt;");
            htmlReady = replace(htmlReady, "\r\n", "<br>");
            htmlReady = replace(htmlReady, "\n\r", "<br>");
            htmlReady = replace(htmlReady, "\n", "<br>");
            htmlReady = replace(htmlReady, "\r", "<br>");
            htmlReady = replace(htmlReady, "//lt;", "<");
            htmlReady = replace(htmlReady, "//gt;", ">");

            //a second call to the function (done in surveys)
            //changes <br> to &lt;br&gt;, this needs to be reverted
            htmlReady = replace(htmlReady, "&lt;br&gt;", "<br>");

            htmlReady = toHtmlChars(htmlReady);
            return (htmlReady);
        } else {
            return ("");
        }
    }

    /**
     * Description of the Method
     *
     * @param str Description of Parameter
     * @param o   Description of Parameter
     * @param n   Description of Parameter
     * @return Description of the Returned Value
     */
    public static String replace(String str, String o, String n) {
        boolean all = true;
        if (str != null && o != null && o.length() > 0 && n != null) {
            StringBuffer result = null;
            int oldpos = 0;
            do {
                int pos = str.indexOf(o, oldpos);
                if (pos < 0) {
                    break;
                }
                if (result == null) {
                    result = new StringBuffer();
                }
                result.append(str.substring(oldpos, pos));
                result.append(n);
                pos += o.length();
                oldpos = pos;
            } while (all);
            if (oldpos == 0) {
                return str;
            } else {
                result.append(str.substring(oldpos));
                return result.toString();
            }
        } else {
            return str;
        }
    }

    /**
     * Description of the Method
     *
     * @param htmlReady Description of the Parameter
     * @return Description of the Return Value
     */
    public static String toHtmlChars(String htmlReady) {
        if (htmlReady != null) {
            htmlReady = replace(htmlReady, "\u00A1", "&iexcl;");
            htmlReady = replace(htmlReady, "\u00A2", "&cent;");
            htmlReady = replace(htmlReady, "\u00A3", "&pound;");
            htmlReady = replace(htmlReady, "\u00A4", "&curren;");
            htmlReady = replace(htmlReady, "\u00A5", "&yen;");
            htmlReady = replace(htmlReady, "\u00A6", "&brvbar;");
            htmlReady = replace(htmlReady, "\u00A7", "&sect;");
            htmlReady = replace(htmlReady, "\u00A8", "&uml;");
            htmlReady = replace(htmlReady, "\u00A9", "&copy;");
            htmlReady = replace(htmlReady, "\u00AA", "&ordf;");
            htmlReady = replace(htmlReady, "\u00AB", "&laquo;");
            htmlReady = replace(htmlReady, "\u00AC", "&not;");
            htmlReady = replace(htmlReady, "\u00AD", "&shy;");
            htmlReady = replace(htmlReady, "\u00AE", "&reg;");
            htmlReady = replace(htmlReady, "\u00AF", "&macr;");
            htmlReady = replace(htmlReady, "\u00B0", "&deg;");
            htmlReady = replace(htmlReady, "\u00B1", "&plusmn;");
            htmlReady = replace(htmlReady, "\u00B2", "&sup2;");
            htmlReady = replace(htmlReady, "\u00B3", "&sup3;");
            htmlReady = replace(htmlReady, "\u00B4", "&acute;");
            htmlReady = replace(htmlReady, "\u00B5", "&micro;");
            htmlReady = replace(htmlReady, "\u00B6", "&para;");
            htmlReady = replace(htmlReady, "\u00B7", "&middot;");
            htmlReady = replace(htmlReady, "\u00B8", "&cedil;");
            htmlReady = replace(htmlReady, "\u00B9", "&sup1;");
            htmlReady = replace(htmlReady, "\u00BA", "&ordm;");
            htmlReady = replace(htmlReady, "\u00BB", "&raquo;");
            htmlReady = replace(htmlReady, "\u00BC", "&frac14;");
            htmlReady = replace(htmlReady, "\u00BD", "&frac12;");
            htmlReady = replace(htmlReady, "\u00BE", "&frac34;");
            htmlReady = replace(htmlReady, "\u00BF", "&iquest;");
            htmlReady = replace(htmlReady, "\u00C0", "&Agrave;");
            htmlReady = replace(htmlReady, "\u00C1", "&Aacute;");
            htmlReady = replace(htmlReady, "\u00C2", "&Acirc;");
            htmlReady = replace(htmlReady, "\u00C3", "&Atilde;");
            htmlReady = replace(htmlReady, "\u00C4", "&Auml;");
            htmlReady = replace(htmlReady, "\u00C5", "&Aring;");
            htmlReady = replace(htmlReady, "\u00C6", "&AElig;");
            htmlReady = replace(htmlReady, "\u00C7", "&Ccedil;");
            htmlReady = replace(htmlReady, "\u00C8", "&Egrave;");
            htmlReady = replace(htmlReady, "\u00C9", "&Eacute;");
            htmlReady = replace(htmlReady, "\u00CA", "&Ecirc;");
            htmlReady = replace(htmlReady, "\u00CB", "&Euml;");
            htmlReady = replace(htmlReady, "\u00CC", "&Igrave;");
            htmlReady = replace(htmlReady, "\u00CD", "&Iacute;");
            htmlReady = replace(htmlReady, "\u00CE", "&Icirc;");
            htmlReady = replace(htmlReady, "\u00CF", "&Iuml;");
            htmlReady = replace(htmlReady, "\u00D0", "&ETH;");
            htmlReady = replace(htmlReady, "\u00D1", "&Ntilde;");
            htmlReady = replace(htmlReady, "\u00D2", "&Ograve;");
            htmlReady = replace(htmlReady, "\u00D3", "&Oacute;");
            htmlReady = replace(htmlReady, "\u00D4", "&Ocirc;");
            htmlReady = replace(htmlReady, "\u00D5", "&Otilde;");
            htmlReady = replace(htmlReady, "\u00D6", "&Ouml;");
            htmlReady = replace(htmlReady, "\u00D7", "&times;");
            htmlReady = replace(htmlReady, "\u00D8", "&Oslash;");
            htmlReady = replace(htmlReady, "\u00D9", "&Ugrave;");
            htmlReady = replace(htmlReady, "\u00DA", "&Uacute;");
            htmlReady = replace(htmlReady, "\u00DB", "&Ucirc;");
            htmlReady = replace(htmlReady, "\u00DC", "&Uuml;");
            htmlReady = replace(htmlReady, "\u00DD", "&Yacute;");
            htmlReady = replace(htmlReady, "\u00DE", "&THORN;");
            htmlReady = replace(htmlReady, "\u00DF", "&szlig;");
            htmlReady = replace(htmlReady, "\u00E0", "&agrave;");
            htmlReady = replace(htmlReady, "\u00E1", "&aacute;");
            htmlReady = replace(htmlReady, "\u00E2", "&acirc;");
            htmlReady = replace(htmlReady, "\u00E3", "&atilde;");
            htmlReady = replace(htmlReady, "\u00E4", "&auml;");
            htmlReady = replace(htmlReady, "\u00E5", "&aring;");
            htmlReady = replace(htmlReady, "\u00E6", "&aelig;");
            htmlReady = replace(htmlReady, "\u00E7", "&ccedil;");
            htmlReady = replace(htmlReady, "\u00E8", "&egrave;");
            htmlReady = replace(htmlReady, "\u00E9", "&eacute;");
            htmlReady = replace(htmlReady, "\u00EA", "&ecirc;");
            htmlReady = replace(htmlReady, "\u00EB", "&euml;");
            htmlReady = replace(htmlReady, "\u00EC", "&igrave;");
            htmlReady = replace(htmlReady, "\u00ED", "&iacute;");
            htmlReady = replace(htmlReady, "\u00EE", "&icirc;");
            htmlReady = replace(htmlReady, "\u00EF", "&iuml;");
            htmlReady = replace(htmlReady, "\u00F0", "&eth;");
            htmlReady = replace(htmlReady, "\u00F1", "&ntilde;");
            htmlReady = replace(htmlReady, "\u00F2", "&ograve;");
            htmlReady = replace(htmlReady, "\u00F3", "&oacute;");
            htmlReady = replace(htmlReady, "\u00F4", "&ocirc;");
            htmlReady = replace(htmlReady, "\u00F5", "&otilde;");
            htmlReady = replace(htmlReady, "\u00F6", "&ouml;");
            htmlReady = replace(htmlReady, "\u00F7", "&divide;");
            htmlReady = replace(htmlReady, "\u00F8", "&oslash;");
            htmlReady = replace(htmlReady, "\u00F9", "&ugrave;");
            htmlReady = replace(htmlReady, "\u00FA", "&uacute;");
            htmlReady = replace(htmlReady, "\u00FB", "&ucirc;");
            htmlReady = replace(htmlReady, "\u00FC", "&uuml;");
            htmlReady = replace(htmlReady, "\u00FD", "&yacute;");
            htmlReady = replace(htmlReady, "\u00FE", "&thorn;");
            htmlReady = replace(htmlReady, "\u00FF", "&yuml;");
            htmlReady = replace(htmlReady, "\u20AC", "&euro;");
            return (htmlReady);
        } else {
            return ("");
        }
    }

    /**
     * Description of the Method
     *
     * @param s Description of Parameter
     * @return Description of the Returned Value
     */
    public static String toString(String s) {
        if (s != null) {
            return (s);
        } else {
            return ("");
        }
    }
}

Related

  1. toHTMLString(String str)
  2. toHtmlText(String s)
  3. toHtmlText(String text)
  4. toHtmlTitle(final String title)
  5. toHtmlUrl(String url, String displayLabel)
  6. toHtmlVerticalString(final String source)