Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xjc and XSD choice

When I run xjc to generate a Java type representing this XSD snippet:

  <xs:complexType name="fileUploadRequest">
    <xs:choice>
      <xs:element name="path" type="xs:string"/>
      <xs:element name="file" type="xs:base64Binary"/>
    </xs:choice>
  </xs:complexType>

I get a class that's indistinguishable from what it would have been if I'd specified a sequence with optional elements instead.

I want a type with a little bit of intelligence, that'll let me have at most 1 element of my choice at a time. If I invoke the generated setFile method for example, it should make the path null. Is there some plugin I can use for what seems like an obvious requirement of a code generator?

like image 915
blueimpb Avatar asked Sep 04 '13 16:09

blueimpb


1 Answers

binding.xml

You can use the following external binding file to generate the type of property you are looking for:

<?xml version="1.0" encoding="UTF-8"?>
<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
          version="2.1">
    <globalBindings choiceContentProperty="true"/>
</bindings> 

XJC Call

The binding file is referenced using the -b flag.

xjc -b binding.xml schema.xsd

Generated Property

Now the following property will be generated:

@XmlElements({
    @XmlElement(name = "path", type = String.class),
    @XmlElement(name = "file", type = byte[].class)
})
protected Object pathOrFile;

For More Information

  • http://blog.bdoughan.com/2011/04/xml-schema-to-java-xsd-choice.html
like image 108
bdoughan Avatar answered Sep 21 '22 00:09

bdoughan