commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <wikidi...@apache.org>
Subject [Jakarta-commons Wiki] Update of "Betwixt/TipsAndHints/Java5Enums" by JesseSweetland
Date Wed, 09 Aug 2006 13:08:25 GMT
Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Jakarta-commons Wiki" for change notification.

The following page has been changed by JesseSweetland:
http://wiki.apache.org/jakarta-commons/Betwixt/TipsAndHints/Java5Enums

New page:
To render Java 5 Enums as primitive strings, two steps are needed.  First, Betwixt needs to
be told that the Enum class should be treated as a primitive and not a Bean, otherwise the
Enum will be introspected and the property accessor getDeclaringClass will be found.  The
result is this:

{{{
<myEnumType>
    <declaringClass>my.package.MyEnumType</declaringClass>
</myEnumType>
}}}

To make Betwixt treat Java 5 Enums as primitive types, implement a TypeBindingStrategy:

{{{
import org.apache.commons.betwixt.strategy.*;
public class EnumTypeBindingStrategy extends TypeBindingStrategy {
    public TypeBindingStrategy.BindingType bindingType(Class type) {
        TypeBindingStrategy.BindingType bindingType = null;
        if(Enum.class.isAssignableFrom(type)) {
            bindingType = TypeBindingStrategy.BindingType.PRIMITIVE;
        } else {
            bindingType = TypeBindingStrategy.DEFAULT.bindingType(type);
        }
        return bindingType;
    }
}
}}}

Once the TypeBindingStrategy class has been created, configure the Betwixt BeanWriter to use
it:

{{{
StringWriter sw = new StirngWriter();
BeanWriter bw = new BeanWriter(sw);
bw.getXMLIntrospector().getConfiguration().setTypeBindingStrategy(new EnumTypeBindingStrategy());
}}}

Next, we need to tell Betwixt how to convert back and forth between String and Enum values.
 To do this, implement an ObjectStringConverter:

{{{
import org.apache.commons.betwixt.expression.*;
import org.apache.commons.betwixt.strategy.*;
public class EnumObjectStringConverter extends ObjectStringConverter {
    public String objectToString(Object object, Class type, Context context) {
        String value = null;
        if(object instanceof Enum) {
            value = ((Enum)object).name();
        } else {
            value = super.objectToString(object, type, context);
        }
        return value;
    }
    
    public Object stringToObject(String value, Class type, Context context) {
        Object object = null;
        if(Enum.class.isAssignableFrom(type)) {
            object = Enum.valueOf(type, value);
        } else {
            object = super.stringToObject(value, type, context);
        }
        return object;
    }
}
}}}

Now, configure the converter on the BeanWriter:

{{{
StringWriter sw = new StirngWriter();
BeanWriter bw = new BeanWriter(sw);
bw.getXMLIntrospector().getConfiguration().setTypeBindingStrategy(new EnumTypeBindingStrategy());
bw.getBindingConfiguration().setObjectStringConverter(new EnumObjectStringConverter());
}}}
Now the above enumeration should render like this:

{{{
<myEnumType>SOME_CONSTANT</myEnumType>
}}}

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message