commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Simon Kitching <si...@ecnetwork.co.nz>
Subject Re: [Digester] Modifying params
Date Thu, 02 Dec 2004 22:28:49 GMT
Hi Andy,

On Fri, 2004-12-03 at 06:39, Andy Kriger wrote:
> I'm new to digester and I have it more or less working the way I want,
> however I have a question about modifying parameters.
> 
> Here's a config
> <a>
> <b>prependMe<b>
> <c>someText<c>
> </a>
> 
> I store b's body in a object field. Later I call addCallMethod using
> c's body as a param. I would like to be able to prepend b's body to
> c's body when I setup addCallParam (so that addCallMethod uses the
> parameter "prependMe/someText"). Is there a way to do this using any
> of the add. methods?

I don't quite understand your problem. This whole idea of "combining"
adjacent nodes transparently seems rather odd to me.

Presumably you have some object representing <a>. In that case I would
have 'setB(String)' and 'setC(String)' methods. The setC method can
choose to prepend anything it wants to its parameter. Implementing
things this way is moving logic out of the parsing stage into the
"business logic" classes where it would seem to me to belong.

But assuming that for some reason you can't do this, I would suggest
creating your own Rule class to handle this instead. This rule would be
used instead of a standard CallParamRule:
  Rule myRule = new MyBCCombinerRule();
  digester.addRule("a/b", myRule);
  digester.addRule("a/c", myRule);

In MyBCCombinerRule.body(...), the rule can check whether it has matched
a "b" node (in which case it stores the data) or a "c" node (in which
case it combines the values then writes the value into a param array for
later use by CallMethodRule (just as CallParamRule class does). Note
that this is a fairly ugly hack; I can't think of a better suggestion
though.

> Right now I do this withdigester.setSubstitutor, but I'm not clear on
> something with that solution - what is the scope of the substitutor?
> Is it used when processing every element that's encountered? How do
> you unset the substitutor?

This problem doesn't really seem to be a suitable use for the
'substitutor' functionality. At least, it wasn't designed with this in
mind. As described in the documentation, it's intended to support
"${foo}" type variable-expansion.

Yes the substitutor will process every element (and every attribute)
that's encountered.

You can 'unset' the substitutor via digester.setSubstitutor(null).

Regards,

Simon


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


Mime
View raw message