You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Wendy Smoak <ja...@wendysmoak.com> on 2005/01/18 04:49:34 UTC

[digester] Populating a LazyDynaBean with Digester

Since Niall confirmed that you can use a LazyDynaBean with JSTL by 
retrieving the underlying map with
>  ${myLazyBean.map.propName}

I'm now trying to populate a LazyDynaBean with Digester.  I'm okay so far 
with simple String properties:

         digester.addObjectCreate("download", LazyDynaBean.class);

         digester.addCallMethod("download/person/preferredName", "set", 2);
         digester.addObjectParam("download/person/preferredName", 0, 
"preferredName");
         digester.addCallParam("download/person/preferredName", 1);

But what do I do with indexed properties where the call to 
LazyDynaBean.set(name,index,value) requires an integer index?  (A person may 
have more than one degree.)

         digester.addCallMethod("download/person/degree", "set", 3);
         digester.addObjectParam("download/person/degree", 0, "degrees");
         //digester.addObjectParam("download/person/degree", 1, 
???integer??? );
         digester.addCallParam("download/person/degree", 2);

Is it possible to set indexed properties with Digester?

A 'normal' bean might have an 'addWhatever' method, but I don't see anything 
helpful in the LazyDynaBean API docs.

Thanks,
Wendy Smoak



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


Re: [digester] Populating a LazyDynaBean with Digester

Posted by Wendy Smoak <ja...@wendysmoak.com>.
From: "Wendy Smoak" <ja...@wendysmoak.com>
> But what do I do with indexed properties where the call to 
> LazyDynaBean.set(name,index,value) requires an integer index?  (A person 
> may have more than one degree.)
>
>         digester.addCallMethod("download/person/degree", "set", 3);
>         digester.addObjectParam("download/person/degree", 0, "degrees");
>         //digester.addObjectParam("download/person/degree", 1, 
> ???integer??? );
>         digester.addCallParam("download/person/degree", 2);
>
> Is it possible to set indexed properties with Digester?

FTR, I solved this one with (using person/address/lines instead of 
download/person/degree):

   digester.addObjectCreate("person", LazyDynaBean.class);
   digester.addObjectCreate("person/address", ArrayList.class);
   digester.addCallMethod("person/address/line", "add", 1);
   digester.addCallParam("person/address/line", 0);
   Rule rule = new CallMethodRule(1, "set", 2);
   digester.addRule("resolution/person/address", rule);
   digester.addObjectParam("resolution/person/address", 0, "address");
   digester.addCallParam("resolution/person/address", 1, true);

This makes a LazyDynaBean for <person> and then makes an ArrayList for 
<address>.  The trick is getting it to call
     set( "address", list);
on the LazyDynaBean which is 'underneath' the ArrayList on the stack.  (I 
suppose this is not "officially" an indexed property since it's an ArrayList 
and not an Object[], but it works for me.)

The full example with more comments is here:
http://wiki.wendysmoak.com/cgi-bin/wiki.pl?DigesterLazyDynaBean

-- 
Wendy Smoak 



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


Re: [digester] Populating a LazyDynaBean with Digester

Posted by robert burrell donkin <ro...@blueyonder.co.uk>.
On 18 Jan 2005, at 09:44, Simon Kitching wrote:

> On Mon, 2005-01-17 at 20:49 -0700, Wendy Smoak wrote:
>> Since Niall confirmed that you can use a LazyDynaBean with JSTL by
>> retrieving the underlying map with
>>>  ${myLazyBean.map.propName}
>>
>> I'm now trying to populate a LazyDynaBean with Digester.  I'm okay so 
>> far
>> with simple String properties:
>>
>>          digester.addObjectCreate("download", LazyDynaBean.class);
>>
>>          digester.addCallMethod("download/person/preferredName", 
>> "set", 2);
>>          digester.addObjectParam("download/person/preferredName", 0,
>> "preferredName");
>>          digester.addCallParam("download/person/preferredName", 1);
>>
>> But what do I do with indexed properties where the call to
>> LazyDynaBean.set(name,index,value) requires an integer index?  (A 
>> person may
>> have more than one degree.)
>>
>>          digester.addCallMethod("download/person/degree", "set", 3);
>>          digester.addObjectParam("download/person/degree", 0, 
>> "degrees");
>>          //digester.addObjectParam("download/person/degree", 1,
>> ???integer??? );
>>          digester.addCallParam("download/person/degree", 2);
>>
>> Is it possible to set indexed properties with Digester?
>>
>> A 'normal' bean might have an 'addWhatever' method, but I don't see 
>> anything
>> helpful in the LazyDynaBean API docs.
>>
>
> I suspect the easiest solution is to create some custom Rules that are
> LazyDynaBean-aware.
>
> Don't be afraid of creating custom rules for any specific purpose if 
> you
> need them; they aren't hard to write. And Digester is specifically
> designed to allow them; the bundled rules are just a "starter".

+1

a couple of thoughts:

i do wonder whether providing some level of support with a bean query 
language might be a useful extension. might be nice to be able to use 
not just beanutils but something like JEXL to access methods deep in 
the object graph from the top object.

SetProperty already supports dynabeans. it doesn't seem unreasonable to 
add addition support for new dynabean tricks providing that it can 
coded in a reasonable way. i'd probably support a patch (though i'm 
very unlikely to create one myself).

- robert


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


Re: [digester] Populating a LazyDynaBean with Digester

Posted by Simon Kitching <sk...@apache.org>.
On Mon, 2005-01-17 at 20:49 -0700, Wendy Smoak wrote:
> Since Niall confirmed that you can use a LazyDynaBean with JSTL by 
> retrieving the underlying map with
> >  ${myLazyBean.map.propName}
> 
> I'm now trying to populate a LazyDynaBean with Digester.  I'm okay so far 
> with simple String properties:
> 
>          digester.addObjectCreate("download", LazyDynaBean.class);
> 
>          digester.addCallMethod("download/person/preferredName", "set", 2);
>          digester.addObjectParam("download/person/preferredName", 0, 
> "preferredName");
>          digester.addCallParam("download/person/preferredName", 1);
> 
> But what do I do with indexed properties where the call to 
> LazyDynaBean.set(name,index,value) requires an integer index?  (A person may 
> have more than one degree.)
> 
>          digester.addCallMethod("download/person/degree", "set", 3);
>          digester.addObjectParam("download/person/degree", 0, "degrees");
>          //digester.addObjectParam("download/person/degree", 1, 
> ???integer??? );
>          digester.addCallParam("download/person/degree", 2);
> 
> Is it possible to set indexed properties with Digester?
> 
> A 'normal' bean might have an 'addWhatever' method, but I don't see anything 
> helpful in the LazyDynaBean API docs.
> 

I suspect the easiest solution is to create some custom Rules that are
LazyDynaBean-aware.

Don't be afraid of creating custom rules for any specific purpose if you
need them; they aren't hard to write. And Digester is specifically
designed to allow them; the bundled rules are just a "starter".

Regards,

Simon


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


Re: [digester] Populating a LazyDynaBean with Digester

Posted by Wendy Smoak <ja...@wendysmoak.com>.
From: "Hubert Rabago" <hr...@gmail.com>
> Well, just to continue with this thought...
> I remembered you can also do
>           digester.addObjectCreate("download", LazyDynaBean.class);
>           digester.addSetNestedProperties("download/person",
>               new String[] {"degree"},
>               new String[] {});
>
> This tells Digester to ignore the degree element.  I've used this rule
> like this.  I used another rule for the element I let
> SetNestedProperties ignore.  For the rest of the props, I didn't have
> to call their setters one by one.

Ahhhh... so that's how that works.  I thought that if I used the methods
that take arrays, I would have to define *all* of the properties, not just
the _one_ I wanted to override/ignore.  I don't know where I got that idea,
but there you go.  Thanks again!

I'm ignoring the indexed property problem [and the write a custom rule
solution] for the moment-- mapped works just as well for me since I always
have a unique key for the whatever-it-is coming out of the database.

-- 
Wendy Smoak


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


Re: [digester] Populating a LazyDynaBean with Digester

Posted by Hubert Rabago <hr...@gmail.com>.
Well, just to continue with this thought...

I remembered you can also do

          digester.addObjectCreate("download", LazyDynaBean.class);
          digester.addSetNestedProperties("download/person",
              new String[] {"degree"},
              new String[] {});

This tells Digester to ignore the degree element.  I've used this rule
like this.  I used another rule for the element I let
SetNestedProperties ignore.  For the rest of the props, I didn't have
to call their setters one by one.

As for your indexed props, well, I'll +1 the suggestions to look into
a custom rule.

Hubert

