/* $Id: UnicodeDecode.java 885 2014-09-17 15:19:26Z shayashi $ */
package com.hexidec.ekit;

public class UnicodeDecode {
	public static void main(String args[]){
		Character c = new Character((char)28165);
//		System.out.println(c.charValue());
		String str = "a &#28165;&#27700;aa";
//		System.out.println(UnicodeDecode.decode(str));
	}
	
	public static String decode(String str_){
		if(str_ == null){
			return str_;
		}
		StringBuffer rtn = new StringBuffer();
		String str = str_;
		while(str.indexOf("&#") != -1){
			int start = str.indexOf("&#");
			int end   = -1;
			StringBuffer unicodeBuf = new StringBuffer();
			for(int i=start+2;i<str.length();i++){
				if(str.charAt(i) == ';'){
					end = i;
					break;
				}else{
					try{
						Integer.parseInt(String.valueOf(str.charAt(i)));
						unicodeBuf.append(str.charAt(i));
					}catch(Exception e){
						break;
					}
				}
			}
			if(end != -1){
				String temp1 = str.substring(0,start);
				String temp2 = str.substring(end + 1,str.length());
				int unicode = Integer.parseInt(unicodeBuf.toString());
				Character c = new Character((char)unicode);
				String replaceStr = String.valueOf(c.charValue());
				rtn.append(temp1);
				rtn.append(replaceStr);
				str = temp2;
			}
		}
		rtn.append(str);
		return rtn.toString();
	}
	
}
