commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Adrian Perez Jorge <>
Subject Re: [digester] How to convert the type of a XML-value before passing it as argument to bean
Date Wed, 19 May 2004 15:35:05 GMT
Lutz Ulrich wrote:

>the XML i want to parse with digester contains long values converted to
>hexadecimal format, like
>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
>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?


Adrian P.J.


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message