commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Ken Egervari" <>
Subject RE: [Digester] Parent Reference Help
Date Tue, 31 Aug 2004 00:35:13 GMT
I believe the problem I'm referring to is that while it does set the parent,
the reference is not available until after the properties have been set in
full.  Thus, if I have a property called "setValidatorId" that needs to
lookup a parent object (let's say a Validator object in this case, but it
doesn't really matter) in the root of the object graph, the child object
won't be able to do this because he'll get a NullPointerException when
trying to do "parent.whatever()".  The only way that I can see around this
is to call the parent's method in the setParent() or addChild() method, thus
forcing the object to temporarily store "validatorId" until setParent() or
addChild() is actually called.  

For instance, in my example below, a SelectOne object would have to store
the validatorId temporarily so that it can do something like this when
setParent() is called in Page:

public void setParent( Page page ) {
	this.validator = page.getContext().getValidator( this.validatorId );

Does that help clarify my problem?  I'd like to avoid the use of validatorId
and get at this validator object immediately when setValidatorId() is called
rather than later.  Perhaps something like this:

Public void setValidatorId( String validatorId ) {
	this.validator = parent.getContext().getValidator( validatorId );

The code above avoids the use of temporary variable storage, but I can't
seem to get it to work.

Best regards and many thanks,
Ken Egervari

-----Original Message-----
From: Craig McClanahan [] 
Sent: Monday, August 30, 2004 8:07 PM
To: Jakarta Commons Users List
Subject: Re: [Digester] Parent Reference Help

The simplest way to address this need is to use the object stack that
Digester creates for you.  Every time you execute an Object Create
Rule, for example, the new instance gets pushed on to the stack, so
that Digester rules for nested elements can get to it easily:

    Page page = (Page) digester.peek();

There's examples of this in the "Package Documentation" part of the
Digester javadocs:

as well as in the source code to rules like SetParentRule and
SetNextRule, which rely on exactly this capability in order to perform
their functions.


On Mon, 30 Aug 2004 20:01:02 -0400, Ken Egervari <> wrote:
> Hi,
> Is there any way to reference a parent object when parsing an XML document
> using the commons-digester component?  For example, let's assume that
> is an addObjectCreate() rule that creates a Page object for the <page> tag
> and a SelectOne object for the <selectone> tag.  If I have the following
> (shown below), is there any way the reference of my Page object can be
> passed in the SelectOne object before the SelectOne's setters are called
> using a typical addSetProperties() rule?  I'm having trouble obtaining a
> reference since using addSetTop() is called after the setters are called.
> This is rather inconvenient since some setters (in SelectOne for instance)
> will often need access to the parent object (the Page object in this
> instance).  In other instances, I need to traverse up the graph of objects
> to the root.  Since the parent is not defined, this is impossible.
> If I cannot access the parent as the setters are being called, I'm
> forced to put this logic in the parent's addChild method instead where all
> the attributes have been mapped to the object's properties already, but
> makes the object less convenient outside the digester.
> <page index="2" formView="page.three.form">
>       <selectone name="company.language" referenceData="languages"
>             validatorId="singlereference"
>             errorMessage="There are no languages in the database." />
> </page>
> I'm not flooding the message with source code because I don't think it's
> required.  The specifics of the example are unimportant and I'm more
> interested in a general solution.
> Thanks for helping me out and best regards,
> Ken Egervari

To unsubscribe, e-mail:
For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message