JAXB allows you to directly locate Enum instances from XML using annotation @XMLEnum. JAXB seems to be case sensitive when it comes to determining the value from @XMLEnumValue.
But I ran into a problem in which I needed to find an instance of Enum case insensitive. Does JAXB provide specific functionality? Without this, I am stuck in a manual search for the Enum constant.
I can use XMLAdapterfor this, but it is very tedious to maintain an adapter for each enum created.
EDIT:
Why should you find case-insensitive transfers?
Because I use xml to read the configuration for my system, and I do not want users to be limited to a fixed case. Hope this answers your question.
public class CaseInsensitiveEnumAdapter<E extends Enum<E>> extends XmlAdapter<String, E> {
private final Class<E> clazz;
public CaseInsensitiveEnumAdapter(Class<E> clazz) {
this.clazz = clazz;
}
@Override
public E unmarshal(String v) throws Exception {
return Enum.<E>valueOf(clazz, v.toUpperCase().trim());
}
@Override
public String marshal(E v) throws Exception {
return v.name();
}
}
.class , .