package com.oqpo.api.util;

public class HtmlCharConverter {

	public static String toText(String str) {
		if (str == null)
			return null;

		String returnStr = str;
		returnStr = returnStr.replaceAll("<br>", "\n");
		returnStr = returnStr.replaceAll("&gt;", ">");
		returnStr = returnStr.replaceAll("&lt;", "<");
		returnStr = returnStr.replaceAll("&quot;", "\"");
		returnStr = returnStr.replaceAll("&nbsp;", " ");
		returnStr = returnStr.replaceAll("&amp;", "&");
		returnStr = returnStr.replaceAll("\"", "&#34;");
		// returnStr = returnStr.replaceAll("&#34;", "\"");

		return returnStr;
	}

	public static String toSpclStr(String srcString) {
		String rtnStr = null;
		try {
			StringBuffer strTxt = new StringBuffer("");
			char chrBuff;
			int len = srcString.length();

			for (int i = 0; i < len; i++) {
				chrBuff = (char) srcString.charAt(i);

				switch (chrBuff) {
				case '<':
					strTxt.append("&lt;");
					break;
				case '>':
					strTxt.append("&gt;");
					break;
				case '&':
					strTxt.append("&amp;");
					break;
				case '\r':
					strTxt.append("");
					break;
				case '\n':
					strTxt.append("<br>");
					break;
				default:
					strTxt.append(chrBuff);
				}
			}
			rtnStr = strTxt.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rtnStr;
	}

	public static void main(String[] args) {
		System.out.println(HtmlCharConverter.toSpclStr("<br>"));

	}

}
