I have found a Converter online and changed it to my needs as far as I could. The problem is though that I need to add a flag (i.e. a string) that must be checked and than the converter must apply a certain pattern to a string.
Custom Converter:
@FacesConverter("convtest.UrlConverter")
public class UrlConverter implements Converter {
@Override
public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
StringBuilder url = new StringBuilder();
if(value!=null){
if(value.length()==13){
String tempstring;
tempstring=value.toString();
String finalstring= tempstring.substring(0, 4) + "-" + tempstring.substring(4, 8) + "-" + tempstring.substring(8, 13);
url.append(finalstring);
}else{
url.append(value);
}
}else
url.append("");
try {
new URI(url.toString());
} catch (URISyntaxException e) {
return null;
}
UrlData urlData = new UrlData(url.toString());
return urlData;
}
@Override
public String getAsString(FacesContext facesContext,
UIComponent component, Object value) {
try {
return value.toString();
} catch (Exception e) {
return null;
}
}
}
XHTML:
<h:inputText value="#{userData.data}">
<f:converter converterId="convtest.UrlConverter" />
</h:inputText>
Now the problem is that for example I have 2 conversion types:
hju
zurt
Let's say that hju
have the output format XXXX-XXXX-XXXXX
and zurt
has the output format XX-XX-XX-XX-XX-XX-X
.
Now I would like to call the converter like for example:
<f:converter converterId="convtest.UrlConverter" type="hju" />
Or something like that and get it to use the correct pattern.
Any ideas on how to do this?