All Downloads are FREE. Search and download functionalities are using the official Maven repository.

brooklyn.util.xstream.EnumCaseForgivingConverter Maven / Gradle / Ivy

There is a newer version: 0.7.0-M1
Show newest version
package brooklyn.util.xstream;

import brooklyn.util.exceptions.Exceptions;

import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.converters.enums.EnumConverter;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;

/** ... except this doesn't seem to get applied when we think it should
 * (normal xstream.resgisterConverter doesn't apply to enums) */
public class EnumCaseForgivingConverter extends EnumConverter {

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
        Class type = context.getRequiredType();
        if (type.getSuperclass() != Enum.class) {
            type = type.getSuperclass(); // polymorphic enums
        }
        String token = reader.getValue();
        // this is the new bit (overriding superclass to accept case-insensitive)
        return resolve(type, token);
    }

    public static > T resolve(Class type, String token) {
        try {
            return Enum.valueOf(type, token.toUpperCase());
        } catch (Exception e) {
            
            // new stuff here:  try reading case insensitive
            
            Exceptions.propagateIfFatal(e);
            try {
                for (T v: type.getEnumConstants())
                    if (v.name().equalsIgnoreCase(token)) return v;
                throw e;
            } catch (Exception e2) {
                throw Exceptions.propagate(e2);
            }
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy