commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Simon Kitching <skitch...@apache.org>
Subject Re: [Digester] How make digester call setStringArray(String[]) from single element node content?
Date Tue, 03 Jan 2006 20:36:50 GMT
On Mon, 2006-01-02 at 18:27 +0100, Alessio Pace wrote:
> Hi,
> 
> imagine I have a class with some simple set methods (setField(String) ,
> setOtherField(int) )  and then a more "complicated" setter method like this:
> 
> public void setStringArray(String[] stringArray){
>     this.stringArray = stringArray;
> }
> 
> 
> Now imagine I would like to read my class from XML using digester, and the
> format is like:
> 
> <myclass>
>     <field>string</field>
>     <otherField>100</otherField>
>    <stringArray>A, B, C</stringArray>
> </myclass>
> 
> Is there any default syntactic sugar that makes me accomplish this task
> without having to write a custom converter or rule (sorry if I don't use the
> correct Digester terms, I am almost a newbie) to make digester call
> setStringArray( {"A", "B", "C"} )  ??
> 
> Using:
>    digester.addBeanPropertySetter("root/someother/stuff/class/?");
> 
> I have succesfully solved the issue about setField(String)  and
> setOtherField(int), but I am not able to get the array setter to be properly
> called.
> 
> If the design of the approach is inherently ugly, suggestions are welcome
> for an alternative XML format (and relative Digester rule) to store an and
> retrieve an array of strings :)

You could certainly solve this with a custom Rule class. Don't be
concerned about this, it's perfectly normal and not particularly
difficult. Digbester comes with a useful set of prebuilt rules, but that
doesn't mean they can solve every problem. In the "body" method of a
custom Rule, just split the body string into an array, then retrieve the
top object on the digester stack, cast it to a MyClass instance and
invoke the setStringArray method passing your data.

You might also be able to solve this by registering a custom
String->StringArray converter with the beanutils library. Digester
always uses the ConvertUtils class within that library to convert the
strings it gets from the xml file into the appropriate type for the
parameter(s) of the method it is invoking.

Regards,

Simon



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message