views:

1642

answers:

3

I have an existing XML schema that contains a type that I would like to map to a Java Map of some sort using JAXB. My build process takes the schema and creates the beans. I would like to customize this process by having one of the complexTypes in my schema map to a java.util.Map. I've read somewhere that JAXB "can't do interfaces." I'm not sure if that applies in this case, but if so I'd be fine with it mapping to a HashMap. Also, it doesn't necessarily have to be the complexType that maps to a Map, it could be an element if that's what's required.

I control my JAXB generation using a .xjb file, so I'm looking for some <jaxb:bindings> to put in there. I've tried the following, but it does not work:

<jaxb:bindings schemaLocation="myschema.xsd" node="/xs:schema">
 <jaxb:globalBindings>
  <jaxb:serializable uid="1"/>
 </jaxb:globalBindings>
 <jaxb:schemaBindings>
  <jaxb:package name="com.myschema.client.types"/>
 </jaxb:schemaBindings>
 <jaxb:bindings node="//xs:complexType[@name='MapType']">
      <jaxb:javaType name="java.util.HashMap"
   parseMethod="com.myschema.common.MapConverter.parseObjectToMap"
   printMethod="com.myschema.common.MapConverter.printMapToObject" />
 </jaxb:bindings>
</jaxb:bindings>

Edit: I've added more detail on the above binding that I've already tried. It generates the following error during schema compile:

[jaxb] [ERROR] compiler was unable to honor this javaType customization. It is attached to a wrong place, or its inconsistent with other bindings.
+4  A: 

Have you looked at example here (Customized mapping of HashMap): http://java.sun.com/javase/6/docs/api/javax/xml/bind/annotation/adapters/XmlAdapter.html

Need to create custom XmlAdapter I'm afraid.

maximdim
Hey, this looks pretty close to what I want to do! The only thing is, I'm using schema to generate Java types (as part of my build process). I don't mind writing the adapter, but I would like to avoid having to have pre-implemented the class that generates the schema (<code>MyHashMapType</code> and <code>MyHashMapEntryType</code> in the linked example) as this is going backwards for me. Is this possible?
Zach
I don't think this strictly necessary, most likely it's just extended example. I know that we have done it in the past with just schema, binding customization file and adapter.
maximdim
Sounds like JSON...
duffymo
An `XmlAdapter` is what XJC should be generating using @Zach's `.xjb` configuration.
skaffman
A: 

<jaxb:javaType> will not work because it can only be used for mapping between XML Schema primitives and Java types. Since I want to map between a complex type and a Java type, I cannot use this.

Zach
+2  A: 

Your XJC file is using the "standard" javaType directive, which I believe is limited to converting String values to and from a representative java type. As such, it's only suitable for converting simple element and attribute content.

The XJC tool provides an enhanced version of javaType, which in theory should be able to handle more complex structures. Unfortunately, this has yet to be implemented:

https://jaxb.dev.java.net/issues/show%5Fbug.cgi?id=209 (unresolved)

For instance, being able to map a schema type to a HashMap might be a likely requirement for those using model driven design/implementation. AFAICS, this is not possible today. Therefore, one must manually edit the generated code.

Looks like you're out of luck.

skaffman
I was hoping this wouldn't be the answer, but I'm glad to know I'm not missing anything.
Zach