⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 encoder.java

📁 java 调用windows的api
💻 JAVA
字号:
package org.jawin.browser.xml;

/**
 * Handles fast XML encoding of Strings.  Nulls are treated as empty Strings
 * so that null handling can be performed centrally.
 *
 * <p>Title: Jawin Code Generation GUI</p>
 * <p>Description: GUI for exploring type libraries and generating Java code</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: Open Source Incentive</p>
 *
 * @author Josh Passenger
 * @version 1.0
 */

public class Encoder
{
	public static final String AMPERSAND = "&amp;";
	public static final String GREATER_THAN = "&gt;";
	public static final String LESS_THAN = "&lt;";
	public static final String QUOTE = "&quot;";

	public static final char CHAR_AMPERSAND = '&';
	public static final char CHAR_GREATER_THAN = '>';
	public static final char CHAR_LESS_THAN = '<';
	public static final char CHAR_QUOTE = '"';

	/**
	 * Encode the requested String creating only one new StringBuffer and
	 * the return String,  Use the StringBuffer method if performance is critical,
	 * and when isn't it?
	 *
	 * @param data the data to encode
	 * @return the encoded String
	 */
	public static String encode(String data)
	{
		if (data == null)
		{
			return "";
		}

		StringBuffer buffer = new StringBuffer(data.length());
		encode(data, buffer);
		return buffer.toString();
	}

	/**
	 * Encode the String into the target buffer creating no objects
	 * in the process
	 *
	 * @param data the data to encode
	 * @param buffer encode the string into teh requested
	 * StringBuffer
	 */
	public static void encode(String data, StringBuffer buffer)
	{
		if (data == null)
		{
			return;
		}

		char [] dataChars = data.toCharArray();

		for (int i = 0; i < dataChars.length; i++)
		{
			if (needsEncoding(dataChars[i]))
			{
				buffer.append(encode(dataChars[i]));
			}
			else
			{
				buffer.append(dataChars[i]);
			}
		}
	}

	/**
	 * Encodes the requested char
	 *
	 * @param c the char to encode
	 * @return the encoded version of the String
	 */
	private static String encode(char c)
	{
		String encoded = null;

		switch (c)
		{
			case CHAR_AMPERSAND:
			{
				encoded = AMPERSAND;
				break;
			}
			case CHAR_GREATER_THAN:
			{
				encoded = GREATER_THAN;
				break;
			}
			case CHAR_LESS_THAN:
			{
				encoded = LESS_THAN;
				break;
			}
			case CHAR_QUOTE:
			{
				encoded = QUOTE;
				break;
			}
		}

		return encoded;
	}

	/**
	 * Find out if the specified char needs encoding
	 *
	 * @param c the char to check for encoding
	 * @return true if the char needs encoding, false if it does not
	 */
	public static boolean needsEncoding(char c)
	{
		boolean needsEncoding = false;

		switch (c)
		{
			case CHAR_AMPERSAND:
			{
				needsEncoding = true;
				break;
			}
			case CHAR_GREATER_THAN:
			{
				needsEncoding = true;
				break;
			}
			case CHAR_LESS_THAN:
			{
				needsEncoding = true;
				break;
			}
			case CHAR_QUOTE:
			{
				needsEncoding = true;
				break;
			}
		}

		return needsEncoding;
	}
}

⌨️ 快捷键说明

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