package tvark.jsf;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;



public class StringConverter implements Converter {

	private static final Pattern sPattern = Pattern.compile("(.*)&#(\\d+);(.*)");
	
	public Object getAsObject(FacesContext pCtx, UIComponent pComp, String pValue) {
		Matcher m = sPattern.matcher(pValue);
		while (m.matches()) {
			long code = Long.parseLong(m.group(2));
			String subs = new String(new char[] {(char) code});
			pValue = m.group(1) + subs + m.group(3);
			m = sPattern.matcher(pValue);
		}
		return pValue;
	}

	public String getAsString(FacesContext pCtx, UIComponent pComp,
			Object pValue) {
		return (String) pValue;
	}
	
	public static void main(String[] pArgs) {
		String s = "1a_pavard&#279;_1 Vardas_1";
		new StringConverter().getAsObject(null, null, s);
		
	}

}
