Java HTML Encode htmlize(CharSequence q)

Here you can find the source of htmlize(CharSequence q)

Description

Return a string which represents a CharSequence in HTML.

License

Open Source License

Parameter

Parameter Description
q a character sequence

Return

a string representing the character sequence in HTML

Declaration

public static String htmlize(CharSequence q) 

Method Source Code

//package com.java2s;
/*/*from  w  ww.  j  av  a  2  s .  c o m*/
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * See LICENSE.txt included in this distribution for the specific
 * language governing permissions and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at LICENSE.txt.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

public class Main {
    /**
     * Return a string which represents a <code>CharSequence</code> in HTML.
     *
     * @param q a character sequence
     * @return a string representing the character sequence in HTML
     */
    public static String htmlize(CharSequence q) {
        StringBuilder sb = new StringBuilder(q.length() * 2);
        htmlize(q, sb);
        return sb.toString();
    }

    /**
     * Append a character sequence to the given destination whereby
     * special characters for HTML are escaped accordingly.
     *
     * @param q    a character sequence to esacpe
     * @param dest where to append the character sequence to
     */
    public static void htmlize(CharSequence q, StringBuilder dest) {
        for (int i = 0; i < q.length(); i++) {
            htmlize(q.charAt(i), dest);
        }
    }

    /**
     * Append a character array to the given destination whereby
     * special characters for HTML are escaped accordingly.
     *
     * @param cs     characters to esacpe
     * @param length max. number of characters to append, starting from index 0.
     * @param dest   where to append the character sequence to
     */
    public static void htmlize(char[] cs, int length, StringBuilder dest) {
        int len = length;
        if (cs.length < length) {
            len = cs.length;
        }
        for (int i = 0; i < len; i++) {
            htmlize(cs[i], dest);
        }
    }

    /**
     * Append a character to the given destination whereby special characters
     * special for HTML are escaped accordingly.
     *
     * @param c    the character to append
     * @param dest where to append the character to
     */
    private static void htmlize(char c, StringBuilder dest) {
        switch (c) {
        case '&':
            dest.append("&amp;");
            break;
        case '>':
            dest.append("&gt;");
            break;
        case '<':
            dest.append("&lt;");
            break;
        case '\n':
            dest.append("<br/>");
            break;
        default:
            dest.append(c);
        }
    }
}

Related

  1. htmlEncode(String txt)
  2. htmlEncoded(String text)
  3. htmlEncoder(String content)
  4. htmlEncoding(String str)
  5. htmlEntityEncode(String s)
  6. htmlize(String str)
  7. htmlize(String text)
  8. htmlize(StringBuffer sb)
  9. htmlSpecialChars(String handleStr)