You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Brent Hale <bh...@eventronix.com> on 2002/05/16 17:23:21 UTC

I need access to bodyText in Digester or a addSetRoot which can get bodyText

Hi,

In my particular situation I need to call a method on an object which is not
the top on on the stack and still get the body of the XML element being
processed.  It appears to me that the only way to ever gain access to the
body of the XML element being processed is through addCallMethod() passing
zero in as the paramCount.  But that only calls methods on an object on the
top of the stack.

I see a couple possible solutions and I hope someone else may have even
better ones.

1) Add a public String getBodyText() method to the digester.  I can then
push the digester itself on the bottom of the stack and thereby get access
to the XML body that way.

2) Create a new series of addCallMethod()'s which take an "int n" parameter.
If n were equal to 0 then it would behave like it does now.  If n == 1 then
it would execute the method on the next-to-top object on the stack and so on
down the line.  Then you could still pass in a paramCount of zero or
whatever.

3) A variation on #2 above.  Only change is to call the new series of
addCallMethod()'s something different like addCallMethodN() or something so
as to not make so many variations of addCallMethod.

4) Another more drastic alternative would be to have all callbacks accept as
a first parameter the Digester and then still do #1 above.  That would break
a lot of code but make somethings a lot easier like collaborating with other
objects on the stack.  Hey...I'm only dreaming here.

But after all this...there may be another way of doing what I need that I am
just missing.  So any help would be appreciated.

Thanks,
Brent


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: I need access to bodyText in Digester or a addSetRoot which can get bodyText

Posted by Brent Hale <bh...@eventronix.com>.
I went ahead as you suggested and it works great.

After seeing what changes needed to be made may I make another suggestion
that I hope can be added to the main distribution.

If the main distribution went ahead and added this functionality then the
proper way would be for CallMethodRule to extend CallMethodRuleN (the class
I cut-n-pasted together) since it is the more generalized case.

However, and even easier way would be to simply add the following to
CallMethodRule.java:

    /**
     *  Specifies which object on the stack to call
     *  methodName on.  If n == 0 then this rule operates just like
     *  CallMethodRule().  n == 1 calls the methodName on the next-to-top
     *  element and so forth.
     */
    protected int nthStackObject = 0;


    public void setNthStackObject(int nthStackObject) {
        this.nthStackObject = nthStackObject;
    }


and then change the line doing this:

        Object top = digester.peek();

to this

        Object top = digester.peek(nthStackObject);

You might also want to change the variable name of "top" to something else
now to reflect the fact that it may be any object on the stack (I used
"object").

Then it would be possible to add the series of addCallMethodN() calls to
Digester by simply doing this type of thing:


    /**
     * Add an "call method" rule for a method which accepts no arguments.
     *
     * @param pattern Element matching pattern
     * @param methodName Method name to be called
     * @param nthObjectStack Specifies which object on the stack to call
     *  methodName on.  If n == 0 then this rule operates just like
     *  CallMethodRule().  n == 1 calls the methodName on the next-to-top
     *  element and so forth.
     */
    public void addCallMethodN(String pattern, String methodName, int
nthObjectStack) {

	CallMethodRule rule = new CallMethodRule(methodName);
	rule.setNthObjectStack( nthObjectStack );
        addRule(
                pattern,
                rule);

    }


What do you think?  Should this be added to the main distribution?

Brent


-----Original Message-----
From: Craig R. McClanahan [mailto:craigmcc@apache.org]
Sent: Thursday, May 16, 2002 9:59 AM
To: Jakarta Commons Developers List; bhale@eventronix.com
Subject: Re: I need access to bodyText in Digester or a addSetRoot which
can get bodyText


On Thu, 16 May 2002, Brent Hale wrote:

