Skip to main content

JAXB and custom toString "strategy"

1 reply [Last post]
erik6048
Offline
Joined: 2011-03-13
Points: 0

About JAXB and its toString strategy:

say, xjc has generated this code for me:

public class PurchaseOrderType
    implements ToString //, ...

    // ...   

    public String toString() {
        final ToStringStrategy strategy = JAXBToStringStrategy.INSTANCE;
        final StringBuilder buffer = new StringBuilder();
        append(null, buffer, strategy);
        return buffer.toString();
    }

    public StringBuilder append(ObjectLocator locator, StringBuilder buffer, ToStringStrategy strategy) {
        strategy.appendStart(locator, this, buffer);
        appendFields(locator, buffer, strategy);
        strategy.appendEnd(locator, this, buffer);
        return buffer;
    }

    public StringBuilder appendFields(ObjectLocator locator, StringBuilder buffer, ToStringStrategy strategy) {
        {
            USAddress theShipTo;
            theShipTo = this.getShipTo();
            strategy.appendField(locator, this, "shipTo", buffer, theShipTo);
        }
        {
            USAddress theBillTo;
            theBillTo = this.getBillTo();
            strategy.appendField(locator, this, "billTo", buffer, theBillTo);
        }
        // ...
        return buffer;
    }


    // ...
}

Now I need the toString method to do something special for me.
Say, returning a name in all uppercase or so.
They say, I have to write a custom class for that and somehow "plug" that into above code, but without altering above code.
What would that new class look like ? and how do I do the plugging in ?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
blaise_doughan
Offline
Joined: 2003-09-30
Points: 0

Hello,

The following link provides instructions on writing an XJC plug-in. It may help you out with what you are trying to do:
http://weblogs.java.net/blog/kohsuke/archive/2005/06/writing_a_plugi.html

-Blaise