htmlencoder.java

来自「openfans 0.1 using: eclipse, maven, s」· Java 代码 · 共 75 行

JAVA
75
字号
package org.openfans.util;

/**
 * 
 * 类说明:对字符串进行HTML编码,可以在页面上显示正确的字符串值<br>
 * 例如: & 会被编码为 &amp; 创建时间:2006-2-22<br>
 * 
 * @author 李涛<br>
 * @email:oofrank@163.com<br>
 *                        Copyright 1999,2004 The Apache Software Foundation.
 */
public class HtmlEncoder {

	private static final String[] htmlCode = new String[256];

	static {
		for (int i = 0; i < 10; i++) {
			htmlCode[i] = "&#00" + i + ";";
		}

		for (int i = 10; i < 32; i++) {
			htmlCode[i] = "&#0" + i + ";";
		}

		for (int i = 32; i < 128; i++) {
			htmlCode[i] = String.valueOf((char) i);
		}

		// Special characters
		htmlCode[' '] = "&nbsp;";
		htmlCode['\n'] = "<BR>\n";
		htmlCode['\"'] = "&quot;"; // double quote
		htmlCode['&'] = "&amp;"; // ampersand
		htmlCode['<'] = "&lt;"; // lower than
		htmlCode['>'] = "&gt;"; // greater than

		for (int i = 128; i < 256; i++) {
			htmlCode[i] = "&#" + i + ";";
		}
	}

	/**
	 * <p>
	 * Encode the given text into html.
	 * </p>
	 * 
	 * @param string
	 *            the text to encode
	 * @return the encoded string
	 * 
	 */
	public static String encode(String string) {
		int n = string.length();
		char character;
		StringBuffer buffer = new StringBuffer();
		// loop over all the characters of the String.
		for (int i = 0; i < n; i++) {
			character = string.charAt(i);
			// the Htmlcode of these characters are added to a StringBuffer one
			// by one
			try {
				buffer.append(htmlCode[character]);
			} catch (ArrayIndexOutOfBoundsException aioobe) {
				buffer.append(character);
			}
		}
		return buffer.toString();
	}

	public static void main(String[] args) {
		System.out.println(HtmlEncoder.encode("<dd>&&"));
	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?