package com.wxpn.tutorial.ec.util;

public class Encoder {

	/**
	 * 处理Html标记，将"\n"转换为"<br>"，将"\r"转换为"<br>"，
	 * 将" "转换为"&nbsp;"
	 * @param 处理前的字符串
	 * @return 处理后的字符串
	 */
	public static String returnToBr(String sStr) {
		if (sStr == null || sStr.equals("")) {
			return sStr;
		}

		StringBuffer sTmp = new StringBuffer();
		int i = 0;

		while (i <= sStr.length() - 1) {
			if (sStr.charAt(i) == '\n' || sStr.charAt(i) == '\r') {
				sTmp = sTmp.append("<br>");
			} else if (sStr.charAt(i) == ' ') {
				sTmp = sTmp.append("&nbsp;");
			} else {
				sTmp = sTmp.append(sStr.substring(i, i + 1));
			}

			i++;
		}
		String S1;
		S1 = sTmp.toString();
		return S1;
	}

	/**
	 * 处理Html标记，将"<"转换为"&lt;"，将">"转换为"&gt;"
	 * 
	 * @param 处理前的字符串
	 * @return 处理后的字符串
	 */
	public static String returnToHTML(String sStr) {
		if (sStr == null || sStr.equals("")) {
			return sStr;
		}

		StringBuffer sTmp1 = new StringBuffer();
		int i = 0;

		while (i <= sStr.length() - 1) {

			if (sStr.charAt(i) == '<') {
				sTmp1 = sTmp1.append("&lt;");
			} else if (sStr.charAt(i) == '>') {
				sTmp1 = sTmp1.append("&gt;");
			} else {
				sTmp1 = sTmp1.append(sStr.substring(i, i + 1));
			}

			i++;
		}
		String S2;
		S2 = sTmp1.toString();
		return S2;
	}

}
