commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Simon Kitching <skitch...@apache.org>
Subject Re: [digester] Need to create custom Rule?
Date Thu, 09 Jun 2005 04:14:06 GMT
On Wed, 2005-06-08 at 12:53 -0700, Dan Tarkenton wrote:
> Hello all.  I have a brief question that I believe revolves around the 
> creation of a custom Rule implementation.  I have an xml pattern that I 
> need to digest/parse, but I'm unsure of how to go about this 
> programmatically.
> 
> The pattern i'm interested in is:
> 
> <parent>
>     <child name="a" value="b" />
>     <child name="theParamDesired" value="theValueISeek" />
> </parent>
> 
> I essentially want to find the child element whose name attribute is 
> theParamDesired -- every time I find this attribute I want to call a 
> setValue(theValueISeek) method on some object.  I want to ignore all 
> other child elements which have different name/value attributes.  I'm 
> not all that comfortable with manipulating the stack with a custom Rule 
> implementation. 

Yes, I believe you do need to create a custom rule. Writing a custom
Rule really is very easy; it's simpler than writing custom SAX code to
achieve the same goal.

Roughly, you need this:

class MyChildHandlerRule extends Rule {
  public void begin(ns, elementName, attrs) {
    String nameAttr = attrs.get("name");
    if (nameAttr != 'theParamDesired')
      return;

    MyValueHolder obj = (MyValueHolder) digester.peek();
    String valueAttr = attrs.getValue("value");
    obj.setValue(valueAttr);
  }
}

digester.addRule("parent/child", new MyChildHandlerRule());

The SetPropertyRule does something similar to what you describe, but not
exactly. You might want to have a look at the code for this rule. But
being a "generic" rule, it can't make some of the simplifying
assumptions that the above custom Rule class can.

By the way, when posting to commons-user, please put the component name
in the subject line (as I have done in this reply).

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