38.4. Customizing Enumeration Mapping
Overview
jaxb:typesafeEnumClass
element along with one or more jaxb:typesafeEnumMember
elements.
globalBindings
customization.
Member name customizer
globalBinding
element's typesafeEnumMemberName
attribute. The typesafeEnumMemberName
attribute's values are described in Table 38.2, “Values for Customizing Enumeration Member Name Generation”.
Table 38.2. Values for Customizing Enumeration Member Name Generation
Value | Description |
---|---|
skipGeneration (default) | Specifies that the Java enum type is not generated and generates a warning. |
generateName | Specifies that member names will be generated following the pattern VALUE_N . N starts off at one, and is incremented for each member of the enumeration. |
generateError | Specifies that the code generator generates an error when it cannot map an enumeration to a Java enum type. |
Example 38.17. Customization to Force Type Safe Member Names
<schema targetNamespace="http://widget.com/types/widgetTypes" xmlns="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.0"> <annotation> <appinfo> <jaxb:globalBindings typesafeEnumMemberName="generateName" /> </appinfo> </annotation> ... </schema>
Class customizer
jaxb:typesafeEnumClass
element specifies that an XML Schema enumeration should be mapped to a Java enum type. It has two attributes that are described in Table 38.3, “Attributes for Customizing a Generated Enumeration Class”. When the jaxb:typesafeEnumClass
element is specified in-line, it must be placed inside the xsd:annotation
element of the simple type it is modifying.
Table 38.3. Attributes for Customizing a Generated Enumeration Class
Attribute | Description |
---|---|
name | Specifies the name of the generated Java enum type. This value must be a valid Java identifier. |
map | Specifies if the enumeration should be mapped to a Java enum type. The default value is true . |
Member customizer
jaxb:typesafeEnumMember
element specifies the mapping between an XML Schema enumeration
facet and a Java enum type constant. You must use one jaxb:typesafeEnumMember
element for each enumeration
facet in the enumeration being customized.
- It can be placed inside the
xsd:annotation
element of theenumeration
facet it is modifying. - They can all be placed as children of the
jaxb:typesafeEnumClass
element used to customize the enumeration.
jaxb:typesafeEnumMember
element has a name
attribute that is required. The name
attribute specifies the name of the generated Java enum type constant. It's value must be a valid Java identifier.
jaxb:typesafeEnumMember
element also has a value
attribute. The value
is used to associate the enumeration
facet with the proper jaxb:typesafeEnumMember
element. The value of the value
attribute must match one of the values of an enumeration
facets' value
attribute. This attribute is required when you use an external binding specification for customizing the type generation, or when you group the jaxb:typesafeEnumMember
elements as children of the jaxb:typesafeEnumClass
element.
Examples
Example 38.18. In-line Customization of an Enumerated Type
<schema targetNamespace="http://widget.com/types/widgetTypes" xmlns="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.0"> <simpleType name="widgetInteger"> <annotation> <appinfo> <jaxb:typesafeEnumClass /> </appinfo> </annotation> <restriction base="xsd:int"> <enumeration value="1"> <annotation> <appinfo> <jaxb:typesafeEnumMember name="one" /> </appinfo> </annotation> </enumeration> <enumeration value="2"> <annotation> <appinfo> <jaxb:typesafeEnumMember name="two" /> </appinfo> </annotation> </enumeration> <enumeration value="3"> <annotation> <appinfo> <jaxb:typesafeEnumMember name="three" /> </appinfo> </annotation> </enumeration> <enumeration value="4"> <annotation> <appinfo> <jaxb:typesafeEnumMember name="four" /> </appinfo> </annotation> </enumeration> </restriction> </simpleType> <schema>
Example 38.19. In-line Customization of an Enumerated Type Using a Combined Mapping
<schema targetNamespace="http://widget.com/types/widgetTypes" xmlns="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.0"> <simpleType name="widgetInteger"> <annotation> <appinfo> <jaxb:typesafeEnumClass> <jaxb:typesafeEnumMember value="1" name="one" /> <jaxb:typesafeEnumMember value="2" name="two" /> <jaxb:typesafeEnumMember value="3" name="three" /> <jaxb:typesafeEnumMember value="4" name="four" /> </jaxb:typesafeEnumClass> </appinfo> </annotation> <restriction base="xsd:int"> <enumeration value="1" /> <enumeration value="2" /> <enumeration value="3" /> <enumeration value="4" > </restriction> </simpleType> <schema>
Example 38.20. Binding File for Customizing an Enumeration
<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="enumMap.xsd"> <jaxb:bindings node="xsd:simpleType[@name='widgetInteger']"> <jaxb:typesafeEnumClass> <jaxb:typesafeEnumMember value="1" name="one" /> <jaxb:typesafeEnumMember value="2" name="two" /> <jaxb:typesafeEnumMember value="3" name="three" /> <jaxb:typesafeEnumMember value="4" name="four" /> </jaxb:typesafeEnumClass> </jaxb:bindings> </jaxb:bindings> <jaxb:bindings>