customizing java code generation of wsimport

1k Views Asked by At

In my XSD I've the following type definition:

<xs:simpleType name="Color">
    <xs:annotation />
    <xs:restriction base="xs:token">
        <xs:enumeration value="B">
            <xs:annotation>
                <xs:appinfo>
                    <codeName>BLUE</codeName>
                </xs:appinfo>
            </xs:annotation>
        </xs:enumeration>
        <xs:enumeration value="R">
            <xs:annotation>
                <xs:appinfo>
                    <codeName>RED</codeName>
                </xs:appinfo>
            </xs:annotation>
        </xs:enumeration>
    </xs:restriction>
</xs:simpleType>

Out of the box wsimport would generate the following java enum out of it:

public enum Color {
    B,
    R;

    public String value() {
        return name();
    }

    public static Color fromValue(String v) {
        return valueOf(v);
    }
}

I'd like wsimport to consider the long descriptions in the codename elements <codeName>BLUE</codeName> and include them as an instance variable in the java enum. Is this prossible for example by using an JAXB binding file?

1

There are 1 best solutions below

4
On

You can provide a one-to-one mapping of values by providing a JAXB bindings file. Within the bindings file, you would want something like this:

<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
               xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
               jaxb:version="2.0">
  <jaxb:bindings schemaLocation="mySchema.xsd">
    <jaxb:bindings node="xsd:simpleType[@name='Color']">
        <jaxb:typesafeEnumClass>
            <jaxb:typesafeEnumMember value="R" name="RED" />
            <jaxb:typesafeEnumMember value="B" name="BLUE" />
        </jaxb:typesafeEnumClass>
    </jaxb:bindings>
  </jaxb:bindings>
<jaxb:bindings>

Alternatively, you could provide the JAXB bindings inline within the schema, but I do not like that approach because it clutters the schema with implementation details that will probably not apply to everyone.

An example of that is below:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
           jxb:version="1.0">

    <xs:simpleType name="Color">
        <!--xs:annotation>
            <xs:appinfo>
                <jaxb:typesafeEnumClass />
            </xs:appinfo>
        </xs:annotation-->
        <xs:restriction base="xs:token">
            <xs:enumeration value="B">
                <xs:annotation>
                    <xs:appinfo>
                        <jaxb:typesafeEnumMember name="BLUE" />
                    </xs:appinfo>
                </xs:annotation>
            </xs:enumeration>
            <xs:enumeration value="R">
                <xs:annotation>
                    <xs:appinfo>
                        <jaxb:typesafeEnumMember name="RED" />
                    </xs:appinfo>
                </xs:annotation>
            </xs:enumeration>
        </xs:restriction>
    </xs:simpleType>

</xs:schema>

You could also do it all at once, with it being a bit disjoint from the actual values, therefore a bit more duplicative:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
           jxb:version="1.0">

    <xs:simpleType name="Color">
        <xs:annotation>
            <xs:appinfo>
                <jaxb:typesafeEnumClass>
                    <jaxb:typesafeEnumMember name="RED" />
                    <jaxb:typesafeEnumMember name="BLUE" />
                </jaxb:typesafeEnumClass>
            </xs:appinfo>
        </xs:annotation>
        <xs:restriction base="xs:token">
            <xs:enumeration value="B" />
            <xs:enumeration value="R" />
        </xs:restriction>
    </xs:simpleType>

</xs:schema>