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

Laurent Bihanic laurent.bihanic at atosorigin.com
Tue Nov 20 08:43:09 PST 2001


Bradley S. Huffman wrote:

>>>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.
> 
> Which is a modeling construct to convey that each object in the sturcture
> needs a method Accept( Vistor).  That doesn't necessarily translate to
> actual code.  As along as each class implements a Accept method and the
> Visitor method has a corresponding method for each class, we're okay.
> 

No, if you do not have an interface, you'll have to do something like:

public void visit(Object o)
{
    if (o instanceof Element)   { ((Element)o).visit(this); }
    if (o instanceof Attribute) { ((Attribute)o).visit(this); }
    if (o instanceof Document)  { ((Document)o).visit(this); }
    if (o instanceof Comment)   { ((Comment)o).visit(this); }
    ...
}

Whereas, with an interface:

public void visit(Node o) { o.visit(this); }


Laurent




More information about the jdom-interest mailing list