You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Hernán Seoane <hs...@gmail.com> on 2006/09/07 16:44:46 UTC

Call a method in an external class

Hello, I'm using Commons Digester to parse a XML similar to this:

<element>
   <name>Name of the element</name>
   <categcode>CATEG_1</categcode>
</element>

Where 'categcode' is a code for the element's category. This category is
actually a Category object, and I need to fetch it with the use of a
service.
What I need is a way to call a method in that service directly from the
Digester rules XML (e.g. CategoryService.getCategory(String categ) ) that
sets this category in the Element as an object. Have in mind that I'm trying
to do is to avoid calling this service from the Element object itself,
because it is a business model object.


Thanks in advance,
Hernán P. L. Seoane

Re: Call a method in an external class

Posted by Hernán Seoane <he...@emindit.com>.
Hello Simon, thanks for your reply.

What I have is a class as the one you mention, whereas Category is a class
with a code, name and description. The XML for the element I'm parsing
contains only the code of the category, but I need to get the full Category
object from a database and set it to my Element object

Thanks again,
Hernan

On 9/8/06, Simon Kitching <sk...@apache.org> wrote:
>
> Hi Hernan,
>
> There is no way that an ObjectCreationFactory can have access to the
> contents of child elements. Digester is SAX based, and therefore the
> only info available to an ObjectCreationFactory is the xml attributes on
> the start element.
>
> Unfortunately, I don't really understand what you are trying to do here.
> Do you have something like this?
>
> public class Element {
>   public void setName(String) {...}
>   public void setCategory(Category c) {...}
> }
>
> or do you want the <element> tag to create an instance of some class
> whose type is returned by CategoryService.getCategory(catCode)
>
> or something else?
>
> Regards,
>
> Simon
>
> On Thu, 2006-09-07 at 13:54 -0300, Hernán Seoane wrote:
> > I've seen that what I request could be done with the
> factory-create-rule.
> > However, I can't seem to make the ObjectCreationFactory have access to
> the
> > content of the <categcode> tag ("CATEG_1" in my example)
> > Any help will be appreciated!
> >
> > Thanks,
> > Hernán
> >
> >
> > On 9/7/06, Hernán Seoane <hs...@gmail.com> wrote:
> > >
> > > Hello, I'm using Commons Digester to parse a XML similar to this:
> > >
> > > <element>
> > >    <name>Name of the element</name>
> > >    <categcode>CATEG_1</categcode>
> > > </element>
> > >
> > > Where 'categcode' is a code for the element's category. This category
> is
> > > actually a Category object, and I need to fetch it with the use of a
> > > service.
> > > What I need is a way to call a method in that service directly from
> the
> > > Digester rules XML (e.g. CategoryService.getCategory(String categ) )
> that
> > > sets this category in the Element as an object. Have in mind that I'm
> trying
> > > to do is to avoid calling this service from the Element object itself,
> > > because it is a business model object.
> > >
> > >
> > > Thanks in advance,
> > > Hernán P. L. Seoane
> > >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>

Re: Call a method in an external class

Posted by Simon Kitching <sk...@apache.org>.
Hi Hernan,

I think you'll need to write a custom rule for this, though it will be a
pretty simple one.

The FactoryCreateRule is very nearly what you want, but that does all
its work when the xml start element is encountered, ie has access to the
element attributes but not the element body text which you need.

So I recommend writing your own rule class:

 public class CreateCategoryRule extends Rule {
    private CategoryLookupThingy clt;

    public CreateCategoryRule(CategoryLookupThingy clt) {
      this.clt = clt;
    }

    public void body(String ns, String name, String text) {
        Category c = clt.lookup(text);
        // push an object on the stack so it can be accessed
        // by other rules that fire before the end method of
        // this rule fires
        digester.push(c);
    }

   public void end(String ns, String name) {
        // leave the stack as it was when this element was entered
        digester.pop();
   }
 }

  then:

    Digester d = new Digester();
    ...
    CreateCategoryRule ccr = new
      CreateCategoryRule(myCategoryLookupObj);
    d.addObjectCreate("element", Element.class);
    d.addRule("element/categcode", ccr);
    d.addSetNext("element/categcode", "setCategory");

The above is only rough code, but I hope it points you in the right
direction.

If you have problems, I recommend enabling logging so that you can see
what objects are on the stack and what rules are being fired. Digester
does a lot of very useful logging...

Regards,

Simon

