[Digester] setting properties from the app's runtime

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

[Digester] setting properties from the app's runtime

Timo Boewing
Hello all,

I am working with Digester since some time now and am very happy with
it; however, I have one major problem with it: whenever I let Digester
create objects using addObjectCreate(), i want to set a property on this
instance before calls of addSetProperties() or so are made. This
property is not from the XML but from the app's runtime, say, a global
variable. So, what I want to do is like this:

cfg.addObjectCreate(
        "*/catalogues/catalogue",
        Question.class.getCanonicalName(),
        "class"
);

// this is my invented "callMethod()"...
cfg.callMethod(
        "*/catalogues/catalogue",
        "setContainer",
        1
        this.container
);
cfg.callMethodParam(
        "*/catalogues/catalogue",
        0,
        this.container
);


cfg.addSetProperties("*/catalogues/catalogue");

So, besides the stuff done by addSetProperties() and addCallMethod() and
that, I just want to call a method (set a property) of the new instance
whose parameter is an object from the calling app.
What have I missed?


--
greetings,                   |  /"\
                             |  \ /  ASCII-Ribbon-Campaign
Timo                         |   X     Against HTML Mail
                             |  / \
----------------------------------------------------------------------
PUBLIC KEY:
52F3311A     Timo Boewing  <[hidden email]> 2003/10/30
Fingerprint = F743 E0AA A2F0 1B33 F6FA 417B 72BE 740D 52F3 311A


signature.asc (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [Digester] setting properties from the app's runtime

Simon Kitching
On Wed, 2005-05-25 at 00:50 +0200, Timo Boewing wrote:
> Hello all,
>
> I am working with Digester since some time now and am very happy with
> it; however, I have one major problem with it: whenever I let Digester
> create objects using addObjectCreate(), i want to set a property on this
> instance before calls of addSetProperties() or so are made. This
> property is not from the XML but from the app's runtime, say, a global
> variable.

Hi Timo,

Sorry I didn't see your email earlier.

You can use ObjectParamRule to pass arbitrary objects as part of a
CallMethodRule. However CallMethodRule rules actually invoke the target
method when the xml end tag is reached, which might not be what you want
if you *must* have the call made before addSetProperties.

An alternative is to use FactoryCreateRule rather than ObjectCreateRule
to create your object. You can then provide a custom
ObjectCreationFactory which configures your new instance however it
wants.

Or you can write a custom rule, QuestionInitialiserRule, that makes the
necessary call, and add it:
  Rule questionInitialiserRule = new QuestionInitialiserRule(...);

  digester.addObjectCreate(...);
  digester.addRule(..., questionInitialiserRule);
  digester.addSetProperties(...);
  ....
A custom rule like this should be about 20 lines of code.


I would think that using FactoryCreateRule is probably the best option
given your description.

Regards,

Simon


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

Reply | Threaded
Open this post in threaded view
|

Re: [Digester] setting properties from the app's runtime

Timo Boewing
On Mon, 2005-05-30 at 14:34 +1200, Simon Kitching wrote:

> Hi Timo,

Hello Simon,
>
> Sorry I didn't see your email earlier.

Hey, better than no answer at all :-)

>
> You can use ObjectParamRule to pass arbitrary objects as part of a
> CallMethodRule. However CallMethodRule rules actually invoke the target
> method when the xml end tag is reached, which might not be what you want
> if you *must* have the call made before addSetProperties.
>
> An alternative is to use FactoryCreateRule rather than ObjectCreateRule
> to create your object. You can then provide a custom
> ObjectCreationFactory which configures your new instance however it
> wants.
Yes, that is pretty fine; I found a sample of this posted by your around
this time using Google in an mail archive; thank you!

>
> Or you can write a custom rule, QuestionInitialiserRule, that makes the
> necessary call, and add it:
>   Rule questionInitialiserRule = new QuestionInitialiserRule(...);
>
>   digester.addObjectCreate(...);
>   digester.addRule(..., questionInitialiserRule);
>   digester.addSetProperties(...);
>   ....
> A custom rule like this should be about 20 lines of code.
The bad news is that I "missed that train" by now, because I completely
settled over to a DigesterLoader XML config in favour of lean Java code,
so
that all my rules are now defined in cfg files rather than in my code,
thus this option is now obsolete for me :-)


However, I am using this opportunity to ask two more questions to you
and the list:

1) I need to set the "classname" attribute of the object-create-rule
dynamically. I did this before when my rules were set in my app's Java
code, so that e.g. the "type" param was read from my XML definitions:

<page id="pg1" type="x.y.QuestionPage">
        [...]
</page>

... so that the type attrib is placed into the classname of the
object-create rule. Is this still possible when using the Digester XML
config?


2) I really am stupid enough not to find a reference documentation for
the DigesterLoader; i.e. what control tags are there and what attribts
are valid for them. Does anyone know where to find it?


>
> Regards,
>
> Simon
>
>

Thank you for your reply, Simon!

--
greetings,                   |  /"\
                             |  \ /  ASCII-Ribbon-Campaign
Timo                         |   X     Against HTML Mail
                             |  / \
----------------------------------------------------------------------
PUBLIC KEY:
52F3311A     Timo Boewing  <[hidden email]> 2003/10/30
Fingerprint = F743 E0AA A2F0 1B33 F6FA 417B 72BE 740D 52F3 311A


signature.asc (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [Digester] setting properties from the app's runtime

Simon Kitching
On Wed, 2005-06-01 at 23:57 +0200, Timo Boewing wrote:

> 1) I need to set the "classname" attribute of the object-create-rule
> dynamically. I did this before when my rules were set in my app's Java
> code, so that e.g. the "type" param was read from my XML definitions:
>
> <page id="pg1" type="x.y.QuestionPage">
> [...]
> </page>
>
> ... so that the type attrib is placed into the classname of the
> object-create rule. Is this still possible when using the Digester XML
> config?

You mean you used this?
  new ObjectCreateRule(digester, "x.y.DefaultPage", "type");

I think you need
  <object-create-rule
     pattern=".."
     classname="x.y.DefaultPage"
     attrname="type"/>

>
>
> 2) I really am stupid enough not to find a reference documentation for
> the DigesterLoader; i.e. what control tags are there and what attribts
> are valid for them. Does anyone know where to find it?

The best thing to do is to look at the dtd for xmlrules:

http://svn.apache.org/repos/asf/jakarta/commons/proper/digester/tags/
  DIGESTER_1_6/src/java/org/apache/commons/digester/
    xmlrules/digester-rules.dtd

Regards,

Simon


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