Re: [Simple-support] Boolean Attribute 0 or 1
Brought to you by:
niallg
|
From: jc.jourdon <jc....@7g...> - 2011-10-21 09:52:55
|
Hello back,
i implemented a Matcher as you proposed, but i need a Matcher that
Transform many different Type of data (Transform<Float>,
Transform<Integer> and Transform<Boolean>).
how can i do this with a single implementation of Matcher class ?
my Matcher , with a single transformer, so far :
public class LigneArticleFloatMatcher implements Matcher, Transform<Float> {
@Override
public Float read(String value) throws Exception {
if((value!="null")&&(value!=""))
{
try{
return Float.valueOf(value);
}catch(Exception e){
return 0f;
}
}else{
return 0f;
}
}
@Override
public String write(Float value) throws Exception {
return String.valueOf(value);
}
@SuppressWarnings("rawtypes")
@Override
public Transform match(Class type) throws Exception {
if(type == Float.class) {
return this;
}
return null;
}
}
Thanks in advance ^^
> hello,
>
> you've done a great job with this tool and i already parsed many
> different XML from soap servers since last 3 days, but i encountered
> boolean attributes with "0" or "1"
>
> <list mybool1="0" mybool2="1" attr1="attr" attr2="attr">
> <page mybool3="1">
> ...
> </page>
> <page mybool3="0">
> ...
> </page>
> ...
> </list>
>
> I tried to create this class :
>
> public class Boolean01Converter implements Converter<Boolean>
> {
> @Override
> public Boolean read(InputNode node) throws Exception {
> return new Boolean(node.getValue().equals("1"));
> }
> @Override
> public void write(OutputNode node, Boolean value) throws Exception {
> node.setValue(value.booleanValue()?"1":"0");
> }
> }
>
> and implemented it on my object definition :
>
> @Root(name="list")
> public class ListFcts
> {
> @Attribute
> @Convert(Boolean01Converter.class)
> private Boolean mybool1;
>
> @Attribute
> @Convert(Boolean01Converter.class)
> private Boolean mybool2;
>
> @Attribute
> private int ...
>
> @ElementList(name="page", inline=true)
> private List<Page> pages;
> }
>
>
> But i still get false for every boolean.
>
> Also : how can I attach the converter to the Persister instead of
> declaring it on @Attributes hundred times ?
>
> Many thanks in advance !!
|