You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Maksimenko Alexander <ma...@bird.cris.net> on 2003/03/14 13:49:08 UTC

[Betwixt] Composite properties

1. How can I define wrapCollectionsInElement  for particular composite property of the bean. Or in general, how can i access ElementDescriptor for particular bean.

2. Suppose i have 
class A
..
Map getFooMap()
void addFoo(B foo)

Can i map A into
<a>
<foo name="name1" value="value1"/>
<foo name="name2" value="value2"/>
<foo name="name3" value="value3"/>
</a>
not in
<a>
<entry>
    <key>name1</key>
    <value>value1</value>
 </entry>
....
</a>

Re: [Betwixt] Composite properties

Posted by robert burrell donkin <ro...@blueyonder.co.uk>.
On Monday, March 17, 2003, at 10:11 AM, Maksimenko Alexander wrote:

<snip>

> Of course it would be nice if I had access to all properties of the 
> Element
> Descriptor in the file .betwixt:
> <element name="foo" property="fooList" wrapCollectionsInElement="false"/>

sorry for being a bit dense - there is actually an easy to do this already.
  when you use a betwixt file, none of the loop elements are wrapped in 
elements. the reason for this is that you can easily add your own wrapping 
element where ever it's needed.

you do something like:

<element name='bar'>
	<element name="foo" property="fooList"/>
</element>

- robert


Re: [Betwixt] Composite properties

Posted by Maksimenko Alexander <ma...@bird.cris.net>.
> > 1. How can I define wrapCollectionsInElement  for particular composite
> > property of the bean. Or in general, how can i access ElementDescriptor
> > for particular bean.
> as for the second question, you can totally replace the betwixt
> introspection system by plugging an custom XMLBeanInfoRegistry. just
> create your own class extending XMLBeanInfo which contains a description
> of you bean. then create a XMLBeanInfoRegistry which contains supplies an
> instance whenever the class is requested.
>
> another item on the to do list is a XMLBeanInfoRegistry which would have a
> protected set of XMLBeanInfo implementations to make this kind of thing
> much easier. i might bring this forward if you plan to go down this route.
>

Thank you for answer. Your suggestions helped me to write the following:
      // Init
      BeanWriter beanWriter = new BeanWriter(outputWriter);
      XMLIntrospector introspector = beanWriter.getXMLIntrospector();
      XMLBeanInfoRegistry registry = introspector.getRegistry();
      // Take ElementDesciptor object
      introspector.introspect(A.class);
      ElementDescriptor fooPropertyDesciptor =
registry.get(A.class).getElementDescriptor().getElementDescriptors()[0];
      fooPropertyDesciptor.setWrapCollectionsInElement(false);
      // Init a object and write it
      A a = new A();
      .......
      beanWriter.write(a);

Of course it would be nice if I had access to all properties of the Element
Descriptor in the file .betwixt:
<element name="foo" property="fooList" wrapCollectionsInElement="false"/>




Re: [Betwixt] Composite properties

Posted by Maksimenko Alexander <ma...@bird.cris.net>.
> > . Suppose i have
> > class A
> > ..
> > Map getFooMap()
> > void addFoo(B foo)
> >
> > Can i map A into
> > <a>
> > <foo name="name1" value="value1"/>
> > <foo name="name2" value="value2"/>
> > <foo name="name3" value="value3"/>
> > </a>
>
> (at the moment) betwixt always outputs map entries using the following
> format:
>
> <entry>
> <key>xxx</key>
>      <value>yyy</value>
> <entry>
> it might be more difficult for object values maps since you'll need
> appropriate object->value conversion. it would make round tripping complex
> since you'd need a string -> object factory. it's not impossible but it'd
> need some thinking about. i'd be interested to hear other people views on

I solve this problem in the following way:
1. I create the following implementation of the Expression
  public class MapValuesExpression implements Expression{
    Expression expression;

    public Object evaluate(Context context){
      Object value = expression.evaluate(context);
      if(value instanceof Map){
        Map map = (Map)value;
        value = map.values().iterator();
      }
      return value;
    }
    .....
  }
2. In the main method
      // Init
      BeanWriter beanWriter = new BeanWriter(outputWriter);
      XMLIntrospector introspector = beanWriter.getXMLIntrospector();
      XMLBeanInfoRegistry registry = introspector.getRegistry();
      // Take ElementDesciptor object
      introspector.introspect(A.class);
      ElementDescriptor fooMapProperty =
registry.get(A.class).getElementDescriptor().getElementDescriptors()[0];
      ElementDescriptor fooProperty =
fooMapProperty.getElementDescriptors()[0];
     fieldProperty.setContextExpression(
                                               new MapValuesExpression(
                                                  new MethodExpression(

A.class.getMethod("getFooMap",new Class[]{})
                                                  )
                                                ) );
      // Init a object and write it
      A a = new A();
      .......
      beanWriter.write(a);





Re: [Betwixt] Composite properties

Posted by robert burrell donkin <ro...@blueyonder.co.uk>.
On Friday, March 14, 2003, at 12:49 PM, Maksimenko Alexander wrote:

> 1. How can I define wrapCollectionsInElement  for particular composite 
> property of the bean. Or in general, how can i access ElementDescriptor 
> for particular bean.

i'm a little unclear about exactly what you're asking here.

are you asking how you can set wrapCollectionsInElement for a particular 
property (rather than globally for every property)?

the answer is - at the moment, there is no way. on the other hand, it is 
on the to do list and i'd be willing to take a stab at an implementation 
if you need it. i'd probably an element attribute whose presence would 
override the writer setting when that element is written.


as for the second question, you can totally replace the betwixt 
introspection system by plugging an custom XMLBeanInfoRegistry. just 
create your own class extending XMLBeanInfo which contains a description 
of you bean. then create a XMLBeanInfoRegistry which contains supplies an 
instance whenever the class is requested.

another item on the to do list is a XMLBeanInfoRegistry which would have a 
protected set of XMLBeanInfo implementations to make this kind of thing 
much easier. i might bring this forward if you plan to go down this route.

> 2. Suppose i have
> class A
> ..
> Map getFooMap()
> void addFoo(B foo)
>
> Can i map A into
> <a>
> <foo name="name1" value="value1"/>
> <foo name="name2" value="value2"/>
> <foo name="name3" value="value3"/>
> </a>
> not in
> <a>
> <entry>
>     <key>name1</key>
>     <value>value1</value>
>  </entry>
> ....
> </a>

(at the moment) betwixt always outputs map entries using the following 
format:

<entry>
	<key>xxx</key>
     <value>yyy</value>
<entry>

where xxx is the betwixt mapping of the key object and yyy the mapping of 
the value object.


i can see that:

<foo name="name1" value="value1"/>
<foo name="name2" value="value2"/>
<foo name="name3" value="value3"/>

would be a common pattern for String valued maps. if this is the case for 
you, then i might be able to come up with a way to quickly add this 
feature to betwixt.

it might be more difficult for object values maps since you'll need 
appropriate object->value conversion. it would make round tripping complex 
since you'd need a string -> object factory. it's not impossible but it'd 
need some thinking about. i'd be interested to hear other people views on 
this.

- robert