On Tue, 18 Jan 2005 22:45:30 -0700, Wendy Smoak <ja...@wendysmoak.com> wrote:
> From: "Hubert Rabago" <hr...@gmail.com>
> > Not an answer to the question you were asking, but looking at your
> > code, I thought I'd share that
> >
> >>          digester.addObjectCreate("download", LazyDynaBean.class);
> >>          digester.addSetNestedProperties("download/person");
> >
> >   works, and adds a preferredName property (and value) to your
> > lazydynabean.
> >
> > Of course, given your case (where you also indexed props at
> > download/person/degree), I'm not sure if your overall result would be
> > what you want.
> 
> Thanks for the tip-- it does work, but it's a little overzealous.  Out of
> this example xml
>     <download>
>        <person key="0452360">
>           <preferredName>Ms. Amy N. Smith</preferredName>
>           <degree key="352">BS Computer Science</degree>
>           <degree key="253">MBA Business Administration</degree>
>        </person>
>     </download>
> 
>  I end up with a 'degree' property set as well as my 'degrees' mapped [was
> indexed] property that I handle separately with
>          digester.addCallMethod("download/person/degree", "set", 3);
>          digester.addObjectParam("download/person/degree", 0, "degrees");
>          digester.addCallParam("download/person/degree", 1, "key");
>          digester.addCallParam("download/person/degree", 2);
> 
> Not _quite_ there yet.  This wasn't a problem when I was populating my own
> javabean, I simply told SetNestedProperties to allowUnknownChildElements,
> and since there _was_ no 'setDegree()' method, nothing bad happened.  Not so
> here, it invents properties I didn't intend.
> 
> I need to specify what properties my dynabean has up front and not let new
> ones be created... obviously I need something other than LazyDynaBean, maybe
> BasicDynaClass would work better for this?
> 
> Thanks,
> --
> Wendy Smoak
>

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


Re: [digester] Populating a LazyDynaBean with Digester

Posted by Wendy Smoak <ja...@wendysmoak.com>.
From: "Hubert Rabago" <hr...@gmail.com>
> Not an answer to the question you were asking, but looking at your
> code, I thought I'd share that
>
>>          digester.addObjectCreate("download", LazyDynaBean.class);
>>          digester.addSetNestedProperties("download/person");
>
>   works, and adds a preferredName property (and value) to your 
> lazydynabean.
>
> Of course, given your case (where you also indexed props at
> download/person/degree), I'm not sure if your overall result would be
> what you want.

Thanks for the tip-- it does work, but it's a little overzealous.  Out of 
this example xml
    <download>
       <person key="0452360">
          <preferredName>Ms. Amy N. Smith</preferredName>
          <degree key="352">BS Computer Science</degree>
          <degree key="253">MBA Business Administration</degree>
       </person>
    </download>

 I end up with a 'degree' property set as well as my 'degrees' mapped [was 
indexed] property that I handle separately with
         digester.addCallMethod("download/person/degree", "set", 3);
         digester.addObjectParam("download/person/degree", 0, "degrees");
         digester.addCallParam("download/person/degree", 1, "key");
         digester.addCallParam("download/person/degree", 2);

Not _quite_ there yet.  This wasn't a problem when I was populating my own 
javabean, I simply told SetNestedProperties to allowUnknownChildElements, 
and since there _was_ no 'setDegree()' method, nothing bad happened.  Not so 
here, it invents properties I didn't intend.

I need to specify what properties my dynabean has up front and not let new 
ones be created... obviously I need something other than LazyDynaBean, maybe 
BasicDynaClass would work better for this?

Thanks,
-- 
Wendy Smoak 



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


Re: [digester] Populating a LazyDynaBean with Digester

Posted by Hubert Rabago <hr...@gmail.com>.
Not an answer to the question you were asking, but looking at your
code, I thought I'd share that

>          digester.addObjectCreate("download", LazyDynaBean.class);
>          digester.addSetNestedProperties("download/person");

   works, and adds a preferredName property (and value) to your lazydynabean.

Of course, given your case (where you also indexed props at
download/person/degree), I'm not sure if your overall result would be
what you want.

Oh well.

Good luck.  Hope you share the answer to your question once you find it.

On Mon, 17 Jan 2005 20:49:34 -0700, Wendy Smoak <ja...@wendysmoak.com> wrote:
> Since Niall confirmed that you can use a LazyDynaBean with JSTL by
> retrieving the underlying map with
> >  ${myLazyBean.map.propName}
> 
> I'm now trying to populate a LazyDynaBean with Digester.  I'm okay so far
> with simple String properties:
> 
>          digester.addObjectCreate("download", LazyDynaBean.class);
> 
>          digester.addCallMethod("download/person/preferredName", "set", 2);
>          digester.addObjectParam("download/person/preferredName", 0,
> "preferredName");
>          digester.addCallParam("download/person/preferredName", 1);
> 
> But what do I do with indexed properties where the call to
> LazyDynaBean.set(name,index,value) requires an integer index?  (A person may
> have more than one degree.)
> 
>          digester.addCallMethod("download/person/degree", "set", 3);
>          digester.addObjectParam("download/person/degree", 0, "degrees");
>          //digester.addObjectParam("download/person/degree", 1,
> ???integer??? );
>          digester.addCallParam("download/person/degree", 2);
> 
> Is it possible to set indexed properties with Digester?
> 
> A 'normal' bean might have an 'addWhatever' method, but I don't see anything
> helpful in the LazyDynaBean API docs.
> 
> Thanks,
> Wendy Smoak
> 
> ---------------------------------------------------------------------
> 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