Re: [Simple-support] Multiple custom boolean formats using @Convert
Brought to you by:
niallg
|
From: Niall G. <gal...@ya...> - 2011-07-02 14:30:02
|
Hi,
Why not transform with
if(string.equalsIgnoreCase("true") || string.equalsIgnoreCase("Y")) {return true;}if(string.equalsIgnoreCase("false") || string.equalsIgnoreCase("N")) { return false;}throw exception;
If this does not work, they why not use a Visitor or a custom strategy.
Niall
--- On Tue, 28/6/11, Owen Mathews <ow...@bi...> wrote:
From: Owen Mathews <ow...@bi...>
Subject: [Simple-support] Multiple custom boolean formats using @Convert
To: sim...@li...
Received: Tuesday, 28 June, 2011, 6:05 AM
Hi all,
I'm dealing with some inconsistent XML that I must parse. See below:
<example tfBool="true" ynBool="Y" />
You can see that the tfBool attribute will be parsed properly, but the ynBool attribute needs special conversion. Here's my attempted solution:
@Rootpublic class Example {
private static String testXML = "<example tfBool=\"true\" ynBool=\"Y\" />";
@Attribute
public boolean tfBool;
@Attribute @Convert(Example.YesNoBooleanConverter.class)
public boolean ynBool;
public static class YesNoBooleanConverter implements Converter<Boolean> {
public Boolean read(InputNode node) throws Exception {
return new Boolean(node.getValue().equals("Y"));
}
public void write(OutputNode node, Boolean value) throws Exception {
node.setValue(value.booleanValue()?"Y":"N");
}
}
public static void main(String[] args) throws Exception {
Serializer s = new Persister(new AnnotationStrategy());
Example e = s.read(Example.class, new StringReader(testXML));
System.out.println("tfBool = " + e.tfBool);
System.out.println("ynBool = " + e.ynBool);
}
}
This code does not work. The output is:
tfBool = true
ynBool = false
As far as I can tell, YesNoBooleanConverter is never used. I'm assuming that it's because the boolean primitive type already matches to an existing Transformer implementation, so there's no way to use @Convert to use a custom converter if it's already mapped by the PrimitiveMatcher.
I can't use my own custom Transform/Match setup in this case, because the implementation is inconsistent (and as far as I can tell, this also eliminates the RegistryStrategy as an option as well). If all boolean values were represented by "Y" and "N", then I could set up a Matcher for boolean and put together a Transform that converts between strings properly. I need to convert standard boolean string representations for the first attribute and custom boolean string representations for the second.
Is there any way to do this easily (without having to have a custom converter for the whole class, for example)?
Owen
-----Inline Attachment Follows-----
------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2d-c2
-----Inline Attachment Follows-----
_______________________________________________
Simple-support mailing list
Sim...@li...
https://lists.sourceforge.net/lists/listinfo/simple-support
|