[digester] create parent from child node value

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

[digester] create parent from child node value

Valerio
Hello,
 say I have this simple xml:
<a>
    <b>testB</b>
</a>

for any element 'a' a new object of type A must be istantiated, passing in
it's 1-param constructor the value testB.
Is this possible?

thanks,
 valerio

--
To Iterate is Human, to Recurse, Divine
James O. Coplien, Bell Labs
Reply | Threaded
Open this post in threaded view
|

RE: [digester] create parent from child node value

Marco Mistroni
Hello,
        I haven't used digester since some time (approx 1 year) , but I am
not 100% sure
That you can create Objects by passing parameters...

At least in all examples that I have seen, all JB that were going to be
Populated via Digester from XML had an empty constructor

Will it be an option to use an empty constructor and call a  setXX method
On your objects?

Regards
        marco

-----Original Message-----
From: Valerio Schiavoni [mailto:[hidden email]]
Sent: 20 December 2005 16:39
To: [hidden email]
Subject: [digester] create parent from child node value

Hello,
 say I have this simple xml:
<a>
    <b>testB</b>
</a>

for any element 'a' a new object of type A must be istantiated, passing in
it's 1-param constructor the value testB.
Is this possible?

thanks,
 valerio

--
To Iterate is Human, to Recurse, Divine
James O. Coplien, Bell Labs


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [digester] create parent from child node value

Valerio
Hello Marco,
i've already considered that option, but it looks quite poor design to
provide so many setXXX methods on objects that will be initialized once and
forever.
Further, there are no restrictions to call those set methods after the
initialization of the object (that is, after the 0-param constructor is
used, either via a addObjectCreate or using addFactoryCreate), apart from
self-disciplined programmers.

What I mean is: how can I istantiate 'immutable' objects  that requires
values in their constructors ? None of the examples I found illustrate this
situation, so maybe this is voluntarly not supported by api designers. if
so, it'be interesting to understand why.

thanks,
valerio



2005/12/20, Marco Mistroni <[hidden email]>:

>
> Hello,
>         I haven't used digester since some time (approx 1 year) , but I am
> not 100% sure
> That you can create Objects by passing parameters...
>
> At least in all examples that I have seen, all JB that were going to be
> Populated via Digester from XML had an empty constructor
>
> Will it be an option to use an empty constructor and call a  setXX method
> On your objects?
>
> Regards
>         marco
>
> -----Original Message-----
> From: Valerio Schiavoni [mailto:[hidden email]]
> Sent: 20 December 2005 16:39
> To: [hidden email]
> Subject: [digester] create parent from child node value
>
> Hello,
> say I have this simple xml:
> <a>
>     <b>testB</b>
> </a>
>
> for any element 'a' a new object of type A must be istantiated, passing in
> it's 1-param constructor the value testB.
> Is this possible?
>
> thanks,
> valerio
>
> --
> To Iterate is Human, to Recurse, Divine
> James O. Coplien, Bell Labs
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>


--
To Iterate is Human, to Recurse, Divine
James O. Coplien, Bell Labs
Reply | Threaded
Open this post in threaded view
|

Re: [digester] create parent from child node value

robert burrell donkin
On Tue, 2005-12-20 at 18:10 +0100, Valerio Schiavoni wrote:

> Hello Marco,
> i've already considered that option, but it looks quite poor design to
> provide so many setXXX methods on objects that will be initialized once and
> forever.
> Further, there are no restrictions to call those set methods after the
> initialization of the object (that is, after the 0-param constructor is
> used, either via a addObjectCreate or using addFactoryCreate), apart from
> self-disciplined programmers.
>
> What I mean is: how can I istantiate 'immutable' objects  that requires
> values in their constructors ? None of the examples I found illustrate this
> situation, so maybe this is voluntarly not supported by api designers. if
> so, it'be interesting to understand why.

the digester api is small and simple: digester fires rules when patterns
are matched in the xml being parsed.

digester ships with a number of standard rules for common use cases.
these have been accumulated over the years. the presence or absence of
rules from this standard set has more to do with what rules developers
have created and contributed to apache than any great design.

digester can do what you want but AFAIK there is no standard rule
suitable for your exact problem (if anyone knows of one, please jump
in). FactoryCreateRule can be used when the required initialization
parameters are contained in attriutes.

you can easily solve your particular problem by creating a custom rule.
if you feel like contributing a more general solution that can be
included in the standard rules, let me know and i'll outline a design.

- robert


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [digester] create parent from child node value

Valerio
Hello Robert, thanks for your answer,

digester can do what you want but AFAIK there is no standard rule
> suitable for your exact problem (if anyone knows of one, please jump
> in).


I looked for materials about writing custom rules: do you have some links
about the topic so that I can think about a solution, and later contribute ?

I'd be glad to put my half cent into digester.

FactoryCreateRule can be used when the required initialization
> parameters are contained in attriutes.


yes, for attributes i have found that, which forced me to think about how
proper my xml was (and indeed it was... ).

let me know and i'll outline a design.


As above, i'll  try to do my best.

cheers,
valerio


--
To Iterate is Human, to Recurse, Divine
James O. Coplien, Bell Labs
Reply | Threaded
Open this post in threaded view
|

Re: [digester] create parent from child node value

Simon Kitching
In reply to this post by robert burrell donkin
On Wed, 2005-12-21 at 09:32 +0000, robert burrell donkin wrote:

> On Tue, 2005-12-20 at 18:10 +0100, Valerio Schiavoni wrote:
> >
> > What I mean is: how can I istantiate 'immutable' objects  that requires
> > values in their constructors ? None of the examples I found illustrate this
> > situation, so maybe this is voluntarly not supported by api designers. if
> > so, it'be interesting to understand why.
>
> digester can do what you want but AFAIK there is no standard rule
> suitable for your exact problem (if anyone knows of one, please jump
> in). FactoryCreateRule can be used when the required initialization
> parameters are contained in attriutes.
>
> you can easily solve your particular problem by creating a custom rule.
> if you feel like contributing a more general solution that can be
> included in the standard rules, let me know and i'll outline a design.

Unfortunately I suspect this is not at all easy to do with Digester.
It's an issue that's been in the back of my mind for ages, and I've got
no clean solution for it.

As Robert says, creating components from the attributes is easy -
FactoryCreateRule does this, or a custom rule is easy to write.

Creating a new instance based upon nested tags is not trivial though.
It *might* be possible but you would be breaking new ground; I'm not
aware of anyone having implemented this with Digester yet.

Maybe something like CallMethodRule could be used, but where it invokes
java.lang.Constructor.newInstance(args) then pushes the object onto the
stack. Tricky, though, as the object needs to remain on the stack long
enough for something like a SetNextRule to operate on it, but must be
removed from the stack before the matching element ends in order for
correct object stack behaviour to occur. The prototype CallMethodRule I
implemented which fired as soon as all parameters were available might
help there; the rule could push a dummy object on begin which is
replaced when all params are available, and the rule's end method could
then pop it off. However that CallMethodRule variant has not yet made it
into trunk. A cooperating pair of rules could do the same job: a rule
that just pushes a dummy object on begin and removes it on end, then a
separate rule that creates the desired object and *replaces* the top
object on the stack with it. That's rather ugly though, both in
implementation and in use.

Combining the construction rule with a SetNextRule would be much easier
to implement: in the end method the rule can create the object using
param objects stored by CallParamRule instances, and immediately invoke
the SetNextRule-like functionality to add the object to a parent without
ever needing to put the created object on the digester object stack. I
think that solves the problems listed above, but at the cost of a
significant loss of flexibility.

Regards,

Simon



---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]