java - Custom (un)marshalling for xsd:union when genereting classes using JAXB -


i'm working on application communicates sending , receiving xml messages. these messages defined in xsd schema. now, we'd have schema in future, able update without breaking clients. have solved of problems adding <anyattribute> elements appropriate places. have huge problem <enumeration> elements.

the idea use this:

<xsd:simpletype name="myfruitenumtype">     <xsd:restriction base="xsd:string">          <xsd:enumeration value="apple"/>          <xsd:enumeration value="orange"/>     </xsd:restriction> </xsd:simpletype>  <xsd:simpletype name="myfruittype">     <xsd:union membertypes="myfruitenumtype xs:string"/> </xsd:simpletype>  <xsd:element name="fruit" type="myfruittype"/> 

in practice, xs:string not used, when update schema this:

<xsd:simpletype name="myfruitenumtype">     <xsd:restriction base="xsd:string">          <xsd:enumeration value="apple"/>          <xsd:enumeration value="orange"/>          <xsd:enumeration value="banana"/>     </xsd:restriction> </xsd:simpletype> 

we can create message with

<fruit>banana</fruit> 

and still pass validation using old schema (although client apps have default or error behaviour case).

but problem appears when use jaxb's xjc generate classes. <xsd:union> genereted java's string property in fruit class. understanble default behaviour, i'd customize it, class fruit use enum myfruitenumtype. don't care case, when string doesn't match enumeration arrives in xml. example throwing kind of validation exception fine me (or map unknown strings default value, etc).

i know jaxb's <typesafeenummember> don't think can use here (or don't know how). thought <javatype parsemethod=... printmethod=...>, need write methods tens of enums have, because can't pass enum parse , print methods take advantage of enum's valueof() method.

so sum up, question is: can customize how xsd:union translated java class variable?

so in end, declared binding in xjb file this:

<bindings node="//xs:simpletype[@name='myfruittype']">     <javatype name="com.example.myfruitenumtype"/> </bindings> 

and created custom jaxb plugin, customizes xmladapter generated xjc work enum.


Comments