‘class defined’ callback methods in JAXB


/**
 * Invoked by Marshaller after it has created an instance of this object.
 */
private void beforeMarshal(final Marshaller marshaller) {
    // this method marked as returning a 'boolean' value,
    // but 'void' seems work.
}

/**
 * Invoked by Marshaller after it has marshalled all properties of this
 * object.
 */
private void afterMmarshal(final Marshaller marshaller) {
}

/**
 * This method is called immediately after the object is created and before
 * the unmarshalling of this object begins. The callback provides an
 * opportunity to initialize JavaBean properties prior to unmarshalling.
 */
private void beforeUnmarshal(final Unmarshaller unmarshaller,
                             final Object parent) {
}

/**
 * This method is called after all the properties (except IDREF) are
 * unmarshalled for this object, but before this object is set to the parent
 * object.
 */
private void afterUnmarshal(final Unmarshaller unmarshaller,
                            final Object parent) {
}

As of writing this entry(java version “1.7.0_03”), becoreMarshal and afterMarshal seem being called twice each with any external listeners set.

import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Callback {

    public static void main(final String[] args) throws Exception {

        final JAXBContext context = JAXBContext.newInstance(Callback.class);

        final Callback before = new Callback();
        before.id = 0L;
        before.name = "name";

        final StringWriter writer = new StringWriter();
        context.createMarshaller().marshal(before, writer);
        writer.flush();

        final String xml = writer.toString();
        System.out.println(xml);

        final StringReader reader = new StringReader(xml);
        final Callback after =
            (Callback) context.createUnmarshaller().unmarshal(reader);
    }

    private void beforeMarshal(Marshaller marshaller) {
        System.out.println("beforeMarshal(" + marshaller + ")");
    }

    private void afterMarshal(final Marshaller marshaller) {
        System.out.println("afterMarshal(" + marshaller + ")");
    }

    private void beforeUnmarshal(final Unmarshaller unmarshaller,
                                 final Object parent) {
        System.out.println(
            "beforeUnmarshal(" + unmarshaller + ", " + parent + ")");
    }

    private void afterUnmarshal(final Unmarshaller unmarshaller,
                                final Object parent) {
        System.out.println(
            "afterUnmarshal(" + unmarshaller + ", " + parent + ")");
    }

    @XmlAttribute
    private Long id;

    @XmlElement
    private String name;
}
beforeMarshal(com.sun.xml.internal.bind.v2.runtime.MarshallerImpl@ccfcd20)
beforeMarshal(com.sun.xml.internal.bind.v2.runtime.MarshallerImpl@ccfcd20)
afterMarshal(com.sun.xml.internal.bind.v2.runtime.MarshallerImpl@ccfcd20)
afterMarshal(com.sun.xml.internal.bind.v2.runtime.MarshallerImpl@ccfcd20)
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><callback id="0"><name>name</name></callback>
beforeUnmarshal(com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl@1ebe99f8, null)
afterUnmarshal(com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl@1ebe99f8, null)
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s