Java castor using custom field handlers

I am trying to write a special field handler that returns the hash code of the java.awt.Image object when writing to XML and extracts an image based on this hash code when binding the XML to the object. For some reason I can't get this to work; the castor, from what I can say, simply creates an instance of the field handler, and then does not call any of its methods.

Can you give me a quick example of how to do this, because I have to miss something simple!

Greetings

Pete

+3
source share
1 answer

Inserting code may be a good idea. Anyway, the following works great 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>

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

,

+1

Source: https://habr.com/ru/post/1704398/


All Articles