On Sun, 2006-09-10 at 12:10 -0300, Hernán Seoane wrote:
> Hello Simon, thanks for your reply.
> 
> What I have is a class as the one you mention, whereas Category is a class
> with a code, name and description. The XML for the element I'm parsing
> contains only the code of the category, but I need to get the full Category
> object from a database and set it to my Element object
> 
> Thanks again,
> Hernan
> 
> On 9/8/06, Simon Kitching <sk...@apache.org> wrote:
> >
> > Hi Hernan,
> >
> > There is no way that an ObjectCreationFactory can have access to the
> > contents of child elements. Digester is SAX based, and therefore the
> > only info available to an ObjectCreationFactory is the xml attributes on
> > the start element.
> >
> > Unfortunately, I don't really understand what you are trying to do here.
> > Do you have something like this?
> >
> > public class Element {
> >   public void setName(String) {...}
> >   public void setCategory(Category c) {...}
> > }
> >
> > or do you want the <element> tag to create an instance of some class
> > whose type is returned by CategoryService.getCategory(catCode)
> >
> > or something else?
> >
> > Regards,
> >
> > Simon
> >
> > On Thu, 2006-09-07 at 13:54 -0300, Hernán Seoane wrote:
> > > I've seen that what I request could be done with the
> > factory-create-rule.
> > > However, I can't seem to make the ObjectCreationFactory have access to
> > the
> > > content of the <categcode> tag ("CATEG_1" in my example)
> > > Any help will be appreciated!
> > >
> > > Thanks,
> > > Hernán
> > >
> > >
> > > On 9/7/06, Hernán Seoane <hs...@gmail.com> wrote:
> > > >
> > > > Hello, I'm using Commons Digester to parse a XML similar to this:
> > > >
> > > > <element>
> > > >    <name>Name of the element</name>
> > > >    <categcode>CATEG_1</categcode>
> > > > </element>
> > > >
> > > > Where 'categcode' is a code for the element's category. This category
> > is
> > > > actually a Category object, and I need to fetch it with the use of a
> > > > service.
> > > > What I need is a way to call a method in that service directly from
> > the
> > > > Digester rules XML (e.g. CategoryService.getCategory(String categ) )
> > that
> > > > sets this category in the Element as an object. Have in mind that I'm
> > trying
> > > > to do is to avoid calling this service from the Element object itself,
> > > > because it is a business model object.
> > > >
> > > >
> > > > Thanks in advance,
> > > > Hernán P. L. Seoane
> > > >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: commons-user-help@jakarta.apache.org
> >
> >


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


Re: Call a method in an external class

Posted by Simon Kitching <sk...@apache.org>.
Hi Hernan,

There is no way that an ObjectCreationFactory can have access to the
contents of child elements. Digester is SAX based, and therefore the
only info available to an ObjectCreationFactory is the xml attributes on
the start element.

Unfortunately, I don't really understand what you are trying to do here.
Do you have something like this?

public class Element {
  public void setName(String) {...}
  public void setCategory(Category c) {...}
}

or do you want the <element> tag to create an instance of some class
whose type is returned by CategoryService.getCategory(catCode)

or something else?

Regards,

Simon

On Thu, 2006-09-07 at 13:54 -0300, Hernán Seoane wrote:
> I've seen that what I request could be done with the factory-create-rule.
> However, I can't seem to make the ObjectCreationFactory have access to the
> content of the <categcode> tag ("CATEG_1" in my example)
> Any help will be appreciated!
> 
> Thanks,
> Hernán
> 
> 
> On 9/7/06, Hernán Seoane <hs...@gmail.com> wrote:
> >
> > Hello, I'm using Commons Digester to parse a XML similar to this:
> >
> > <element>
> >    <name>Name of the element</name>
> >    <categcode>CATEG_1</categcode>
> > </element>
> >
> > Where 'categcode' is a code for the element's category. This category is
> > actually a Category object, and I need to fetch it with the use of a
> > service.
> > What I need is a way to call a method in that service directly from the
> > Digester rules XML (e.g. CategoryService.getCategory(String categ) ) that
> > sets this category in the Element as an object. Have in mind that I'm trying
> > to do is to avoid calling this service from the Element object itself,
> > because it is a business model object.
> >
> >
> > Thanks in advance,
> > Hernán P. L. Seoane
> >


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


Re: Call a method in an external class

Posted by Hernán Seoane <he...@emindit.com>.
I've seen that what I request could be done with the factory-create-rule.
However, I can't seem to make the ObjectCreationFactory have access to the
content of the <categcode> tag ("CATEG_1" in my example)
Any help will be appreciated!

Thanks,
Hernán


On 9/7/06, Hernán Seoane <hs...@gmail.com> wrote:
>
> Hello, I'm using Commons Digester to parse a XML similar to this:
>
> <element>
>    <name>Name of the element</name>
>    <categcode>CATEG_1</categcode>
> </element>
>
> Where 'categcode' is a code for the element's category. This category is
> actually a Category object, and I need to fetch it with the use of a
> service.
> What I need is a way to call a method in that service directly from the
> Digester rules XML (e.g. CategoryService.getCategory(String categ) ) that
> sets this category in the Element as an object. Have in mind that I'm trying
> to do is to avoid calling this service from the Element object itself,
> because it is a business model object.
>
>
> Thanks in advance,
> Hernán P. L. Seoane
>