package com.eactive.eai.common.util;

import java.util.HashMap;

public class XSSUtil {
	public static final HashMap<Integer, String> m = new HashMap<>();
	static {
		m.put(34, "&quot;");
		m.put(60, "&lt;");
		m.put(62, "&gt;");
		m.put(38, "&amp;");
		m.put(162, "&cent;");
		m.put(163, "&pound;");
		m.put(165, "&yen;");
		m.put(8364, "&euro;");
		m.put(167, "&sect;");
		m.put(169, "&copy;");
		m.put(174, "&reg;");
		m.put(8482, "&trade;");
	}

	public static String escape(String str) {
		StringBuilder sb = new StringBuilder();
		int charSize = str.length();

		for (int i = 0; i < charSize; i++) {
			char c = str.charAt(i);
			int ascCode = (int) c;

			String replaceCode = (String) m.get(ascCode);
			if (replaceCode == null) {
				if (c > 0x7F) {
					sb.append("&#");
					sb.append(Integer.toString(c, 10));
					sb.append(";");
				} else {
					sb.append(c);
				}
			} else {
				sb.append(replaceCode);
			}
		}
		return sb.toString();
	}
}
