Here you can find the source of htmlize(CharSequence q)
CharSequence
in HTML.
Parameter | Description |
---|---|
q | a character sequence |
public static String htmlize(CharSequence q)
//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("&"); break; case '>': dest.append(">"); break; case '<': dest.append("<"); break; case '\n': dest.append("<br/>"); break; default: dest.append(c); } } }