[XML-SIG] My DOM Visitor Class(es)

Gabe Wachob gwachob@aimnet.com
Fri, 11 Dec 1998 23:54:06 -0800 (PST)


Hi folks-
	I threw together a very simple DOM Visitor class (it also has a
"Walker" mixin to create a Visitor that automatically walks a tree and
visits each Node).
	You can get it at:
http://www.aimnet.com/~gwachob/DOMVisitor.py

	I use the term "Visitor" loosely -- while inspired by the Visitor
Design Pattern in the book "Design Patterns", it is technically not
following that pattern. It looks to be useful nontheless. 
	The basic Visitor class does very very little -- if you subclass
it, you must add visit_ELEMENT, visit_TEXT, etc methods. The basic Visitor
class simply gets the type of the node you pass it and calls a
visit_<TYPENAME> method on that Node. (ie visit_TEXT(node)). 
	The WalkerMixin changes this basic behavior by visiting the Node's
children after the Node itself is visited. What makes this really nice is
that the method which visits the Node returns a value which tells the main
dispatcher method (visit) whether or not to visit the Node's children.
Thus, whole subtrees can be treated separately (or not at all) depending
on a visit to the root node of the subtree (that visit to the "root" node
of a subtree can visit parts of the subtree itself, or you may decide in
implementing the visit method that you can skip the entire subtree because
that subtree is irrelevant for your purposes. You can even build a
separate walker for that subtree to do some completely different
processing. How about multithreaded parsing?) 

Wouldn't using a DOM tree in this way (structurally) better allow DOM
parsers to only hold part of the DOM tree in memory?

	Anyway, enough rambling, I'd like people to take a look at the
code, tell me what they think (has any body else written code like this?),
tell me what improvements the code would need (yeah, yeah, it uses
recursion), etc. 
	Oh yeah, and please use it for your own projects!

	-Gabe
	

-------------------------------------------------------------------
http://www.aimnet.com/~gwachob               http://www.findlaw.com
"A popular Government, without popular information, or the means of 
acquiring it, is but a Prologue to a Farce or a Tragedy; or perhaps 
both." -- James Madison 
                       import std.disclaimer