views:

618

answers:

1

I have a series of xml messages, all with their own schemas and namespaces. The messages are currently marshalled using JAXB (we still live in a Java 1.4 environment) and we have a large amount of legacy code using this JAXB code so any solution needs to be minimally intrusive.

My problem is that while each of the messages has a set of common header tags the namespace covers the entire message and therefore makes the header for each message unique as well. As a result the "common" header sections are loaded into the namespace bound versions using a common class that is implemented using very ugly proxy classes and dynamic reflection code. This common class has been identified as the source of some performance issues.

Ideally I want to implement a replacement using the following:

  • Use Maven2 to replace current manual jaxb build process.
  • Generate once-off JAXB classes for "common" headers to be reused in each message.
  • Convert current dynamic/proxy classes to use the above concrete header classes.
  • Generate JAXB classes for unqiue section of each message.

Unfortunately, I don't have control of the message structure otherwise I would look at creating a seperate "header" namespace. I thought of running an XSLT transform to "rename" the header namespace after marshalling and prior to unmarshalling but I would prefer to avoid the extra load if possible, even if it means a more complex build.

Is what I want to do feasible or have I missed something fundamental? Is there any hints as to how to implement? Plugin versions etc?

Addendum 1: Binding using javaType would do the job but it appears that doesn't work for complex types.

Addendum 2: Binding using class almost does it as well but I would want it to specify a specific class and package so I could ignore the generated duplicates.

+1  A: 

JAXB 2.x has a @XmlJavaTypeAdapter annotation which may be the solution to your problem (see this blog by Kohsuke Kawaguchi).

You can map your common header class to the generated namespace-specific header classes with an implementation of XmlAdapter<XMLHeaderFromNamespaceX, CommonHeader> and use the adapter with XmlJavaTypeAdapter.

However, as a downside you would need an adapter for each of your particular namespaces.

janko
I had spotted that but it has a prerequisite of Java 1.5 and I'm stuck with 1.4 at the moment.
Michael Rutherfurd
Ah, sorry, I missed that.
janko
It's far from ideal, but if you're still really stuck you could try using that and then retrotranslating the annotations in the jaxb-api jar
GaZ