I use maven JaxB plugin to generate source from XSD.
The plugin details are as below,
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.8.3</version>
Enumerations defined in XSD has two values,
<simpleType name="MyEnum">
<restriction base="xsd:string">
<enumeration value="SimpleText" />
<enumeration value="ComplexText" />
</restriction>
</simpleType>
The generated code adds underscore between work boundaries of the enum values. For eg:, "SimpleText" comes as SIMPLE_TEXT in enum.
Generated code,
@XmlType(name = "MyEnum")
@XmlEnum
public enum MyEnum {
@XmlEnumValue("SimpleText")
SIMPLE_TEXT("SimpleText"),
@XmlEnumValue("ComplexText")
COMPLEX_TEXT("ComplexText");
private final String value;
MyEnum(String v) {
value = v;
}
public String value() {
return value;
}
public static MyEnum fromValue(String v) {
for (MyEnum c: MyEnum.values()) {
if (c.value.equals(v)) {
return c;
}
}
throw new IllegalArgumentException(v);
}
}
The problem occurs when an enumeration has same text separated with underscore at different places. For Eg;
<simpleType name="MyEnum">
<restriction base="xsd:string">
<enumeration value="My_Simple_Text" />
<enumeration value="MySimple_Text" />
</restriction>
</simpleType>
doesn't convert to an enum. Is there any way to avoid JaxB adding underscore between words.
You can use an external bindings file to customize the enum generation:
UPDATE
Turns out the better approach is to use the following bindings file instead: