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] question about parent-parameter-access
Date Thu, 04 Sep 2003 00:04:06 GMT
On Tue, 2003-09-02 at 23:03, Geronimo Ma. Hernandez wrote:
> Thanks for your reply, Simon!
> 
> Simon wrote:
> > It's still not clear to me what your data model is.

Ok, I see what you are trying to achieve now.
[please note: all code/suggestions below are untested; any or
all of them could be wrong :-]

I think the cause of your problems with Digester is this:

> class EntityDefinition {
>   public void addAttribute(String attributeName) throws DefinitionException;
>   public void putUniqueIndex(Integer idxNr, Integer idxOrder, String
> 		 attributeName);
> }

You are trying to avoid creating an object corresponding to the
"addAttributes" xml element, yet process subelements associated
with that element.

This isn't exactly "invalid", but this kind of disconnect between the
xml structure and your object structure leads to the kind of problems
you are experiencing. I've been there myself...


If you were to define a class

class OptionallyIndexedAttribute {
  String attributeName;
  Integer idxNr;
  Integer idxOrder;

  public void setName() {...};
  public void setUniqueIndex(int idxNr, int idxOrder) {...};
}

and have this method on class Entity:
  addAttribute(OptionallyIndexedAttribute attr) {
  }

then I think you would find the digester rules suddenly fall into place.
Use an ObjectCreateRule for "entity/addAttribute" to create an
OptionallyIndexedAttribute instance, and then rules triggered by
subelements (unique-index in your case) then operate on an object which
*knows* its name.


Other possibilities:

(1)
Assuming that there is only ever zero or one <unique-index> child
of an <addAttribute> node,

digester.addCallMethod(ADD_ATTRIBUTE_NODE, "addAttribute", 3);
digester.addCallParam(ADD_ATTRIBUTE_NODE, 0, "name");
digester.addCallParam(UINDEX_NODE, 1, "idx_number");
digester.addCallParam(UINDEX_NODE, 2, "idx_order");

and have this method on Entity:
  addAttribute(String name, Integer idxNr, Integer idxOrder) {...}

this will cause the following xml 
  <entity>
    <addAttribuute name="foo"/>
    <addAttribute name="bar"
      <unique-index idx_number="1" idx_order="2"/>
    </addAttribute>
  </entity>

to invoke 
	addAttribute("foo, null, null);
        addAttribute("bar", new Integer(1), new Integer(2));

(2)
When a "unique-index" element is encountered, you know that it applies
to the enclosing attribute, which will trigger an "addAttribute" method
soon. So why not just cache the index info until the addAttribute method
is invoked?

class Entity {
  boolean haveIndex = false;
  int currIdxNr;
  int currIdxOrder;

  void setUniqueIndex(int idxNr, int idxOrder) {
    haveIndex = true;
    currIdxNr = idxNr; 
    currIdxOrder = idxOrder;
  }

  void addAttribute(String name) {
     ....
     if (haveIndex) {
       addAttributeIndex(name, currIdxNr, currIdxOrder);
       haveIndex = false;
     }
  }

}

This feels a bit hacky to me, but I don't see why it won't work.
It certainly feels no hackier to me than referencing call parameters



Hope this helps,

Simon




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


Mime
View raw message