> Date: Thu, 16 May 2002 09:23:21 -0600
> From: Brent Hale <bh...@eventronix.com>
> Reply-To: Jakarta Commons Developers List
<co...@jakarta.apache.org>,
>      bhale@eventronix.com
> To: commons-dev@jakarta.apache.org
> Subject: I need access to bodyText in Digester or a addSetRoot which can
>     get bodyText
>
> Hi,
>
> In my particular situation I need to call a method on an object which is
not
> the top on on the stack and still get the body of the XML element being
> processed.  It appears to me that the only way to ever gain access to the
> body of the XML element being processed is through addCallMethod() passing
> zero in as the paramCount.  But that only calls methods on an object on
the
> top of the stack.
>
> I see a couple possible solutions and I hope someone else may have even
> better ones.
>
> 1) Add a public String getBodyText() method to the digester.  I can then
> push the digester itself on the bottom of the stack and thereby get access
> to the XML body that way.
>
> 2) Create a new series of addCallMethod()'s which take an "int n"
parameter.
> If n were equal to 0 then it would behave like it does now.  If n == 1
then
> it would execute the method on the next-to-top object on the stack and so
on
> down the line.  Then you could still pass in a paramCount of zero or
> whatever.
>
> 3) A variation on #2 above.  Only change is to call the new series of
> addCallMethod()'s something different like addCallMethodN() or something
so
> as to not make so many variations of addCallMethod.
>
> 4) Another more drastic alternative would be to have all callbacks accept
as
> a first parameter the Digester and then still do #1 above.  That would
break
> a lot of code but make somethings a lot easier like collaborating with
other
> objects on the stack.  Hey...I'm only dreaming here.
>
> But after all this...there may be another way of doing what I need that I
am
> just missing.  So any help would be appreciated.
>

#2 is probably the best of these approaches -- it generalizes the concept
of "walk up the stack before calling" the best.  #1 and #4 seem especially
kludgey, because Rule instances have access to the body text of the
current element (via the body() method getting called) already.

In the short term, I'd suggest just writing your own version of
CallMethodRule (either by subclassing or cut-n-paste) and add a
constructor that takes the extra argument.  You can use your custom
implementation by calling Digester.addRule(), even if it is not part of
the standard Digester suite of Rule implementations yet.

> Thanks,
> Brent
>

Craig


--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: I need access to bodyText in Digester or a addSetRoot which can get bodyText

Posted by "Craig R. McClanahan" <cr...@apache.org>.
On Thu, 16 May 2002, Brent Hale wrote:

> Date: Thu, 16 May 2002 09:23:21 -0600
> From: Brent Hale <bh...@eventronix.com>
> Reply-To: Jakarta Commons Developers List <co...@jakarta.apache.org>,
>      bhale@eventronix.com
> To: commons-dev@jakarta.apache.org
> Subject: I need access to bodyText in Digester or a addSetRoot which can
>     get bodyText
>
> Hi,
>
> In my particular situation I need to call a method on an object which is not
> the top on on the stack and still get the body of the XML element being
> processed.  It appears to me that the only way to ever gain access to the
> body of the XML element being processed is through addCallMethod() passing
> zero in as the paramCount.  But that only calls methods on an object on the
> top of the stack.
>
> I see a couple possible solutions and I hope someone else may have even
> better ones.
>
> 1) Add a public String getBodyText() method to the digester.  I can then
> push the digester itself on the bottom of the stack and thereby get access
> to the XML body that way.
>
> 2) Create a new series of addCallMethod()'s which take an "int n" parameter.
> If n were equal to 0 then it would behave like it does now.  If n == 1 then
> it would execute the method on the next-to-top object on the stack and so on
> down the line.  Then you could still pass in a paramCount of zero or
> whatever.
>
> 3) A variation on #2 above.  Only change is to call the new series of
> addCallMethod()'s something different like addCallMethodN() or something so
> as to not make so many variations of addCallMethod.
>
> 4) Another more drastic alternative would be to have all callbacks accept as
> a first parameter the Digester and then still do #1 above.  That would break
> a lot of code but make somethings a lot easier like collaborating with other
> objects on the stack.  Hey...I'm only dreaming here.
>
> But after all this...there may be another way of doing what I need that I am
> just missing.  So any help would be appreciated.
>

#2 is probably the best of these approaches -- it generalizes the concept
of "walk up the stack before calling" the best.  #1 and #4 seem especially
kludgey, because Rule instances have access to the body text of the
current element (via the body() method getting called) already.

In the short term, I'd suggest just writing your own version of
CallMethodRule (either by subclassing or cut-n-paste) and add a
constructor that takes the extra argument.  You can use your custom
implementation by calling Digester.addRule(), even if it is not part of
the standard Digester suite of Rule implementations yet.

> Thanks,
> Brent
>

Craig


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>