Java HTML HTMLEnc(String s)

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

Description

HTML encoding (does not convert line breaks).

License

Open Source License

Declaration

public static String HTMLEnc(String s) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from   w ww .  j av a  2 s  .  co m
     *  HTML encoding (does not convert line breaks).
     *  Replaces all '>' '<' '&' and '"' with entity reference
     */
    public static String HTMLEnc(String s) {
        return XMLEncNA(s);
    }

    /**
     *  XML encoding without replacing apostrophes.
     *  @see #XMLEnc(String)
     */
    public static String XMLEncNA(String s) {
        int ln = s.length();
        for (int i = 0; i < ln; i++) {
            char c = s.charAt(i);
            if (c == '<' || c == '>' || c == '&' || c == '"') {
                StringBuffer b = new StringBuffer(s.substring(0, i));
                switch (c) {
                case '<':
                    b.append("&lt;");
                    break;
                case '>':
                    b.append("&gt;");
                    break;
                case '&':
                    b.append("&amp;");
                    break;
                case '"':
                    b.append("&quot;");
                    break;
                }
                i++;
                int next = i;
                while (i < ln) {
                    c = s.charAt(i);
                    if (c == '<' || c == '>' || c == '&' || c == '"') {
                        b.append(s.substring(next, i));
                        switch (c) {
                        case '<':
                            b.append("&lt;");
                            break;
                        case '>':
                            b.append("&gt;");
                            break;
                        case '&':
                            b.append("&amp;");
                            break;
                        case '"':
                            b.append("&quot;");
                            break;
                        }
                        next = i + 1;
                    }
                    i++;
                }
                if (next < ln)
                    b.append(s.substring(next));
                s = b.toString();
                break;
            } // if c ==
        } // for
        return s;
    }
}

Related

  1. htmlCode(char tok)
  2. htmlcodeToSpecialchars(String str)
  3. htmlcodeToSpecialchars(String str)
  4. htmlContentHeaderGen(String providerNo, String output, String errorMsg)
  5. htmlConvert(String htmlStr)
  6. htmlEnclose(String html)
  7. htmlFilenameForClass(Class c)
  8. HTMLFilter(String the_txt)
  9. htmlFilter(String value)