You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Simon Kitching <sk...@apache.org> on 2006/01/03 21:36:50 UTC

Re: [Digester] How make digester call setStringArray(String[]) from single element node content?

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


Re: [Digester] How make digester call setStringArray(String[]) from single element node content?

Posted by Alessio Pace <al...@gmail.com>.
I tried using the Converter way, but with no success. Here is what I
did in practise:

My bean has some setters, one of them is a setStringArray(String[] values).

To associate a converter to be used when having to call a setter whose
actual parameter type is a String[] I do:

Class stringArrayClass = new String[] {}.getClass();
ConvertUtils.register(new StringArrayConverter(), stringArrayClass);
Digester digester = new Digester();

// to call setters
digester.addBeanPropertySetter("root/someother/stuff/mybean/?");



Then my converter looks like:

public final class StringArrayConverter implements Converter {
        private Object defaultValue = new String[] {};
        private boolean useDefault = true;

        public FrameSequenceConverter() {
            log.info("-------------->Constructed FrameSequence converter");
            this.defaultValue = null;
            this.useDefault = false;
        }

        public Object convert(Class type, Object value) throws
ConversionException {
            log.info("----------> Invoked with value: " + value);

          // ......
       }
}

Well, the log inside the constructor is printed, while instead the log
inside the convert method is never printed (and of course my unit test
never passes :)

What's wrong with what I did? Is it because Array are a special beast?


Thanks in advance,
Alessio Pace.


On 1/4/06, Alessio Pace <al...@gmail.com> wrote:
> Thanks for the answer, I'll dig into the details myself now.
>
>  bye
>  Alessio Pace.
>
>
>
>
> On 1/3/06, Simon Kitching <sk...@apache.org> wrote:
> >  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
> >
>



--
Alessio Pace.

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


Re: [Digester] How make digester call setStringArray(String[]) from single element node content?

Posted by Alessio Pace <al...@gmail.com>.
Thanks for the answer, I'll dig into the details myself now.

bye
Alessio Pace.


On 1/3/06, Simon Kitching <sk...@apache.org> wrote:
>
> 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
>