Adding annotations to JAXB-generated classes which depend on information in XSD

4.6k views Asked by At

I have a WSDL + XSD that needs to be turned into Java classes. That's pretty simple - wsimport will handle that without issue. However, I also need to be able to add annotations to the generated classes, and those annotations need to contain information that is contained in the XSD (in particular, they need to reference the xsd:maxLength or xsd:length properties).

Why? Because I plan to transform them into a flat file afterwards, using Bindy. For reference, I know that I can use Annox to add custom annotations to the generated classes, but as far as I'm aware, that would require that either all annotations are identical, with all parameters being identical, or specifying annotations for each element individually, with no way to specify the annotation once along with some way (e.g. xpath) of specifying that the value of one of the parameters should be different for each element.

That is, given a schema extract like

<xsd:element name="smapleRequest">
    <xsd:sequence>
         <xsd:element name="ELEMENT_ONE">
             <xsd:simpleType>
                 <xsd:restriction base="xsd:string">
                     <xsd:length value="3" />
                 </xsd:restriction>
             </xsd:simpleType>
         </xsd:element>
         <xsd:element name="ELEMENT_TWO">
             <xsd:simpleType>
                 <xsd:restriction base="xsd:string">
                     <xsd:maxLength value="8" />
                 </xsd:restriction>
             </xsd:simpleType>
         </xsd:element>
    </xsd:sequence>
</xsd:element>

I would like to see classes that look this:

.
.
.
@FixedLengthRecord
public class SampleRequest {

    @XmlElement(name = "ELEMENT_ONE", required = true)
    @DataField(pos = 1, length=3)
    protected String elementOne;


    @XmlElement(name = "ELEMENT_TWO", required = true)
    @DataField(pos = 4, length=8)
    protected String elementTwo;
    .
    .
    .
}

Ideally, I would like to be able to do this without having to duplicate all the information from the XSD into the JAXB Binding File. I mean, I could, but with potentially hundreds of elements per web service method, and dozens of methods, that would get very, very old very, very fast. At that point, I would probably have to use another tool to generate the XSD and JAXB binding file(s) from the COBOL!

So, does anyone know if this is possible? Have I just missed something in Annox? Or am I just asking for too much here?

2

There are 2 answers

1
Patrice M. On

You have few options: XJC plugins is one route and Annox looks interesting. But I'm no expert so I'll let others explore it with you.

The other route I would suggest you consider, if you get stuck with the first one, is to post-process your generated JAXB sources via annotation processing (formerly the apt tool, now part of the javac tool) to access the XSD and append your annotations on the fly. Not sure that would work for all your cases, but in the example you gave, the JAXB-generated annotations should be enough to construct an XPath expression to read the corresponding XML element type characteristics. Assuming your needs are essentially around the field length, that should be few use cases and XPath expressions.

0
Hubbitus On

To automatically add XJsr303Annotations annotations you could use xjc plugin https://github.com/krasa/krasa-jaxb-tools

Plese see my answer Generation of XSD restrictions in a schema generated from Java JAXB annotated classes for details.