commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Simon Kitching <si...@ecnetwork.co.nz>
Subject Re: digester - How do I call methods on objects further up the stack
Date Tue, 15 Apr 2003 22:40:03 GMT
If I have understood your email correctly, you have the following on the
Digester stack of created objects:
  ShopRequests | ShopRequest | Location

you then want a rule to fire against the "level" tag in the input which
will invoke an addHierarchyNode(num, body) method on the enclosing
ShopRequest object, not the enclosing Location object?

And that you tried to solve this by the following but it doesn't work as
expected?

> 	// This is the problem section.  How can I call addHierarchyNode in
> the 
> 	// ShopRequests object for every instance of <level>?
>       String[] ctypes = {"java.lang.Integer", "java.lang.String"};
>       dig.addCallMethod("ShopRequests/visit", "addHierarchyNode", 2,
> ctypes);
>       dig.addCallParam("ShopRequests/visit/location/hierarchy/level", 0,
> "num");
>       dig.addCallParam("ShopRequests/visit/location/hierarchy/level", 1);
> 
> 	// End of problem section

You mention "HierarchyLevel" objects and SetNext rules, but your example
doesn't use either of these. I assume that you have tried two approaches
to solving your problem: (a) creating a HierarchyLevel and using
SetNext, or (b) using CallMethodRule("ShopRequests/visit", ...), and
that your email includes only code for the latter. If I have
misunderstood, please explain further.


Your addCallMethod is given the pattern "ShopRequests/visit", so it will
fire only once for each visit node, not once for each level node. This
approach will not do what you need; in order for the method to be
invoked for each level tag, the AddCallMethodRule needs to be created
using pattern matching level tags - and of course it then fires when
Location is the top object, not ShopRequest.


The simplest solution would be to add methods on Location:
  /** create link from child to parent */
  public void setParent(ShopRequest sr)
  { 
    parent = sr;
  }

  public void addHierarchyNode(int num, String name)
  {
    parent.addHierarchyNode(num, name);
  }
then to use a SetTop rule to tell Location objects what their parent
SopRequest object is when they are created (following the
ObjectCreateRule). A CallMethodRule or SetNextRule can then be used in
its "normal" way, to invoke a method on the top object (a Location
object). The fact that the Location object forwards the data on to its
parent becomes invisible to Digester.


An alternative solution would be to define a new CallMethodOnStackRule
which is basically a copy of CallMethodRule except that it takes an
extra parameter being a stack offset, and instead of invoking the method
on the top object, it invokes it on the object at the specified offset.
This shouldn't be much work.  The same approach could be used to create
a SetAnyRule which is based on SetNextRule but takes a stack offset,
etc.


Hope this helps,

Simon





Mime
View raw message