You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Erik Price <ep...@ptc.com> on 2003/03/17 21:54:20 UTC

[digester] setting a default value for a callMethod

The answer to this question seems like it should be trivial.  But I am 
just not seeing it.  If someone could help that'd be great.

My digester has hit upon a pattern, which triggers a method in the top 
object on the call stack.  Fine, that's normal.  However, the method 
expects an integer argument and in my XML file the element representing 
this pattern is empty.  This is throwing a SAXException, as expected.

There are several solutions:

1. Overload my method with a no-arg version.  This doesn't seem like a 
good idea, changing my class structure simply to satisfy the digester, 
because a no-arg version would do nothing.

2. Not include the element at all if it does not contain a value, so 
that the digester doesn't see the pattern.  This doesn't seem like a 
great solution either.

3. Use a dummy value (-1 in my case) and then have the method check for 
the dummy value.  This still seems a little inelegant.

Hmm... I'm wondering if there's a way to tell the digester that I don't 
want to call that method if the element at the matching pattern is 
empty?  Or is this getting too detailed, in which case I'd have to look 
into JDOM or something.

Thanks!



Erik


Re: [digester] setting a default value for a callMethod

Posted by Erik Price <ep...@ptc.com>.

Simon Kitching wrote:


> Just on a side-note, I am not sure that your XML design is entirely
> sound. To me,
> 
> <foo>1</foo>  means "here is a foo, with value 1".
> <foo/> means "here is a foo, with no value".
> 
> You appear to want to pretend that <foo/> doesn't exist - which doesn't
> seem right to me. If <foo/> has no meaning, then why is it in the input
> at all?

Well, that's a good point.  The only reason I would rather leave it in 
the input is that I am using the XML file as a user-friendly means of 
supplying data to my program.  (Another alternative I had considered was 
a Preferences or Properties file, but I wanted to try Digester after 
hearing how good it is -- and it *is* good.)

Ideally I'd just give my users an empty XML file and say "go ahead and 
fill out the values that apply, but leave the ones that don't alone".

> Regardless of the above digression, I think implementing a custom Rule
> object should solve your problem.

Thanks Simon, I will investigate the Rule classes.



Erik


Re: [digester] setting a default value for a callMethod

Posted by Simon Kitching <si...@ecnetwork.co.nz>.
On Tue, 2003-03-18 at 08:54, Erik Price wrote:
> Hmm... I'm wondering if there's a way to tell the digester that I don't 
> want to call that method if the element at the matching pattern is 
> empty?  Or is this getting too detailed, in which case I'd have to look 
> into JDOM or something.

I am not aware of any built-in digester functionality to do this.

However, one of the *features* of Digester is that you can write your
own Rule objects. So, you could write a class which implements Rule
which checks the attribute and only invokes your target method under
whatever conditions you feel are appropriate.

Just on a side-note, I am not sure that your XML design is entirely
sound. To me,

<foo>1</foo>  means "here is a foo, with value 1".
<foo/> means "here is a foo, with no value".

You appear to want to pretend that <foo/> doesn't exist - which doesn't
seem right to me. If <foo/> has no meaning, then why is it in the input
at all?

Regardless of the above digression, I think implementing a custom Rule
object should solve your problem.

Regards,

Simon