Reputation: 130
In my code I need to map enumerated types from an XML file based on my schema to an enumerated type in another part of the code and vice versa.
The problem is that a construct like:
<simpleType name="sensorKindType">
<restriction base="string">
<enumeration value="ATS_S2_VOLTAGE_1_2" />
<enumeration value="ATS_S2_VOLTAGE_2_3" />
</restriction>
</simpleType>
Will cause the following to be generated.
@XmlEnumValue("ATS_S2_VOLTAGE_1_2")
ATS_S_2_VOLTAGE_1_2("ATS_S2_VOLTAGE_1_2"),
@XmlEnumValue("ATS_S2_VOLTAGE_2_3")
ATS_S_2_VOLTAGE_2_3("ATS_S2_VOLTAGE_2_3"),
However, what I would prefer is to have the following:
@XmlEnumValue("ATS_S2_VOLTAGE_1_2")
ATS_S2_VOLTAGE_1_2("ATS_S2_VOLTAGE_1_2"),
@XmlEnumValue("ATS_S2_VOLTAGE_2_3")
ATS_S2_VOLTAGE_2_3("ATS_S2_VOLTAGE_2_3"),
In other words, no underscore between the S and the 2.
Upvotes: 2
Views: 1911
Reputation: 729
You can control the names of the generated enums using a custom JAXB binding. If you have access to the schema, then you can inline these bindings in the schema as shown below. If you don't have access to the schema then you can define these preferences in an external bindings file.
<xs:schema elementFormDefault="qualified"
targetNamespace="stackoverflow"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
jaxb:version="2.1"
xsi:schemaLocation=
"http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd">
<xs:simpleType name="sensorKindType">
<xs:restriction base="xs:string">
<xs:enumeration value="ATS_S2_VOLTAGE_1_2">
<xs:annotation>
<xs:appinfo>
<jaxb:typesafeEnumMember name="ATS_S2_VOLTAGE_1_2"/>
</xs:appinfo>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="ATS_S2_VOLTAGE_2_3">
<xs:annotation>
<xs:appinfo>
<jaxb:typesafeEnumMember name="ATS_S2_VOLTAGE_2_3"/>
</xs:appinfo>
</xs:annotation>
</xs:enumeration>
</xs:restriction>
</xs:simpleType>
This worked fine for me with the default settings for the JAXB maven plugin with the snippet below in your pom file:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
</plugin>
The following type is generated:
@XmlType(name = "sensorKindType", namespace = "stackoverflow")
@XmlEnum
public enum SensorKindType {
ATS_S2_VOLTAGE_1_2,
ATS_S2_VOLTAGE_2_3;
public String value() {
return name();
}
public static SensorKindType fromValue(String v) {
return valueOf(v);
}
}
Upvotes: 2