Android HTML Encode htmEncode(String input)

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

Description

htm Encode

Declaration

public static String htmEncode(String input) 

Method Source Code

//package com.java2s;

public class Main {
    private static final int VALUE_10 = 10;
    private static final int VALUE_13 = 13;
    private static final int VALUE_32 = 32;
    private static final int VALUE_60 = 60;
    private static final int VALUE_62 = 62;
    private static final int VALUE_38 = 38;
    private static final int VALUE_34 = 34;
    private static final int VALUE_165 = 165;
    private static final int VALUE_174 = 174;
    private static final int VALUE_8482 = 8482;
    private static final int VALUE_8364 = 8364;
    private static final int VALUE_169 = 169;

    public static String htmEncode(String input) {
        if (null == input || "".equals(input)) {
            return input;
        }//from  w  w w  .j a  v a 2s .  c  o m

        StringBuffer stringbuffer = new StringBuffer();
        int j = input.length();
        for (int i = 0; i < j; i++) {
            char c = input.charAt(i);
            switch (c) {
            case VALUE_60:
                stringbuffer.append("&lt;");
                break;
            case VALUE_62:
                stringbuffer.append("&gt;");
                break;
            case VALUE_38:
                stringbuffer.append("&amp;");
                break;
            case VALUE_34:
                stringbuffer.append("&quot;");
                break;
            case VALUE_169:
                stringbuffer.append("&copy;");
                break;
            case VALUE_174:
                stringbuffer.append("&reg;");
                break;
            case VALUE_165:
                stringbuffer.append("&yen;");
                break;
            case VALUE_8364:
                stringbuffer.append("&euro;");
                break;
            case VALUE_8482:
                stringbuffer.append("&#153;");
                break;
            case VALUE_13:
                if (i < j - 1 && input.charAt(i + 1) == VALUE_10) {
                    stringbuffer.append("<br>");
                    i++;
                }
                break;
            case VALUE_32:
                if (i < j - 1 && input.charAt(i + 1) == ' ') {
                    stringbuffer.append(" &nbsp;");
                    i++;
                    break;
                }
            default:
                stringbuffer.append(c);
                break;
            }
        }
        return new String(stringbuffer.toString());
    }
}

Related

  1. htmlencode(String str)
  2. replaceHTMLTags(final String source)
  3. encodeHtml(String to_encode)
  4. fromHTML(String textToTransform)