Forcing Jaxb preference of union member type when compiling

1.3k Views Asked by At

I am compiling an XSD using xjc that includes the following type:

<xs:simpleType name="CPT-DateTime">
    <xs:annotation>
        <xs:appinfo>Can be specified as a integer number or as xs:dateTime</xs:appinfo>
    </xs:annotation>
    <xs:union memberTypes="xs:unsignedLong xs:dateTime"/>
</xs:simpleType>

The resulting classes that use this type are compiled with this element set as String, while I would prefer them to use XMLGregorianCalendar.

Is there a way to that would force xjc to pick the xs:dateTime member type over the String? I've seen how to do this for simple types but not unions.

3

There are 3 best solutions below

0
On BEST ANSWER

I believe, what you've referenced should actually work for arbitrary simple types. Assuming tns is a declared prefix for the targen namespace in your schema, try the following mapping:

<globalBindings>
    <javaType name="javax.xml.datatype.XMLGregorianCalendar" xmlType="tns:CPT-DateTime" .../>
</globalBindings>

However, I am not 100% sure.

Also check xjc:javaType.

0
On

Similar question here xjc: override xs:simpleType definition

javaType won't work because XMLGregorianCalendar is abstract. I'm headed down the path of adding a small adapter.

0
On

The solution comes courtesy of denishaskin's final implementation:

<?xml version="1.0" encoding="UTF-8"?>
<jxb:bindings jxb:version="2.1" xmlns:jxb="{NS1-n}" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc">
<jxb:bindings node="/xs:schema" schemaLocation="../{SCHEMA}.xsd">
<jxb:globalBindings>
<jxb:javaType name="org.joda.time.DateTime" xmlType="xs:dateTime"/>
<jxb:javaType name="org.joda.time.DateTime" xmlType="tns:CPT-DateTime"/>
<jxb:javaType name="org.joda.time.LocalDate" xmlType="tns:CPT-Date"/>
<jxb:javaType name="org.joda.time.LocalTime" xmlType="tns:CPT-Time"/>
</jxb:globalBindings>
</jxb:bindings>