Oops sorry, I wasn't finished with my e-mail yet...damn keyboard
shortcuts...
As I was saying the BarHandler would look something like the following:
public class BarHandler
extends AbstractFieldHandler
{
/**
* Creates a new BarHandler for the Foo class
*/
public BarHandler() {
super();
}
public Object getValue( Object object )
throws IllegalStateException
{
Foo foo = (Foo)object;
Bar bar = new Bar();
bar.setLeft(foo.getLeft());
bar.setRight(foo.getRight());
return bar;
}
public void setValue( Object object, Object value )
throws IllegalStateException, IllegalArgumentException
{
Foo foo = (Foo)object;
Bar bar = (Bar)value;
foo.setLeft(bar.getLeft());
foo.setRight(bar.getRight());
}
So the handler itself would deal with the Wrapper class, you wouldn't
need to change your object model at all.
I know it's a bit more work than doing location="bar[1]", but at least
it's a workaround for now.
Thanks,
--Keith
Keith Visco wrote:
>
> Hi Dan,
>
> You can't do that with the location attribute yet. I've got code that I
> started a couple weeks back that is for precisely this problem, but I
> wasn't finished with it, so it didn't make the 0.9.5 release. It will
> however be part of the 0.9.5.1 release.
>
> The only real issue I'm facing right now is how to specify the behavior
> in the mapping file.
>
> I was thinking about using predicates in the location as such:
>
> location="bar[1]" would indicate that you want the result merged with
> the first bar element. I'm not 100% happy with that, but that's my
> current thinking.
>
> A workaround is to use a custom FieldHandler with a wrapper element.
> Don't worry this doesn't require any changes to your existing object
> model.
>
> The FieldHandler will do the wrapping and unwrapping of the bar element.
>
> So your mapping would look like:
>
> <class name="Foo">
> <map-to xml="foo"/>
> <field name "bar" type="Bar" handler="BarHandler"/>
> </class>
>
> <class name="Bar">
> <map-to xml="bar"/>
> <field name "barLeft" type="integer">
> <bind-xml name="left" node="element"/>
> </field>
> <field name "barLeft" type="integer">
> <bind-xml name="right" node="element"/>
> </field>
> </class>
>
> And BarHandler would look something like the following:
>
> --Keith
>
> > Dan Marcus wrote:
> >
> > Hi-
> >
> > I've just started using Castor and am excited about its potential, but
> > I've run into a problem. I would like to map two fields to a single
> > wrapper element. I tried using the 'location' attribute of bind-xml,
> > but it yielded two wrapper elements. The example below, I hope,
> > will make this clearer. Is it possible to nest multiple elements
> > within a single wrapper element?
> >
> > Thanks,
> > Dan Marcus
> >
> > Given a bean like this:
> > public class Foo {
> > private int barLeft;
> > private int barRight;
> >
> > public int getBarLeft(){
> > return barLeft;
> > }
> >
> > public int getBarRight(){
> > return barRight;
> > }
> > }
> >
> > I would like to create xml like this:
> > <foo>
> > <bar>
> > <left/> <!-- Note left and right are both within a
> > single bar-->
> > <right/>
> > </bar>
> > </foo>
> >
> > Unfortunately, using the mapping below, it looks like this:
> > <foo>
> > <!-- Note the 2 bar elements! -->
> > <bar>
> > <left>0</left>
> > <bar>
> > <bar>
> > <right>5</right>
> > <bar>
> > </foo>
> >
> > Here's the mapping:
> > <class name="Foo">
> > <map-to xml="foo"/>
> > <field name "barLeft" type="integer">
> > <bind-xml name="left" location="bar" node="element"/>
> > </field>
> > <field name "barLeft" type="integer">
> > <bind-xml name="right" location="bar" node="element"/>
> > </field>
> > </class>
> >
-----------------------------------------------------------
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-dev