4

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?

ipsi
  • 2,049
  • 18
  • 23
  • Could you make xsl transformation that would generate jaxb binding file from your xsd (no need to make it universal)? – Alpedar May 28 '12 at 10:20
  • That could work, though it's another step in the process. The other thing I'm worried about is if the binding file and the XSD get too big. I'm not sure exactly how many methods I'd have on the web service, but it would probably be several dozen, with each method having a hundred or more elements for both request and response. I'm not an expert on the internals of JAXB, but I'd worry that XML files that large could result in OoM exceptions... – ipsi May 28 '12 at 13:03
  • split it in multiple files? Increase available memory? Obviously not ideal solution. – Alpedar May 28 '12 at 13:37
  • Nope. I suspect that splitting into multiple files wouldn't do much, as it will still have to read them all into memory at once to properly handle references between schemas and such I imagine. Of course, I could be wrong about this and it'll handle everything flawlessly, but it's a lot of work to check that. – ipsi May 29 '12 at 02:11

2 Answers2

1

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.

Patrice M.
  • 4,209
  • 2
  • 27
  • 36
  • That's an interesting idea, and one I hadn't thought of, but it's another step to handle. On the other hand, all my cases are really very simple (the example above is representative of all of them), and all that would be necessary is the length. At this stage, though, an XJC plugin to add the annotations I want (and only those) might be the quickest solution. – ipsi May 29 '12 at 08:28
0

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.

Hubbitus
  • 5,161
  • 3
  • 41
  • 47