You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by ra...@dreamthought.com on 2005/04/15 21:51:40 UTC

DIGESTER/XML-RULES: Accessing attributes in a call-method-rule

Dear All,

I have a tree fragment looking a little like:

<something somename="12" somedate="2005-03-15" sometruth="true"
somearray="[foo,bar]"/>

Using xml rules I am trying to populate something like:
public class foo {
        private Vector somearray;
        private Date somedate;
        private String somename;
        // setters/accessors etc
}

Now in my digester rules I have the issue that due to the type differences
between the somearray - which I don't care about - and my xml
representation - any calls to <set-properties-rules/> seem to give me type
exceptions.  To circumvent this, and given that I'm only interested in the
somedate and sometruth attributes, I tried to use the call-method-rule. 
It's also perfect since I can specify the types to cast to.

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:

<pattern "*/something">
   <object-create-rule classname="bar.foo"/>
   <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>

Or something which will let me have the flexibility of call-method-rule
when setting my attributes.

I'm new to digester/xml-rules and am trying really hard not to curse and
just go back to writing my own sax handlers.

Either way, I'd be very grateful to anyone able to show me the light here.

Cheers,

Raf




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


Re: DIGESTER/XML-RULES: Accessing attributes in a call-method-rule

Posted by ra...@dreamthought.com.
Hi Simon,
...
> [snip]
>> 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.

Thanks for the clarification.

> 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.
>

ok.  exactly what I'm after.

Many thanks for clearing things up.

> 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.

Me too.  I'm trying to 'maintain the conventions' of the code base which
I'm currently extending.  It just so happens that it has extensively used
xmlrules.

> automatically converted to a java.util.Date object you will need to
> register an appropriate converter object using
>   ConvertUtils.register(..)
> See the BeanUtils library documentation for info on this.

Right.  I figured as much.

Many Thanks,

Raf



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


Re: DIGESTER/XML-RULES: Accessing attributes in a call-method-rule

Posted by Simon Kitching <sk...@apache.org>.
On Fri, 2005-04-15 at 20:51 +0100, rafiq@dreamthought.com wrote:
> Dear All,
> 
> I have a tree fragment looking a little like:
> 
> <something somename="12" somedate="2005-03-15" sometruth="true"
> somearray="[foo,bar]"/>
> 
[snip]
> 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="bar.foo"/>
>    <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
however.

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="bar.foo"/>

  <!-- 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"/>
</pattern>

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
  ConvertUtils.register(..)
See the BeanUtils library documentation for info on this.

Regards,

Simon


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