commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Simon Kitching <>
Subject Re: DIGESTER/XML-RULES: Accessing attributes in a call-method-rule
Date Fri, 15 Apr 2005 23:19:21 GMT
On Fri, 2005-04-15 at 20:51 +0100, wrote:
> Dear All,
> I have a tree fragment looking a little like:
> <something somename="12" somedate="2005-03-15" sometruth="true"
> somearray="[foo,bar]"/>
> The problem is that I haven't figured out and can't find refernce of how
> or even 'if' it is possible to refer to attributes in my pattern.  xpath
> style /path/to/@attribute don't seem to work and thus I'd like help in
> figuring out how to do the following illegal bit of digesting:

No, there is no way to refer to attributes in the "pattern" associated
with a rule. It would indeed be nice to have this, but no-one has
implemented this, and it is not trivial. There was a proposal to add
"filters" to rules, so that once a pattern matched, the associated rule
might still be skipped if the "filter" failed (eg if a required
attribute was not present or did not match the required value). This
does look easier to implement than attribute-matching support in rule
patterns. However this has not been implemented either.

I do hope that digester 2.x will have such a feature. However that's
still vapourware at the current time.

> <pattern "*/something">
>    <object-create-rule classname=""/>
>    <call-method-rule pattern="*/@somename" methodname="setName"
> paramcount="0" paramtypes="java.lang.String"/>
>    <call-method-rule pattern="*/@somedate" methodname="setDate"
> paramcount="0" paramtypes="java.util.Date"/>
> </pattern>

I'm not entirely sure that you've understood call-method-rule correctly

The call-method-rule entry determines what method will be called, and on
what object (the top object on the stack at the time the
call-method-rule matches the input xml). However the parameters that are
passed to the target method are generally selected via call-param-rule
entries - and call-param-rule *can* select attributes from the matching
xml element. 

<pattern = "*/something">
  <object-create-rule classname=""/>

  <!-- call setName passing the 'somename' xml attribute -->
  <call-method-rule methodname="setName" 
    paramcount="1" paramtypes="java.lang.String"/>
  <call-param-rule paramnumber="0" attrname="somename"/>

  <!-- call setDate passing the 'somedate' xml attribute -->
  <call-method-rule methodname="setDate"
    paramcount="1" paramtypes="java.util.Date"/>
  <call-param-rule paramnumber="0" attrname="somedate"/>

The call-method-rule does include a "shortcut" for the common case where
there is one parameter to the target method, and that parameter is taken
from the body text of the matched xml element. However this shortcut
doesn't support passing attribute data - and it doesn't need to, as
call-param-rule can do this.

The "paramtypes" fields above are probably optional; the value defaults
to whatever is declared as the parameter type for the target method.
Only in odd situations do you actually need to specify the paramtype
directly. However it can't do any harm...

By the way, I think the xmlrules module sucks. I believe Digester is
much easier to understand when you use the Digester java API directly.

Note also that for the "somedate" attribute, digester will try to use
the BeanUtils library's ConvertUtils functionality to do the necessary
conversion from String to java.util.Date. However BeanUtils does not
define a converter from String to Date (because different countries have
different conventions). So in order for somedate (a string) to be
automatically converted to a java.util.Date object you will need to
register an appropriate converter object using
See the BeanUtils library documentation for info on this.



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

View raw message