I want to generate classes using maven-jaxb2-plugin.
My input xsd looks like the following:
<complexType>
<complexContent>
<restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
<sequence>
<element name="ReferencedElementA" type="{http://www.w3.org/2001/XMLSchema}IDREFS" minOccurs="0" />
<element name="ReferencedElementB" type="{http://www.w3.org/2001/XMLSchema}IDREF"
maxOccurs="unbounded" minOccurs="0" />
</sequence>
</restriction>
</complexContent>
Please note, that i can't alter the schema as it is provided by an external customer!
This is the generated code:
@XmlList
@XmlElement(name = "ReferencedElementA")
@XmlIDREF
@XmlSchemaType(name = "IDREFS")
protected List<Object> referencedElementA;
@XmlElementRef(name = "ReferencedElementB", namespace = "ABC", type = JAXBElement.class, required = false)
protected List<JAXBElement<Object>> referencedElementB;
As you see, the "ReferencedElementB" is generated as a List> with the @XMLElementRef-Annotation.
What i want, is something like the following:
@XmlElement(name = "ReferencedElementB")
@XmlIDREF
@XmlSchemaType(name = "IDREF")
protected List<Object> referencedElementB;
Using the workaround from Blaise Doughan, i expanded my binding file with the following code:
<bindings node = ".//xs:element[@name='ReferencedElementB']">
<class ref="java.lang.Object"/>
</bindings>
This almost finished the job, generating code like this:
@XmlElement(name = "ReferencedElementB")
@XmlSchemaType(name = "IDREF")
protected List<Object> referencedElementB;
However, the @XmlIDREF-Annotation is still missing. This produces errors during marshalling.
I've tried to inject the missing annotation via annox-plugin:
<bindings node = ".//xs:element[@name='ReferencedElementB']">
<class ref="java.lang.Object"/>
<annox:annotate target="field">@javax.xml.bind.annotation.XmlIDREF</annox:annotate>
</bindings>
But the generation fails with
compiler was unable to honor this annox:annotate customization. It is attached to a wrong place, or its inconsistent with other bindings
Once again, i'm not able to alter the input xsd.
Thank you in advance!
Please note, that i can't alter the schema as it is provided by an external customer!
I read this all the time here on SO and I always wonder - why? Why can't you alter the schema?
Well, of course you can alter the schema prior to compilation. Take the original schema and apply a patch/XSLT/whatever to modify it. As long as the result structurally matches the original schema, it is fine.
Consider your case: you use tricks and workarounds to effectively hack the generated code. How much better is it compared to modifying the schema prior to compilation? I don't think it is better at all. It is more complicated, error-prone and at the end you cannot say that your generated code is the reflection of your schema. Effectively, you do not have a schema for your code. You can't even say if your hacked generated code is an adequate reflection of the original schema.
Modifying the schema prior to compilation is quite easy. You can easily compare original vs. modified and see if changes are compatible or not. Generation is straightforward, no need for hacks and workarounds.
Now to your question.
The binding
<annox:annotate target="field">@javax.xml.bind.annotation.XmlIDREF</annox:annotate>
looks fine for me. For some reason it is not considered during schema compilation, this is why you get the error.
Hard to say why it is not considered. I'd try the following:
Check that you actually include/turn on the JAXB2 Basics Annotate plugin in your compilation:
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<configuration>
<extension>true</extension>
<args>
<arg>-Xannotate</arg>
</args>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-annotate</artifactId>
</plugin>
</plugins>
</configuration>
</plugin>
Put the customization element in its own bindings
element. Maybe it does not work together with class
for some reason.
I don't see more at the moment should work.
If it does not help, please send me a PR with a minimal reproducing project here:
https://github.com/highsource/jaxb2-annotate-plugin-support
under i/idrefs. I will take a look.
Disclaimer: I am the author of annox, jaxb2-annotate-plugin and maven-jaxb2-plugin.