nl.vpro.xml.bind.EnumAdapter Maven / Gradle / Ivy
package nl.vpro.xml.bind;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.annotation.adapters.XmlAdapter;
/**
* This is a nice idea. The sad thing however, is that when using this, the generated XSD will not anymore be an enum.
*
* @author Michiel Meeuwissen
* @since 1.63
*/
public abstract class EnumAdapter> extends XmlAdapter {
private final Class enumClass;
protected EnumAdapter(Class enumClass) {
this.enumClass = enumClass;
}
protected T valueOf(String v) {
return Enum.valueOf(enumClass, v.trim());
}
@Override
public T unmarshal(String v) throws Exception {
if (v == null) {
return null;
}
try {
return valueOf(v.trim());
} catch (IllegalArgumentException iae) {
try {
return valueOf(v.trim().toUpperCase());
} catch (IllegalArgumentException e) {
throw new JAXBException(e);
}
}
}
@Override
public String marshal(T v) {
if (v == null) {
return null;
}
return v.name();
}
}