0

I have a bunch of XSDs that i transform into POJO with the jaxb maven plugin.

For logging purposes, i'd like to have a "unmarshal" method directly integrated inside the JAXB objects, so that i could call something like generatedPOJO.toXMLString(), knowing that generatedPOJO is the POJO generated from the XSD file via JAXB.

I tried to look in the Custom bindings documentation, but i got nothing out of that.

Thank you for your help.

EDIT: What i want is that JAXB, in addition to generating POJOs from XSD files, adds a toXMLString() method to these POJOs.

This method needs to be generated by JAXB, since i can't edit the generated POJOS.

clapsus
  • 442
  • 6
  • 19
  • The question is unclear, maybe you are looking for something like `afterUnmarshal(Unmarshaller u, Object parent)` – j.con Aug 06 '15 at 15:02
  • No, i only have XSD files in the first place, and i generate POJOs using theses XSDs and the jaxb-maven plugin. So i cannot edit the POJOs to add any Java code since they are generated. But i want to have a method `toXMLString()` in the generated POJOs. This mehtod will have to be either generated by the JAXB maven plugin, or i will declare it in a superclass for all JAXB objects (POJOs) and indicate it as a custom binding for JAXB. I'll edit the question. – clapsus Aug 06 '15 at 15:09
  • Here's a duplicate http://stackoverflow.com/questions/14285928/adding-extra-methods-to-a-jaxb-class-generated-from-schema – j.con Aug 06 '15 at 15:13
  • @j.con I don't think so. It's not just about adding some method, it's about adding a marshalling method. – lexicore Aug 06 '15 at 19:45

1 Answers1

2

In short, don't do this, it won't be a good design.

While, as @j.con pointed out, it is possible to add further methods to the generated classes, using -xinject-code or a custom XJC plugin, adding a marshalling method is not a good idea. With JAXB API, it will be pretty ugly.

To do anything you'll need an instance of JAXBContext. Either you'll pass it to your method or instantiate within the method.

The latter isn't quite good as JAXBContext is instantiated for a collection of classes or packages (context path). So you'll basically have to preset, with which classes your class may be used together. Doing so, you're losing flexibility.

Next, JAXB marshallers produce many things, not just strings/stream results but also DOM or SAX or StAX. JAXB API is quite cool about that. Opting just for strings seems to be a shortsighted choice to be.

Finally, I don't think adding toXMLString() or whatever is so much sweet syntactic sugar compared to a simple utility service or class. And hacking into code generation for that really feels like misplaced effort.

lexicore
  • 42,748
  • 17
  • 132
  • 221
  • Thanks for your pretty complete question. FYI, i choose the XML string over the DOX/SAX juste because i need to display it in the log. I guess i'll juste use final static `JAXContent` for every package. – clapsus Aug 06 '15 at 23:08
  • If you get worried about performance, you will want to know that making the marshal object is a little expensive. So, it is better to make one per thread and hold onto it for a long time. – johnstosh Aug 08 '15 at 05:24
  • As a separate issue, you can instruct JAXB to create its classes with a superclass that you specify. I've never done this, but it was tempting. Another possibility is to use a delegate pattern to create objects which implement both the original interface of the JAXB pojo as well as the interface with the new method named toXmlString(). This isn't recommended because you end up with two objects and it gets confusing when to use the outer object and when to use the inner object. So, for example, if you hand the outer object to JAXB then will it know what to do with it? Nope. – johnstosh Aug 08 '15 at 05:30