RE: Attribute question for Commons Digester
Hi Jonathan, The "plugin" mechanism is for something completely different. It allows an application to process an xml file, and call back into user code when certain elements are found. This allows "extensible" frameworks to be written where the basic file format is fixed but users can *at specific locations* insert their own xml that triggers calls to their own rule classes. In your case (special handling of a particular xml attribute), writing a custom subclass of Rule is the correct solution. And the digester pattern strings only match elements, not attributes. So, as you found out, you need to add your custom rule using a pattern matching the required element, then within your rule pick out whichever attribute you are actually interested in. By the way, there are extensive examples on using and extending Digester. These examples are included in the "source" download bundle, and can also be browsed directly in the svn repository: http://svn.apache.org/repos/asf/jakarta/commons/proper/digester/trunk/src/examples/ Regards, Simon On Fri, 2007-06-29 at 13:46 +0200, Poulton, Jonathan wrote: > OK, got it. Configured the rule by doing: > > digester.addObjectCreate("article", Article.class); > digester.addRule("article", new ArticleIdSetRule()); > > With a class pretty much identical to the skeleton one you posted this > works just fine, (which is good enough for me), but I'm still not sure > about how the Plugin mechanism works. Do people usually just create > their own custom rules instead? > > Thanks very much for the help btw. > > Jon > > -Original Message- > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] > On Behalf Of James Carman > Sent: 29 June 2007 12:22 > To: Jakarta Commons Users List > Subject: Re: Attribute question for Commons Digester > > Just make a custom rule... > > public class SetIdRule extends Rule > { > public void begin(java.lang.String namespace, java.lang.String > name, org.xml.sax.Attributes attributes) > { > Article article = ( Article )digester.peek(); // get the thing off > the top of the "stack" > article.setId(cleanUpUnderscore(attributes.getAttribute("id"))); > } > } > > That's off the top of my head, so the method names might have been > changed to protect the innocent. :) > > Then, add it to your Digester using the addRule() method > > On 6/29/07, Poulton, Jonathan <[EMAIL PROTECTED]> wrote: > > Also, does the plugin class need to implement a specific class? It > > doesn't look that way in the examples, and there doesn't appear to be > > any way to get the Digester to call a specific method in the plugin > > class. > > > > I have the feeling I have misunderstood something here.. > > > > -Original Message- > > From: [EMAIL PROTECTED] > > [mailto:[EMAIL PROTECTED] > > On Behalf Of James Carman > > Sent: 29 June 2007 11:59 > > To: Jakarta Commons Users List > > Subject: Re: Attribute question for Commons Digester > > > > Yeah, I didn't notice the "_" (or read the rest of the request). > > You'll need a custom rule here to convert that value, otherwise > > SetProperties would work (and you don't need to specify "id"/"id" > > because it'll match the properties to the attributes if they match > > exactly). Why the underscore anyway? > > > > On 6/29/07, Poulton, Jonathan <[EMAIL PROTECTED]> wrote: > > > > > > You mean: > > > > > > digester.addObjectCreate("article", Article.class); > > > digester.addSetProperties("article", "id", "id"); > > > > > > Yup. I've tried that. The actual value of the id field in the XML > > > "_1234" is ignored and "0" is set on the Article id field instead. I > > > > assume that this is because of the underscore in the id field. > > > > > > -Original Message- > > > From: [EMAIL PROTECTED] > > > [mailto:[EMAIL PROTECTED] > > > On Behalf Of James Carman > > > Sent: 29 June 2007 11:48 > > > To: Jakarta Commons Users List > > > Subject: Re: Attribute question for Commons Digester > > > > > > Have you tried an ObjectCreate followed by a SetProperties? > > > > > > On 6/29/07, Poulton, Jonathan <[EMAIL PROTECTED]> wrote: > > > > > > > > Surely someone has done this before? > > > > > > > > > > > > -Original Message- > >
RE: Attribute question for Commons Digester
OK, got it. Configured the rule by doing: digester.addObjectCreate("article", Article.class); digester.addRule("article", new ArticleIdSetRule()); With a class pretty much identical to the skeleton one you posted this works just fine, (which is good enough for me), but I'm still not sure about how the Plugin mechanism works. Do people usually just create their own custom rules instead? Thanks very much for the help btw. Jon -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of James Carman Sent: 29 June 2007 12:22 To: Jakarta Commons Users List Subject: Re: Attribute question for Commons Digester Just make a custom rule... public class SetIdRule extends Rule { public void begin(java.lang.String namespace, java.lang.String name, org.xml.sax.Attributes attributes) { Article article = ( Article )digester.peek(); // get the thing off the top of the "stack" article.setId(cleanUpUnderscore(attributes.getAttribute("id"))); } } That's off the top of my head, so the method names might have been changed to protect the innocent. :) Then, add it to your Digester using the addRule() method On 6/29/07, Poulton, Jonathan <[EMAIL PROTECTED]> wrote: > Also, does the plugin class need to implement a specific class? It > doesn't look that way in the examples, and there doesn't appear to be > any way to get the Digester to call a specific method in the plugin > class. > > I have the feeling I have misunderstood something here.. > > -Original Message- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] > On Behalf Of James Carman > Sent: 29 June 2007 11:59 > To: Jakarta Commons Users List > Subject: Re: Attribute question for Commons Digester > > Yeah, I didn't notice the "_" (or read the rest of the request). > You'll need a custom rule here to convert that value, otherwise > SetProperties would work (and you don't need to specify "id"/"id" > because it'll match the properties to the attributes if they match > exactly). Why the underscore anyway? > > On 6/29/07, Poulton, Jonathan <[EMAIL PROTECTED]> wrote: > > > > You mean: > > > > digester.addObjectCreate("article", Article.class); > > digester.addSetProperties("article", "id", "id"); > > > > Yup. I've tried that. The actual value of the id field in the XML > > "_1234" is ignored and "0" is set on the Article id field instead. I > > assume that this is because of the underscore in the id field. > > > > -Original Message- > > From: [EMAIL PROTECTED] > > [mailto:[EMAIL PROTECTED] > > On Behalf Of James Carman > > Sent: 29 June 2007 11:48 > > To: Jakarta Commons Users List > > Subject: Re: Attribute question for Commons Digester > > > > Have you tried an ObjectCreate followed by a SetProperties? > > > > On 6/29/07, Poulton, Jonathan <[EMAIL PROTECTED]> wrote: > > > > > > Surely someone has done this before? > > > > > > > > > -Original Message- > > > From: Poulton, Jonathan [mailto:[EMAIL PROTECTED] > > > Sent: 28 June 2007 17:41 > > > To: commons-user@jakarta.apache.org > > > Subject: Digester problem > > > > > > Hi there, > > > I've tried finding a solution to what I think should be quite a > > > simple > > > > > problem using the Commons Digester, but the tutorials I can find > > > on the subject only really cover the basics and aren't of any help. > > > > > > Given an XML fragment like the following: > > > > > > > > > ... > > > > > > > > > I need the Digester to call a method on an Article class, with the > > > following signiture: > > > > > > public void setId(long id); > > > > > > In other words I just need a Rule that will fire for an XML "id" > > > attribute, and remove an underscore from the front of a String > > > before converting it to a long, and calling the appropriate method. > > > > > > I realise that this will require some kind of custom class to chop > > > up the String, but the pattern you apply to matching the rule > > > ("article/id") appears to refer to a nested id element, rather > > > than an > > > > > id attribute. > > > > > > I can't seem to find an example of this anywhere. All the cu
Re: Attribute question for Commons Digester
Just make a custom rule... public class SetIdRule extends Rule { public void begin(java.lang.String namespace, java.lang.String name, org.xml.sax.Attributes attributes) { Article article = ( Article )digester.peek(); // get the thing off the top of the "stack" article.setId(cleanUpUnderscore(attributes.getAttribute("id"))); } } That's off the top of my head, so the method names might have been changed to protect the innocent. :) Then, add it to your Digester using the addRule() method On 6/29/07, Poulton, Jonathan <[EMAIL PROTECTED]> wrote: Also, does the plugin class need to implement a specific class? It doesn't look that way in the examples, and there doesn't appear to be any way to get the Digester to call a specific method in the plugin class. I have the feeling I have misunderstood something here.. -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of James Carman Sent: 29 June 2007 11:59 To: Jakarta Commons Users List Subject: Re: Attribute question for Commons Digester Yeah, I didn't notice the "_" (or read the rest of the request). You'll need a custom rule here to convert that value, otherwise SetProperties would work (and you don't need to specify "id"/"id" because it'll match the properties to the attributes if they match exactly). Why the underscore anyway? On 6/29/07, Poulton, Jonathan <[EMAIL PROTECTED]> wrote: > > You mean: > > digester.addObjectCreate("article", Article.class); > digester.addSetProperties("article", "id", "id"); > > Yup. I've tried that. The actual value of the id field in the XML > "_1234" is ignored and "0" is set on the Article id field instead. I > assume that this is because of the underscore in the id field. > > -Original Message- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] > On Behalf Of James Carman > Sent: 29 June 2007 11:48 > To: Jakarta Commons Users List > Subject: Re: Attribute question for Commons Digester > > Have you tried an ObjectCreate followed by a SetProperties? > > On 6/29/07, Poulton, Jonathan <[EMAIL PROTECTED]> wrote: > > > > Surely someone has done this before? > > > > > > -Original Message- > > From: Poulton, Jonathan [mailto:[EMAIL PROTECTED] > > Sent: 28 June 2007 17:41 > > To: commons-user@jakarta.apache.org > > Subject: Digester problem > > > > Hi there, > > I've tried finding a solution to what I think should be quite a > > simple > > > problem using the Commons Digester, but the tutorials I can find on > > the subject only really cover the basics and aren't of any help. > > > > Given an XML fragment like the following: > > > > > > ... > > > > > > I need the Digester to call a method on an Article class, with the > > following signiture: > > > > public void setId(long id); > > > > In other words I just need a Rule that will fire for an XML "id" > > attribute, and remove an underscore from the front of a String > > before converting it to a long, and calling the appropriate method. > > > > I realise that this will require some kind of custom class to chop > > up the String, but the pattern you apply to matching the rule > > ("article/id") appears to refer to a nested id element, rather than > > an > > > id attribute. > > > > I can't seem to find an example of this anywhere. All the custom > > rules > > > examples appear to refer to nested elements. Any suggestions? Have I > > missed something in the API? > > > > Cheers > > > > Jon > > > > > > > > > > - To unsubscribe, e-mail: > > [EMAIL PROTECTED] > > For additional commands, e-mail: > > [EMAIL PROTECTED] > > > > > > - > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Attribute question for Commons Digester
Also, does the plugin class need to implement a specific class? It doesn't look that way in the examples, and there doesn't appear to be any way to get the Digester to call a specific method in the plugin class. I have the feeling I have misunderstood something here.. -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of James Carman Sent: 29 June 2007 11:59 To: Jakarta Commons Users List Subject: Re: Attribute question for Commons Digester Yeah, I didn't notice the "_" (or read the rest of the request). You'll need a custom rule here to convert that value, otherwise SetProperties would work (and you don't need to specify "id"/"id" because it'll match the properties to the attributes if they match exactly). Why the underscore anyway? On 6/29/07, Poulton, Jonathan <[EMAIL PROTECTED]> wrote: > > You mean: > > digester.addObjectCreate("article", Article.class); > digester.addSetProperties("article", "id", "id"); > > Yup. I've tried that. The actual value of the id field in the XML > "_1234" is ignored and "0" is set on the Article id field instead. I > assume that this is because of the underscore in the id field. > > -Original Message- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] > On Behalf Of James Carman > Sent: 29 June 2007 11:48 > To: Jakarta Commons Users List > Subject: Re: Attribute question for Commons Digester > > Have you tried an ObjectCreate followed by a SetProperties? > > On 6/29/07, Poulton, Jonathan <[EMAIL PROTECTED]> wrote: > > > > Surely someone has done this before? > > > > > > -Original Message- > > From: Poulton, Jonathan [mailto:[EMAIL PROTECTED] > > Sent: 28 June 2007 17:41 > > To: commons-user@jakarta.apache.org > > Subject: Digester problem > > > > Hi there, > > I've tried finding a solution to what I think should be quite a > > simple > > > problem using the Commons Digester, but the tutorials I can find on > > the subject only really cover the basics and aren't of any help. > > > > Given an XML fragment like the following: > > > > > > ... > > > > > > I need the Digester to call a method on an Article class, with the > > following signiture: > > > > public void setId(long id); > > > > In other words I just need a Rule that will fire for an XML "id" > > attribute, and remove an underscore from the front of a String > > before converting it to a long, and calling the appropriate method. > > > > I realise that this will require some kind of custom class to chop > > up the String, but the pattern you apply to matching the rule > > ("article/id") appears to refer to a nested id element, rather than > > an > > > id attribute. > > > > I can't seem to find an example of this anywhere. All the custom > > rules > > > examples appear to refer to nested elements. Any suggestions? Have I > > missed something in the API? > > > > Cheers > > > > Jon > > > > > > > > > > - To unsubscribe, e-mail: > > [EMAIL PROTECTED] > > For additional commands, e-mail: > > [EMAIL PROTECTED] > > > > > > - > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Attribute question for Commons Digester
I have no idea why the underscore is there; but its there in the source XML and my code has to deal with it *shrug*. I do realise that I need a custom rule - but I can't seem to get it working. Say I have a rule called "ArticleIDTransform", then I do something like: PluginCreateRule pcr = new PluginCreateRule(ArticleIDTransform.class); digester.addRule("article", pcr); digester.addSetProperties("article", "id", "id"); The rule doesn't fire..what I am doing wrong? Jon -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of James Carman Sent: 29 June 2007 11:59 To: Jakarta Commons Users List Subject: Re: Attribute question for Commons Digester Yeah, I didn't notice the "_" (or read the rest of the request). You'll need a custom rule here to convert that value, otherwise SetProperties would work (and you don't need to specify "id"/"id" because it'll match the properties to the attributes if they match exactly). Why the underscore anyway? On 6/29/07, Poulton, Jonathan <[EMAIL PROTECTED]> wrote: > > You mean: > > digester.addObjectCreate("article", Article.class); > digester.addSetProperties("article", "id", "id"); > > Yup. I've tried that. The actual value of the id field in the XML > "_1234" is ignored and "0" is set on the Article id field instead. I > assume that this is because of the underscore in the id field. > > -----Original Message- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] > On Behalf Of James Carman > Sent: 29 June 2007 11:48 > To: Jakarta Commons Users List > Subject: Re: Attribute question for Commons Digester > > Have you tried an ObjectCreate followed by a SetProperties? > > On 6/29/07, Poulton, Jonathan <[EMAIL PROTECTED]> wrote: > > > > Surely someone has done this before? > > > > > > -Original Message- > > From: Poulton, Jonathan [mailto:[EMAIL PROTECTED] > > Sent: 28 June 2007 17:41 > > To: commons-user@jakarta.apache.org > > Subject: Digester problem > > > > Hi there, > > I've tried finding a solution to what I think should be quite a > > simple > > > problem using the Commons Digester, but the tutorials I can find on > > the subject only really cover the basics and aren't of any help. > > > > Given an XML fragment like the following: > > > > > > ... > > > > > > I need the Digester to call a method on an Article class, with the > > following signiture: > > > > public void setId(long id); > > > > In other words I just need a Rule that will fire for an XML "id" > > attribute, and remove an underscore from the front of a String > > before converting it to a long, and calling the appropriate method. > > > > I realise that this will require some kind of custom class to chop > > up the String, but the pattern you apply to matching the rule > > ("article/id") appears to refer to a nested id element, rather than > > an > > > id attribute. > > > > I can't seem to find an example of this anywhere. All the custom > > rules > > > examples appear to refer to nested elements. Any suggestions? Have I > > missed something in the API? > > > > Cheers > > > > Jon > > > > > > > > > > - To unsubscribe, e-mail: > > [EMAIL PROTECTED] > > For additional commands, e-mail: > > [EMAIL PROTECTED] > > > > > > - > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Attribute question for Commons Digester
Yeah, I didn't notice the "_" (or read the rest of the request). You'll need a custom rule here to convert that value, otherwise SetProperties would work (and you don't need to specify "id"/"id" because it'll match the properties to the attributes if they match exactly). Why the underscore anyway? On 6/29/07, Poulton, Jonathan <[EMAIL PROTECTED]> wrote: You mean: digester.addObjectCreate("article", Article.class); digester.addSetProperties("article", "id", "id"); Yup. I've tried that. The actual value of the id field in the XML "_1234" is ignored and "0" is set on the Article id field instead. I assume that this is because of the underscore in the id field. -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of James Carman Sent: 29 June 2007 11:48 To: Jakarta Commons Users List Subject: Re: Attribute question for Commons Digester Have you tried an ObjectCreate followed by a SetProperties? On 6/29/07, Poulton, Jonathan <[EMAIL PROTECTED]> wrote: > > Surely someone has done this before? > > > -Original Message- > From: Poulton, Jonathan [mailto:[EMAIL PROTECTED] > Sent: 28 June 2007 17:41 > To: commons-user@jakarta.apache.org > Subject: Digester problem > > Hi there, > I've tried finding a solution to what I think should be quite a simple > problem using the Commons Digester, but the tutorials I can find on > the subject only really cover the basics and aren't of any help. > > Given an XML fragment like the following: > > > ... > > > I need the Digester to call a method on an Article class, with the > following signiture: > > public void setId(long id); > > In other words I just need a Rule that will fire for an XML "id" > attribute, and remove an underscore from the front of a String before > converting it to a long, and calling the appropriate method. > > I realise that this will require some kind of custom class to chop up > the String, but the pattern you apply to matching the rule > ("article/id") appears to refer to a nested id element, rather than an > id attribute. > > I can't seem to find an example of this anywhere. All the custom rules > examples appear to refer to nested elements. Any suggestions? Have I > missed something in the API? > > Cheers > > Jon > > > > - > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Attribute question for Commons Digester
You mean: digester.addObjectCreate("article", Article.class); digester.addSetProperties("article", "id", "id"); Yup. I've tried that. The actual value of the id field in the XML "_1234" is ignored and "0" is set on the Article id field instead. I assume that this is because of the underscore in the id field. -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of James Carman Sent: 29 June 2007 11:48 To: Jakarta Commons Users List Subject: Re: Attribute question for Commons Digester Have you tried an ObjectCreate followed by a SetProperties? On 6/29/07, Poulton, Jonathan <[EMAIL PROTECTED]> wrote: > > Surely someone has done this before? > > > -Original Message- > From: Poulton, Jonathan [mailto:[EMAIL PROTECTED] > Sent: 28 June 2007 17:41 > To: commons-user@jakarta.apache.org > Subject: Digester problem > > Hi there, > I've tried finding a solution to what I think should be quite a simple > problem using the Commons Digester, but the tutorials I can find on > the subject only really cover the basics and aren't of any help. > > Given an XML fragment like the following: > > > ... > > > I need the Digester to call a method on an Article class, with the > following signiture: > > public void setId(long id); > > In other words I just need a Rule that will fire for an XML "id" > attribute, and remove an underscore from the front of a String before > converting it to a long, and calling the appropriate method. > > I realise that this will require some kind of custom class to chop up > the String, but the pattern you apply to matching the rule > ("article/id") appears to refer to a nested id element, rather than an > id attribute. > > I can't seem to find an example of this anywhere. All the custom rules > examples appear to refer to nested elements. Any suggestions? Have I > missed something in the API? > > Cheers > > Jon > > > > - > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Attribute question for Commons Digester
Have you tried an ObjectCreate followed by a SetProperties? On 6/29/07, Poulton, Jonathan <[EMAIL PROTECTED]> wrote: Surely someone has done this before? -Original Message- From: Poulton, Jonathan [mailto:[EMAIL PROTECTED] Sent: 28 June 2007 17:41 To: commons-user@jakarta.apache.org Subject: Digester problem Hi there, I've tried finding a solution to what I think should be quite a simple problem using the Commons Digester, but the tutorials I can find on the subject only really cover the basics and aren't of any help. Given an XML fragment like the following: ... I need the Digester to call a method on an Article class, with the following signiture: public void setId(long id); In other words I just need a Rule that will fire for an XML "id" attribute, and remove an underscore from the front of a String before converting it to a long, and calling the appropriate method. I realise that this will require some kind of custom class to chop up the String, but the pattern you apply to matching the rule ("article/id") appears to refer to a nested id element, rather than an id attribute. I can't seem to find an example of this anywhere. All the custom rules examples appear to refer to nested elements. Any suggestions? Have I missed something in the API? Cheers Jon - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Attribute question for Commons Digester
Surely someone has done this before? -Original Message- From: Poulton, Jonathan [mailto:[EMAIL PROTECTED] Sent: 28 June 2007 17:41 To: commons-user@jakarta.apache.org Subject: Digester problem Hi there, I've tried finding a solution to what I think should be quite a simple problem using the Commons Digester, but the tutorials I can find on the subject only really cover the basics and aren't of any help. Given an XML fragment like the following: ... I need the Digester to call a method on an Article class, with the following signiture: public void setId(long id); In other words I just need a Rule that will fire for an XML "id" attribute, and remove an underscore from the front of a String before converting it to a long, and calling the appropriate method. I realise that this will require some kind of custom class to chop up the String, but the pattern you apply to matching the rule ("article/id") appears to refer to a nested id element, rather than an id attribute. I can't seem to find an example of this anywhere. All the custom rules examples appear to refer to nested elements. Any suggestions? Have I missed something in the API? Cheers Jon - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]