You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Sebastian <no...@gmx.net> on 2004/01/31 13:55:50 UTC

Betwixt: Setting up BeanWriter to render a bean property as attribute rather than element

Hello,
is it possible to configure the BeanWriter to always render a specific 
bean property as attribute rather than an element. In my case it's the 
property "id". All other properties shall still be rendered as xml elements.

I know that I can provide a .betwixt file for every bean and specify it 
in there, but I don't want to set it individually for every bean.

Thanks in advance,
Sebastian

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


Re: Betwixt: Setting up BeanWriter to render a bean property as attribute rather than element

Posted by Sebastian <no...@gmx.net>.
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


Re: Betwixt: Setting up BeanWriter to render a bean property as attribute rather than element

Posted by Sebastian <no...@gmx.net>.
robert burrell donkin wrote:
> the reading code has been completed rewritten (it's still a bit of a 
> mess at the moment since there's more refactoring to come) but the 
> behaviour shouldn't have changed. i've added an additional test for 
> reading collectives based on the information you gave me but it doesn't 
> fail. if you'd be willing to donate a unit test to the ASF that 
> demonstrates the problem. i'm confident that i'd be able to quickly find 
> a way to resolve it.
> 
> - robert

Hi Robert,
I just sent you an example junit test showing the problem.

Sebastian


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


Re: Betwixt: Setting up BeanWriter to render a bean property as attribute rather than element

Posted by robert burrell donkin <ro...@blueyonder.co.uk>.
On 10 Feb 2004, at 16:14, Sebastian wrote:

> Sebastian wrote:
>> I just recognized that changing
>>   beanReader.registerBeanClass(Instructor.class);
>>   beanReader.registerBeanClass(Instructors.class);
>> to
>>   beanReader.registerBeanClass(Instructors.class);
>>   beanReader.registerBeanClass(Instructor.class);
>> gets betwixt working as expected. So currently the order in which the 
>> beans are registered seems to be important.
>> Sebastian
>
> Robert,
> using the lastest nightly build of commons-digester solves the issue.

cool :)

- robert


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


Re: Betwixt: Setting up BeanWriter to render a bean property as attribute rather than element

Posted by Sebastian <no...@gmx.net>.
Sebastian wrote:
> I just recognized that changing
> 
>   beanReader.registerBeanClass(Instructor.class);
>   beanReader.registerBeanClass(Instructors.class);
> 
> to
> 
>   beanReader.registerBeanClass(Instructors.class);
>   beanReader.registerBeanClass(Instructor.class);
> 
> gets betwixt working as expected. So currently the order in which the 
> beans are registered seems to be important.
> 
> Sebastian

Robert,
using the lastest nightly build of commons-digester solves the issue.

Thank you for the fix!

Sebastian


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


Re: Betwixt: Setting up BeanWriter to render a bean property as attribute rather than element

Posted by Sebastian <no...@gmx.net>.
Sebastian wrote:
> I just recognized that it works with Categories and Category, but it 
> doesn't work with Instructors and Instructor, even if they have exactly 
> the same properties.
> When I change Instructor to Instructory and Instructors to Instructories 
> it works.
> Can it be that there is a bug in the way how betwixt determines 
> singular/plural?
> 
> Sebastian

I just recognized that changing

   beanReader.registerBeanClass(Instructor.class);
   beanReader.registerBeanClass(Instructors.class);

to

   beanReader.registerBeanClass(Instructors.class);
   beanReader.registerBeanClass(Instructor.class);

gets betwixt working as expected. So currently the order in which the 
beans are registered seems to be important.

Sebastian


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


Re: Betwixt: Setting up BeanWriter to render a bean property as attribute rather than element

Posted by Sebastian <no...@gmx.net>.
robert burrell donkin wrote:

