You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by petra staub <ca...@hotmail.com> on 2004/07/20 10:43:36 UTC

Digester: dynamic property setter

Hi

I think this is a trivial question but as I am quite a newbie with
digster, I dare to pose it... ;-)

How can I implement a dynamic property setter?

I have for instance a bean 'address' with different properties and
getter/setters...something like:

public class Address {
  private String firstname;
  private String lastname;
  ...
  ...

  public void setFirstname(String str) {
    firstname = str;
  }
  public String getFirstname() {
    return firstname;
  }
  ...
  ...
}

I want now to parse a XML file where the bean properties are
specified by a 'property tag':

<address>
  <property id="firstname">petra</property>
  <property id="lastname">staub</property>
</address>

What rule do I have to apply for this?
I want to dynamically call the setter method on the bean what
is specified by the attribute 'id'...

I am able to solve this with the existing rules, if I know what
setter I actually want to call. A workaround would be to add
a 'addProperty(key,value)' method to the bean and implement
so a 'DynaBean'...but maybe this can be done with Digester
without this???

Thanks for any help or suggestions!

_________________________________________________________________
Sie wollen die letzten Nachrichten oder Ihr eigenes Horoskop per SMS 
erhalten? http://www.msn.ch/mobile/


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


Re: Digester: dynamic property setter

Posted by Simon Kitching <si...@ecnetwork.co.nz>.
On Tue, 2004-07-20 at 20:43, petra staub wrote:

> How can I implement a dynamic property setter?
> 
> I have for instance a bean 'address' with different properties and
> getter/setters...something like:
> 
> public class Address {
>   private String firstname;
>   private String lastname;
>   ...
>   ...
> 
>   public void setFirstname(String str) {
>     firstname = str;
>   }
>   public String getFirstname() {
>     return firstname;
>   }
>   ...
>   ...
> }
> 
> I want now to parse a XML file where the bean properties are
> specified by a 'property tag':
> 
> <address>
>   <property id="firstname">petra</property>
>   <property id="lastname">staub</property>
> </address>
> 
Hi Petra,

If you can write your xml as:
  <address>
    <property id="firstname" value="petra"/>
    <property id="lastname" value="staub"/>
  </address>
then you can use the SetPropertyRule.

If you can write your xml as:
  <address>
    <firstname>petra</firstname>
    <lastname>staub</lastname>
  </address>
then you can either use BeanPropertySetterRule + the ExtendedBaseRules
matcher, or can use the new SetNestedPropertiesRule from the CVS version
of Digester.

But unfortunately I don't know of a way of parsing the xml you indicate
using the standard rules.

You could of course write a custom Rule to do this (or modify the
existing SetPropertyRule to add this feature). If you do so, please
consider submitting it for inclusion in the Digester core. I'm not
currently sure what the best way of doing this is, because it requires
info from both the begin() method (attribute) and body() method
(content), and rules should never store "state" on themselves. Perhaps
an argument could be made that this is ok for this rule, because it
always deals with "leaf" xml elements.

Or, as you say, you could add a setProperty(name, value) method to your
bean that either has a hard-wired if/then structure to call the correct
setter method, or uses reflection upon itself to invoke the appropriate
method, or make the class a DynaBean.

Regards,

Simon


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