commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "J. H." <jhu...@efn.org>
Subject Re: Boolean getters in Betwixt
Date Fri, 22 Sep 2006 22:29:05 GMT

Alas, this still doesn't recognize "public Boolean isSomeValue()" as a valid getter, though
i suspect if i could get that to work i'd also have to implement 
these steps as well in order to get everything running smoothly. Which leads to the question:

Where did you read about TypeBindingStrategy? It doesn't appear to be discussed in the documentation
i found. If there's some other documentation out there that 
discusses more of these intricacies of Bewixt i could proboboly figure out the rest.

Thanks for this hint. I do believe it is a step in the right direction!

- jordi

Sweetland, Jesse wrote:
> You might need a custom TypeBindingStrategy and ObjectStringConverter.  This
> should work:
> 
> public class BooleanTypeBindingStrategy extends TypeBindingStrategy {
> 	public BindingType bindingType(Class type) {
> 		if(Boolean.class.isAssignableFrom(type)) {
> 			return BindingType.PRIMITIVE;
> 		} else {
> 			return DEFAULT.bindingType(type);
> 		}
> 	}
> }
> 
> public class BooleanObjectStringConverter extends
> DefaultObjectStringConverter {
> 	public String objectToString(Object object, Class type, Context
> context) {
> 		if(object == null) return null;
> 		else if(Boolean.class.isAssignableFrom(type)) {
> 			return object.toString();
> 		} else {
> 			return super.objectToString(object, type, context);
> 		}
> 	}
> 
> 	public Object stringToObject(String string, Class type, Context
> context) {
> 		if(string == null) return null;
> 		else if(Boolean.class.isAssignableFrom(type)) {
> 			return new Boolean(string);
> 		} else {
> 			return super.stringToObject(string, type, context);
> 		}
> 	}
> }
> 
> To use these, you need to register them on the BeanWriter:
> 
> BeanWriter bw = new BeanWriter();
> bw.getXMLIntrospector()
> 	.getConfiguration()
> 	.setTypeBindingStrategy(new BooleanTypeBindingStrategy());
> bw.getBindingConfiguration()
> 	.setObjectStringConverter(new BooleanObjectStringConverter());
> 
> If you have a need to implement this for multiple types, then check the wiki
> for the CustomerObjectStringConverter class which allows you to register
> multiple converters by class (similar to the bean creation chain).  You can
> only have one TypeBindingStrategy, so to deal with multiple types you'd have
> to keep adding new else ifs in the example above.
> 
> Hope that helps,
> Jesse
> 


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


Mime
View raw message