You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Simon Kitching <sk...@apache.org> on 2005/06/09 06:14:06 UTC

Re: [digester] Need to create custom Rule?

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


Re: [digester] Need to create custom Rule?

Posted by Rahul Akolkar <ra...@gmail.com>.
On 6/9/05, Simon Kitching <sk...@apache.org> wrote:
> On Wed, 2005-06-08 at 12:53 -0700, Dan Tarkenton wrote:
<snip/>
> > 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. 
<snap/>
> 
> Yes, I believe you do need to create a custom rule.
<snip/>

You can get by without it if you want (at the risk of polluting the
class with a convenience method). Add a method setValue(String,
String) and use CallMethodRule.

-Rahul

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


Re: [digester] Need to create custom Rule?

Posted by "Daniel S. Tarkenton" <da...@tarksoft.com>.
> 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

Sweet!  That custom Rule worked great and was very, very simple.  Thanks
for the tip.  My parser seems to be working again.

Oh, and apologies for omitting the particular commons resource my question
related to in the subject -- won't forget next time ;)

/Dan




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