dbertoni 2003/01/28 21:22:38 Modified: c/src/DOMSupport TreeWalker.hpp TreeWalker.cpp Log: Added support for stopping a traversal, then restarting it. Revision Changes Path 1.6 +95 -27 xml-xalan/c/src/DOMSupport/TreeWalker.hpp Index: TreeWalker.hpp =================================================================== RCS file: /home/cvs/xml-xalan/c/src/DOMSupport/TreeWalker.hpp,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- TreeWalker.hpp 20 Nov 2002 02:27:25 -0000 1.5 +++ TreeWalker.hpp 29 Jan 2003 05:22:38 -0000 1.6 @@ -2,7 +2,7 @@ * The Apache Software License, Version 1.1 * * - * Copyright (c) 1999-2002 The Apache Software Foundation. All rights + * Copyright (c) 1999-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -85,47 +85,87 @@ ~TreeWalker(); /** - * Perform a pre-order traversal non-recursive style. - * - * @param pos starting node $$$ + * Perform a document-order traversal. + * + * Derived classes and stop the traversal by returning + * true from startNode() or endNode(). If that happens, + * the function will return the next node in document + * order. If desired, the caller can start traversing + * the tree again from that point. Note that terminal + * nodes will always have startNode() and endNode() + * called before the traversal terminates. + * + * @param pos The node in the tree with which to start the walk + * + * @return 0 if the traversal completes, or the next node if the traversal doesn't complete. */ - - virtual void + const XalanNode* traverse(const XalanNode* pos); /** - * Perform a pre-order traversal non-recursive style. - * - * @param pos starting node $$$ + * Perform a document-order traversal. + * + * Derived classes and stop the traversal by returning + * true from startNode() or endNode(). If that happens, + * the function will return the next node in document + * order. If desired, the caller can start traversing + * the tree again from that point. Note that terminal + * nodes will always have startNode() and endNode() + * called before the traversal terminates. + * + * @param pos The node in the tree with which to start the walk + * + * @return 0 if the traversal completes, or the next node if the traversal doesn't complete. */ - - virtual void + XalanNode* traverse(XalanNode* pos); /** - * Perform a pre-order traversal non-recursive style. - * - * @param pos starting node $$$ - * @param parent parent node $$$ + * Perform a document-order traversal stopping at the + * provided parent node. + * + * Derived classes and stop the traversal by returning + * true from startNode() or endNode(). If that happens, + * the function will return the next node in document + * order. If desired, the caller can start traversing + * the tree again from that point. Note that terminal + * nodes will always have startNode() and endNode() + * called before the traversal terminates. + * + * @param pos The node in the tree with which to start the walk + * @param parent The parent of pos. Note that for multiple calls that continue the traversal, this node must remain the same. + * + * @return parent if the traversal completes, or the next node if the traversal doesn't complete. */ - virtual void + const XalanNode* traverse( const XalanNode* pos, const XalanNode* parent); /** - * Perform a pre-order traversal non-recursive style. - * - * @param pos starting node $$$ - * @param parent parent node $$$ + * Perform a document-order traversal stopping at the + * provided parent node. + * + * Derived classes and stop the traversal by returning + * true from startNode() or endNode(). If that happens, + * the function will return the next node in document + * order. If desired, the caller can start traversing + * the tree again from that point. Note that terminal + * nodes will always have startNode() and endNode() + * called before the traversal terminates. + * + * @param pos The node in the tree with which to start the walk + * @param parent The parent of pos. Note that for multiple calls that continue the traversal, this node must remain the same. + * + * @return parent if the traversal completes, or the next node if the traversal doesn't complete. */ - virtual void + XalanNode* traverse( XalanNode* pos, XalanNode* parent); /** - * Perform a pre-order traversal non-recursive style. + * Perform a pre-order traversal. * * @param pos starting node */ @@ -133,7 +173,7 @@ traverseSubtree(const XalanNode* pos); /** - * Perform a pre-order traversal non-recursive style. + * Perform a pre-order traversal. * * @param pos starting node */ @@ -142,16 +182,44 @@ protected: - virtual void + /** + * Called when first walking a node + * + * @param node The node + * + * @return return false if the walk should continue, or true if it should not. + */ + virtual bool startNode(const XalanNode* node) = 0; - virtual void + /** + * Called when first walking a node + * + * @param node The node + * + * @return return false if the walk should continue, or true if it should not. + */ + virtual bool startNode(XalanNode* node) = 0; - virtual void + /** + * Called when leaving a node + * + * @param node The node + * + * @return return false if the walk should continue, or true if it should not. + */ + virtual bool endNode(const XalanNode* node) = 0; - virtual void + /** + * Called when leaving a node + * + * @param node The node + * + * @return return false if the walk should continue, or true if it should not. + */ + virtual bool endNode(XalanNode* node) = 0; private: 1.5 +61 -17 xml-xalan/c/src/DOMSupport/TreeWalker.cpp Index: TreeWalker.cpp =================================================================== RCS file: /home/cvs/xml-xalan/c/src/DOMSupport/TreeWalker.cpp,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- TreeWalker.cpp 20 Nov 2002 02:27:25 -0000 1.4 +++ TreeWalker.cpp 29 Jan 2003 05:22:38 -0000 1.5 @@ -2,7 +2,7 @@ * The Apache Software License, Version 1.1 * * - * Copyright (c) 1999-2002 The Apache Software Foundation. All rights + * Copyright (c) 1999-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -83,22 +83,31 @@ -void +const XalanNode* TreeWalker::traverse(const XalanNode* pos) { assert(pos != 0); const XalanNode* thePos = pos; - while(0 != thePos) + bool fStop = false; + + while(0 != thePos && fStop == false) { - startNode(thePos); + fStop = startNode(thePos); const XalanNode* nextNode = thePos->getFirstChild(); while(0 == nextNode) { - endNode(thePos); + if (fStop == false) + { + fStop = endNode(thePos); + } + else + { + endNode(thePos); + } nextNode = thePos->getNextSibling(); @@ -117,26 +126,37 @@ thePos = nextNode; } + + return thePos; } -void +XalanNode* TreeWalker::traverse(XalanNode* pos) { assert(pos != 0); XalanNode* thePos = pos; - while(0 != thePos) + bool fStop = false; + + while(0 != thePos && fStop == false) { - startNode(thePos); + fStop = startNode(thePos); XalanNode* nextNode = thePos->getFirstChild(); while(0 == nextNode) { - endNode(thePos); + if (fStop == false) + { + fStop = endNode(thePos); + } + else + { + endNode(thePos); + } nextNode = thePos->getNextSibling(); @@ -155,11 +175,13 @@ thePos = nextNode; } + + return thePos; } -void +const XalanNode* TreeWalker::traverse( const XalanNode* pos, const XalanNode* parent) @@ -169,15 +191,24 @@ const XalanNode* thePos = pos; - while(parent != thePos) + bool fStop = false; + + while(parent != thePos && fStop == false) { - startNode(thePos); + fStop = startNode(thePos); const XalanNode* nextNode = thePos->getFirstChild(); while(0 == nextNode) { - endNode(thePos); + if (fStop == false) + { + fStop = endNode(thePos); + } + else + { + endNode(thePos); + } nextNode = thePos->getNextSibling(); @@ -196,11 +227,13 @@ thePos = nextNode; } + + return thePos; } -void +XalanNode* TreeWalker::traverse( XalanNode* pos, XalanNode* parent) @@ -210,15 +243,24 @@ XalanNode* thePos = pos; - while(parent != thePos) + bool fStop = false; + + while(parent != thePos && fStop == false) { - startNode(thePos); + fStop = startNode(thePos); XalanNode* nextNode = thePos->getFirstChild(); while(0 == nextNode) { - endNode(thePos); + if (fStop == false) + { + fStop = endNode(thePos); + } + else + { + endNode(thePos); + } nextNode = thePos->getNextSibling(); @@ -237,6 +279,8 @@ thePos = nextNode; } + + return thePos; }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]