> On 7 Feb 2004, at 14:37, Sebastian wrote:
> 
> hi sebastian
> 
>> Hi Robert,
>> I managed to download the refactoring branch and build a distribution 
>> using maven. The I created a custom simplytypemapper and launched a 
>> junit test on my project. Then I discovered that nearly nothing works 
>> anymore. I removed my simpletypemapper and tried using the .betwixt 
>> files again. the junit test still failed. then I recognized, that 
>> collections are not unserialized as they used to do before, so when I 
>> have an xml like this
>> <categories>
>>    <category>...</cateogry>
>>    <category>...</cateogry>
>>    <category>...</cateogry>
>> </categories>
>> I expected that calling beanReader.parse(new StringReader(myXml)) 
>> returns a Categories object as it did before (using betwixt alpha 1), 
>> but for any reason it now returns a Category object.
>> Did anything substantial changed in how betwixt handles collections?
> 
> 
> the reading code has been completed rewritten (it's still a bit of a 
> mess at the moment since there's more refactoring to come) but the 
> behaviour shouldn't have changed. i've added an additional test for 
> reading collectives based on the information you gave me but it doesn't 
> fail. if you'd be willing to donate a unit test to the ASF that 
> demonstrates the problem. i'm confident that i'd be able to quickly find 
> a way to resolve it.
> 
> - robert

I just recognized that it works with Categories and Category, but it 
doesn't work with Instructors and Instructor, even if they have exactly 
the same properties.
When I change Instructor to Instructory and Instructors to Instructories 
it works.
Can it be that there is a bug in the way how betwixt determines 
singular/plural?

Sebastian


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


Re: Betwixt: Setting up BeanWriter to render a bean property as attribute rather than element

Posted by robert burrell donkin <ro...@blueyonder.co.uk>.
On 7 Feb 2004, at 14:37, Sebastian wrote:

hi sebastian

> Hi Robert,
> I managed to download the refactoring branch and build a distribution 
> using maven. The I created a custom simplytypemapper and launched a 
> junit test on my project. Then I discovered that nearly nothing works 
> anymore. I removed my simpletypemapper and tried using the .betwixt 
> files again. the junit test still failed. then I recognized, that 
> collections are not unserialized as they used to do before, so when I 
> have an xml like this
> <categories>
>    <category>...</cateogry>
>    <category>...</cateogry>
>    <category>...</cateogry>
> </categories>
> I expected that calling beanReader.parse(new StringReader(myXml)) 
> returns a Categories object as it did before (using betwixt alpha 1), 
> but for any reason it now returns a Category object.
> Did anything substantial changed in how betwixt handles collections?

the reading code has been completed rewritten (it's still a bit of a 
mess at the moment since there's more refactoring to come) but the 
behaviour shouldn't have changed. i've added an additional test for 
reading collectives based on the information you gave me but it doesn't 
fail. if you'd be willing to donate a unit test to the ASF that 
demonstrates the problem. i'm confident that i'd be able to quickly 
find a way to resolve it.

- robert


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


Re: Betwixt: Setting up BeanWriter to render a bean property as attribute rather than element

Posted by Sebastian <no...@gmx.net>.
robert burrell donkin wrote:
> On 4 Feb 2004, at 15:29, Sebastian wrote:
> 
>> Hi Robert,
> 
> 
> hi sebastian
> 
>> thanks a lot for adding this new Mapper. A dumb question: how can I 
>> access the refactoring branch? I'm using eclipse to access the apache 
>> CVS but can't  see that branch anywhere.
> 
> 
> i'd say that the easiest way would be to update (a checked out version) 
> to the branch (cvs -q up -r REFACTORING-BRANCH_2004-01-13 -d -P in the 
> betwixt folder) outside eclipse (the ASF does not allow history commands 
> and i have a feel this is how eclipse finds out about branches. (you'll 
> need to get hold of a command line client to do this if you don't have 
> one already.)
> 
> i broke with tradition for the betwixt refactoring and used eclipse (i 
> almost always using a simple test editor for the projects i work on) and 
> so i know that betwixt will build fine without it but i'd strongly 
> recommend downloading maven (http://maven.apache.org) so that you can 
> build standard distributions and the documentation.
> 
> - robert
Hi Robert,
I managed to download the refactoring branch and build a distribution 
using maven. The I created a custom simplytypemapper and launched a 
junit test on my project. Then I discovered that nearly nothing works 
anymore. I removed my simpletypemapper and tried using the .betwixt 
files again. the junit test still failed. then I recognized, that 
collections are not unserialized as they used to do before, so when I 
have an xml like this
<categories>
    <category>...</cateogry>
    <category>...</cateogry>
    <category>...</cateogry>
</categories>
I expected that calling beanReader.parse(new StringReader(myXml)) 
returns a Categories object as it did before (using betwixt alpha 1), 
but for any reason it now returns a Category object.
Did anything substantial changed in how betwixt handles collections?

This is how I configured the beanreader:
   BeanReader beanReader = new BeanReader();
   XMLIntrospector xi = beanReader.getXMLIntrospector();
   IntrospectionConfiguration ic = xi.getConfiguration();
   ic.setAttributesForPrimitives(false);
   ic.setElementNameMapper(capitalizeNameMapper);
   ic.setAttributeNameMapper(capitalizeNameMapper);
   ic.setWrapCollectionsInElement(false);
   beanReader.getBindingConfiguration().setMapIDs(false);

   beanReader.registerBeanClass(Category.class);
   beanReader.registerBeanClass(Categories.class);

Thanks in advance,
Sebastian


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


Re: Betwixt: Setting up BeanWriter to render a bean property as attribute rather than element

Posted by robert burrell donkin <ro...@blueyonder.co.uk>.
On 4 Feb 2004, at 15:29, Sebastian wrote:
> Hi Robert,

hi sebastian

> thanks a lot for adding this new Mapper. A dumb question: how can I 
> access the refactoring branch? I'm using eclipse to access the apache 
> CVS but can't  see that branch anywhere.

i'd say that the easiest way would be to update (a checked out version) 
to the branch (cvs -q up -r REFACTORING-BRANCH_2004-01-13 -d -P in the 
betwixt folder) outside eclipse (the ASF does not allow history 
commands and i have a feel this is how eclipse finds out about 
branches. (you'll need to get hold of a command line client to do this 
if you don't have one already.)

i broke with tradition for the betwixt refactoring and used eclipse (i 
almost always using a simple test editor for the projects i work on) 
and so i know that betwixt will build fine without it but i'd strongly 
recommend downloading maven (http://maven.apache.org) so that you can 
build standard distributions and the documentation.

- robert


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


Re: Betwixt: Setting up BeanWriter to render a bean property as attribute rather than element

Posted by Sebastian <no...@gmx.net>.
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

Hi Robert,
thanks a lot for adding this new Mapper. A dumb question: how can I 
access the refactoring branch? I'm using eclipse to access the apache 
CVS but can't  see that branch anywhere.

Sebastian


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


Re: Betwixt: Setting up BeanWriter to render a bean property as attribute rather than element

Posted by robert burrell donkin <ro...@blueyonder.co.uk>.
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

On 1 Feb 2004, at 22:01, Sebastian wrote:

> robert burrell donkin wrote:
>> hi sebastian
>> this feature is (at the moment) missing from betwixt (but is on the 
>> long list of features that betwixt needs before a proper release). it 
>> should be pretty easy to add. anyone want to volunteer to develop it?
>> - robert
>
> Hi Robert,
>
> thanks for your response, at least now I know.
>
> I already had a look at the sourcecode but I'm lacking the right 
> skills to add this feature to betwixt. So hopefully someone else is 
> able and willing to do so.
>
> Sebastian
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>


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


Re: Betwixt: Setting up BeanWriter to render a bean property as attribute rather than element

Posted by Sebastian <no...@gmx.net>.
robert burrell donkin wrote:
> hi sebastian
> 
> this feature is (at the moment) missing from betwixt (but is on the long 
> list of features that betwixt needs before a proper release). it should 
> be pretty easy to add. anyone want to volunteer to develop it?
> 
> - robert
> 

Hi Robert,

thanks for your response, at least now I know.

I already had a look at the sourcecode but I'm lacking the right skills 
to add this feature to betwixt. So hopefully someone else is able and 
willing to do so.

Sebastian


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


Re: Betwixt: Setting up BeanWriter to render a bean property as attribute rather than element

Posted by robert burrell donkin <ro...@blueyonder.co.uk>.
hi sebastian

this feature is (at the moment) missing from betwixt (but is on the 
long list of features that betwixt needs before a proper release). it 
should be pretty easy to add. anyone want to volunteer to develop it?

- robert

On 31 Jan 2004, at 12:55, Sebastian wrote:

> Hello,
> is it possible to configure the BeanWriter to always render a specific 
> bean property as attribute rather than an element. In my case it's the 
> property "id". All other properties shall still be rendered as xml 
> elements.
>
> I know that I can provide a .betwixt file for every bean and specify 
> it in there, but I don't want to set it individually for every bean.
>
> Thanks in advance,
> Sebastian
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>


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