You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Lutz Ulrich <Ul...@optimal-systems.de> on 2004/05/19 15:50:04 UTC

[digester] How to convert the type of a XML-value before passing it as argument to bean

hello,

the XML i want to parse with digester contains long values converted to
hexadecimal format, like

<value>34FA5</value>

since the text represents a long value, the corresponding bean
shall have a corresponding long property, not a string property:

class MyBean
{
  long value;

  public long getValue() { return value; }
  public void setValue(long value) { this.value = value; }
}

how do you convert the long value given as a hexadezimal string
to long without letting the conversion be part of the bean logic?

of course, i know the conversion routine java.lang.Long.parseLong(String
s, int radix).
but how do i configure my digester to call this method before passing
the
resulting value to the bean?

regards,
lutz

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


Re: [digester] How to convert the type of a XML-value before passing it as argument to bean

Posted by Adrian Perez Jorge <ap...@ignos.com>.
Lutz Ulrich wrote:

>hello,
>
>the XML i want to parse with digester contains long values converted to
>hexadecimal format, like
>
><value>34FA5</value>
>
>since the text represents a long value, the corresponding bean
>shall have a corresponding long property, not a string property:
>
>class MyBean
>{
>  long value;
>
>  public long getValue() { return value; }
>  public void setValue(long value) { this.value = value; }
>}
>
>how do you convert the long value given as a hexadezimal string
>to long without letting the conversion be part of the bean logic?
>
>of course, i know the conversion routine java.lang.Long.parseLong(String
>s, int radix).
>but how do i configure my digester to call this method before passing
>the
>resulting value to the bean?
>
>  
>
Digester seems to use BeanUtils (SetPropertiesRule use 
BeanUtil.populate()) to set values to bean properties.  So BeanUtils 
does the conversion from String to basic types according to 
*org.apache.commons.beanutils.ConvertUtils*.  But you want to do a 
custom conversion (from hex string to long).

To implement such custom conversion you can't distinguish an hex string 
from any generic String.  So what about creating a new instance of a 
custom class HexString whenever you find a <value>hexstring 
here</value>, and implement a conversion from HexString to long, so 
BeanUtils automagically does the proper stuff?

The inconvenience is that this doesn't work for SetPropertiesRule 
because attribute values are created as String's and you don't have any 
chance use the custom class.

The other way around is to implement a custom rule (and/or if you use 
element attributes for properties implement an ObjectCreateFactory) wich 
does the conversion.

Any other ideas?

Cheers,

Adrian P.J.

>regards,
>lutz
>


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