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 (Hashtables...)
Date Thu, 04 Sep 2003 11:22:51 GMT
On Thu, 2003-09-04 at 21:23, petra staub wrote:
> hi,
> 
> i have following little problem:
> 
> How can I get following XML structure into some java entities?
> my example.xml look about this:
> 
> <list name="list1">
>   <element name="name1">value1</element>
>   <element name="name2">value2</element>
>   <element name="name3">value3</element>
> </lists>

Hi Petra,

As far as I know it is not possible to do exactly what you are trying to
achieve. 

What you want is for each <list> element to cause the creation of a
HashMap object. However you also want to associate a name with each
created HashMap, but HashMaps don't have anywhere to store the name.

[NB: If your <list> objects were "anonymous" (without the name
attribute) it would be fairly simple to build the map-of-maps
structure you are looking for].


The only way around this is for the "parent" object of the HashMaps to
store the (name->map) association. However unfortunately I know of no
Rule in Digester capable of passing this info to the parent in the
required manner.

I think the simplest solution is to create your own class to represent
"list" elements;

class ElementList {  // one created for every <list> element
  private String name;
  private HashMap elements = new HashMap();

  public void setName(String name) {...}
  public void getName() {...}
  public void addElement(String name, String value) {...}
}

then when you use SetNext to tell the parent object about the new list
object, the parent can query the new child object for its name:

class ListHolder {  // holds set of Elementlist objects
  private HashMap lists = new HashMap();

  public void addElementList(ElementList el) {  
    lists.put(el.getName(), el);
  }
}

[If you feel like being fancy, ElementList could implement the Map
interface, making it transparent to users (or, more hackily, extend
HashMap)].


-- 

Why Digester can't currently do what you need:

Currently, the CallMethodRule is capable of passing arguments to methods
in a very flexible way - but it always targets the top object on the
stack. 

SetNextRule targets the "parent" object (top-1), but is only capable of
passing one parameter - the top object - so it can't do anything like
call put(String, Object).

I can see some justification here for enhancing CallMethodRule to be
able to invoke methods on objects other than the top one, and I don't
think it would be terribly difficult to do.

In fact, the hardest thing is probably to find a tasteful way to pass
the extra parameter to the CallMethodRule class (relative index on stack
of target object), given that its constructor is already heavily
overloaded. 

If you wished to have a go at coming up with a tidy solution for this, I
think there is a reasonable chance of the patch being accepted into the
Digester project....[NB: I am not a maintainer of Digester, just a
frequent user].


Regards,

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