Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java castor using custom field handlers

I have been trying to write a custom field handler which returns the hash code of a java.awt.Image object when writing to XML and retrieves an image based on this hash code when binding the XML to an object. For some reason, I can't get this to work; castor, from what I can tell, simply instantiates the field handler and then doesn't call any of its methods.

Can you give me a quick example of how to do this because I must be missing something simple!

Cheers,

Pete


1 Answers

Pasting your code can be a good idea. Anyway following works fine for me

<m:class name="someClass">
        <m:map-to xml="class"/>
        <m:field name="lineColor" type="java.awt.Color"  handler="ColorFieldHandler">
            <m:bind-xml name="lineColor" node="attribute"/>
        </m:field>
    </m:class>

And the handler itself

public class ColorFieldHandler extends GeneralizedFieldHandler {
    public Object convertUponGet(Object value) {
        if (value == null) {
            return null;
        }
        Integer colorHash = (Integer) value;
    ...

Hope that helps

like image 116
SirVaulterScoff Avatar answered May 21 '26 14:05

SirVaulterScoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!