commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sebastian <nospam...@gmx.net>
Subject Re: Betwixt: Setting up BeanWriter to render a bean property as attribute rather than element
Date Sun, 08 Feb 2004 13:28:40 GMT
Hi Robert,
based on the refactoring branch I created a TypeMapper that looks like this:

public class BetwixtTypeMapper extends SimpleTypeMapper
{
   protected HashSet attributes = new HashSet();
   protected HashSet elements = new HashSet();

   public synchronized void registerAsAttribute(String propertyName) {
     if (elements.contains(propertyName)) elements.remove(propertyName);
     attributes.add(propertyName);
   }

   public synchronized void unregisterAttribute(String propertyName) {
     attributes.remove(propertyName);
   }

   public synchronized void registerAsElement(String propertyName) {
     if (attributes.contains(propertyName))
         attributes.remove(propertyName);
     elements.add(propertyName);
   }

   public synchronized void unregisterElement(String propertyName) {
     elements.remove(propertyName);
   }

   public Binding bind(
     String propertyName,
     Class propertyType,
     IntrospectionConfiguration configuration) {
     if (attributes.contains(propertyName))
        return SimpleTypeMapper.Binding.ATTRIBUTE;
     if (elements.contains(propertyName))
        return SimpleTypeMapper.Binding.ELEMENT;
     return
        configuration.isAttributesForPrimitives() ?
        SimpleTypeMapper.Binding.ATTRIBUTE :
        SimpleTypeMapper.Binding.ELEMENT;
   }
}

It can be used like this:
   BetwixtTypeMapper btm = new BetwixtTypeMapper();
   btm.registerAsAttribute("id");
   ic.setSimpleTypeMapper(btm);
   XMLIntrospector xi = beanReader.getXMLIntrospector();
   xi.getConfiguration().setSimpleTypeMapper(btm);

If you like you can add this typemapper as an example to the betwixt 
distribution.

Sebastian

robert burrell donkin wrote:

> hey Sebastian
> 
> i have some good news and some less good news:
> 
> the good news is that i've added a pluggable strategy for binding simple 
> types (primitives at the moment). i hope that you should be able to 
> subclass the new SimpleTypeMapper class with something a litte like:
> 
>     /** Implementation binds strings to elements but everything else to 
> attributes */
>     class StringsAsElementsSimpleTypeMapper extends SimpleTypeMapper {
> 
>         /**
>          * Binds strings to elements but everything else to attributes
>          */
>         public Binding bind(
>                             String propertyName,
>                             Class propertyType,
>                             IntrospectionConfiguration configuration) {
>             if (String.class.equals(propertyType)) {
>                 return SimpleTypeMapper.Binding.ELEMENT;
>             }
>             return SimpleTypeMapper.Binding.ATTRIBUTE;
>         }
> 
>     }
> 
> the less good news is that i needed to add it on the refactoring branch 
> (since the code involved on CVS HEAD has been refactored on the branch). 
> so you'll need to grab the REFACTORING-BRANCH_2004-01-13 version of the 
> source from cvs and build it yourself (probably using 
> http://maven.apache.org).
> 
> i may get round to adding some documentation on it but again this will 
> be on the branch.
> 
> - robert


---------------------------------------------------------------------
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