[jdom-interest] How to ease traversal of JDOM tree

Laurent Bihanic laurent.bihanic at atosorigin.com
Tue Nov 20 01:01:26 PST 2001


Bradley S. Huffman wrote:

>>add/remove/get/setContent are today only defined by Element and Document
>>but should be defined in all classes if we want to be able to traverse
>>the tree downward. The add/remove/setContent would then throw an
>>exception while getContent could return either an empty List or a List
>>containing a single Text object.
> 
> Why add these methods to the leaf nodes (Comments, Text, PI, etc.)
> where they don't apply? I don't like the idea of throwing a exception
> at runtime for something that can be caught at comiple time.
> 

The idea was to define a single interface for all nodes, making no difference 
between non-leaf and leaf nodes. This interface must then include that means 
to traverse the tree. getContent is probably the main method to do that.

I proposed to include add/remove/setContent too because, most of the time, you 
walk the tree to alter it.
add/setContent already throw a runtime exception (IllegalAddException) and 
removeContent simply return false if the removal did not occur. So, there's no 
change in the API here.

> 
> Why not a Visitor class (see the TODO list)?
>

Yes but visited objects are supposed to accept the Visitor. Gamma & al. 
actually propose to define an interface (thay call "Node" !) that defines the 
accept() method.
So we are kind of back to the same proposal: It would be nice if all JDOM 
node-type classes implement an single interface !

Laurent




More information about the jdom-interest mailing list