Need to create custom Rule?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Need to create custom Rule?

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

I chose to use digester because it's usually a very fast and simple way
to get my XML parsing needs satisfied (well, at least for SAX parsing).  
Plus, I'm working with a high volume of messages to parse, so SAX is
appropriate here.

So do I need to create a custom rule here?  I don't think I missed a
convenience method that already exists that would cover this issue, but
I could be wrong.  If I do need to create a custom Rule, I suppose I
could take a look at the source for some of the Rule classes that exist
now.  I figure this is a common pattern to encounter and that I'm just
missing the obvious here.  Any direction would be greatly appreciated.

Thanks in advance.


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [digester] Need to create custom Rule?

Simon Kitching
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: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [digester] Need to create custom Rule?

Rahul Akolkar
On 6/9/05, Simon Kitching <[hidden email]> 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: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [digester] Need to create custom Rule?

Dan Tarkenton
In reply to this post by Simon Kitching

> 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: [hidden email] For
> additional commands, e-mail: [hidden email]

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: [hidden email]
For additional commands, e-mail: [hidden email]