Here you can find the source of htmlEncode(final String source, StringBuffer target)
Parameter | Description |
---|---|
source | a parameter |
target | a parameter |
public static void htmlEncode(final String source, StringBuffer target)
//package com.java2s; /*//from ww w .j a va2 s.co m * Utility.java * * HomePage : http://www.codeproject.com/csharp/TraceTool.asp * Download : http://sourceforge.net/projects/tracetool/ * See License.txt for license information * * Author : Thierry Parent * Version : 12.3 * * Provide some utility functions */ public class Main { /** * Html encode, with StringBuffer as target (faster) * @param source * @param target */ public static void htmlEncode(final String source, StringBuffer target) { if (source == null || source.length() == 0) return; int length = source.length(); char[] charArray = new char[length]; source.getChars(0, length, charArray, 0); int startIndex = 0; int currentIndex = 0; while (currentIndex < length) { char ch = charArray[currentIndex]; int oneInt = ch; switch (ch) { case '<': if (startIndex < currentIndex) target.append(charArray, startIndex, currentIndex - startIndex); target.append("<"); //$NON-NLS-1$ startIndex = currentIndex + 1; break; case '>': if (startIndex < currentIndex) target.append(charArray, startIndex, currentIndex - startIndex); target.append(">"); //$NON-NLS-1$ startIndex = currentIndex + 1; break; case '"': if (startIndex < currentIndex) target.append(charArray, startIndex, currentIndex - startIndex); target.append("""); //$NON-NLS-1$ startIndex = currentIndex + 1; break; case '&': if (startIndex < currentIndex) target.append(charArray, startIndex, currentIndex - startIndex); target.append("&"); //$NON-NLS-1$ startIndex = currentIndex + 1; break; default: if (oneInt > 65533) // char > 65533 cannot be converted to &#nnn; Use a formal text ?nnnn target.append("?").append(intToString(ch)); //$NON-NLS-1$ else if ((ch < ' ') || ((oneInt >= 160))) // 0 to 31 and 160 to 256 // && (ch < 'A') { if (startIndex < currentIndex) target.append(charArray, startIndex, currentIndex - startIndex); target.append("&#"); //$NON-NLS-1$ target.append(intToString(ch)); target.append(';'); startIndex = currentIndex + 1; } // else no encoding break; } // switch currentIndex++; } target.append(charArray, startIndex, length - startIndex); } /** * convert an integer to a string. * @param param An integer to convert * @return a string representation * @see #intToString(int, int) for formatted string */ public static String intToString(final int param) { return Integer.toString(param); } /** * convert an integer to a string (left padding). * @param param An integer to convert * @param len The string length * @return a string representation */ public static String intToString(final int param, final int len) { return intToStringBuffer(param, len).toString(); } /** * convert an integer to a StringBuffer (left padding). * @param param An integer to convert * @param len The string length * @return a string representation */ public static StringBuffer intToStringBuffer(final int param, final int len) { StringBuffer temp = new StringBuffer(Integer.toString(param)); leftPadding(temp, len, ' '); return temp; } /** * Left Pad Stringbuffer with special char * @param strBuf Target buffer * @param bufLen Buffer length * @param fill Char to fill * @return a padded StringBuffer */ public static StringBuffer leftPadding(final StringBuffer strBuf, final int bufLen, final char fill) { while (strBuf.length() < bufLen) strBuf.insert(0, fill); return strBuf; } }