Using JAXB to customise the generation of java enums
- by belltower
I'm using an external bindings file when using jaxb against an XML schema.
I'm mostly using the bindings file to map from the XML schema primitives to my own types.
This is a snippet of the bindings file
<jxb:bindings version="1.0" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ai="http://java.sun.com/xml/ns/jaxb/xjc" extensionBindingPrefixes="ai">
<jxb:bindings schemaLocation="xsdurl" node="xs:schema">
<jxb:globalBindings>
<jxb:javaType name="com.companyname.StringType" xmlType="xs:string"
parseMethod="parse" printMethod="print" hasNsContext="true">
</jxb:javaType>
</jxb:globalBindings>
</jxb:bindings>
</jxb:bindings>
So whenever a xs:string is encountered, the com.companyname.StringType the methods print / parse are called for marshalling/unmarshalling etc.
Now if JAXB encounters an xs:enumeration it will generate a java enum.
For example:
<xs:simpleType name="Address">
<xs:restriction base="xs:string">
<xs:enumeration value="ADDR"/>
<xs:enumeration value="PBOX"/>
</xs:restriction>
</xs:simpleType>
public enum Address
{
ADDR,
PBOX,
public String value() {
return name();
}
public static Address fromValue(String v) {
return valueOf(v);
}
}
Does anyone know if it is possible to customise the creation of an enum like it is for a primitive? I would like to be able to:
Add a standard member variable / other methods to every enum generated by jaxb.
Specify the static method used to create the enum.