commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From robert burrell donkin <>
Subject Re: [digester] help with multi-argument constructor
Date Mon, 26 May 2003 20:31:00 GMT

On Monday, May 26, 2003, at 08:12 PM, Christopher Bare wrote:

> Thanks Simon and Robert for the insight into how these
> rules work. I understand now why my idea is somewhat
> at odds with the way digester is designed.
> You are correct that I want to use a setNext rule
> after creating the User 0bject to add each user to an
> aggregate object. Sorta like this:
>     digester.addObjectCreate( "users", Users.class );
>     digester.addCallConstructor( "users/user",
> User.class, 2 );
>     digester.addCallParam( "users/user/id", 0 );
>     digester.addCallParam( "users/user/name", 1 );
>     digester.addSetNext( "users/user", "addUser" );
> If I understand correctly, the CallParam pushes values
> onto the stack and the CallMethod rule pops them off
> and finally invokes the method in the
> end(java.lang.String namespace, java.lang.String name)
> method of the rule. I was thinking that the
> CallConstructor rule could work the same way, and that
> the newly created object could be pushed onto the
> stack in the end(...) method. Then the SetNext rule
> could come along and pop do its thing.
> The issue with this, I guess, is that you need to
> insure that the the constructor is called and the
> object pushed onto the stack before SetNext's end(...)
> is invoked. Or, is there more to it than that?

you need to ensure that the constructor is called and the object pushed 
onto the stack before SetNext's end method is called and then popped off 
the stack after it's called.

> If the
> Constructor was invoked in the body(...) of my
> hypothetical CallConstructorRule, that would
> definitely push the new object first. Would body(...)
> definitely be called -after- the params were pushed
> onto the stack?


what happens is that when the digester endElement implementation is called,
  all the body calls for all the matched rules are called and then all the 
end calls. endElement for the parent has to be called (by the SAX parser) 
after each endElement is called for each child element - therefore any 
params which are going to be pushed onto the stack will have been. (the 
only exception is where you're setting a parameter from the body text of 
the parent - in this case, you'd need to be careful about the ordering.)

- robert

View raw message