TreeWalker.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //
  2. // TreeWalker.h
  3. //
  4. // Library: XML
  5. // Package: DOM
  6. // Module: TreeWalker
  7. //
  8. // Definition of the DOM TreeWalker class.
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef DOM_TreeWalker_INCLUDED
  16. #define DOM_TreeWalker_INCLUDED
  17. #include "Poco/XML/XML.h"
  18. #include "Poco/XML/XMLString.h"
  19. namespace Poco {
  20. namespace XML {
  21. class Node;
  22. class NodeFilter;
  23. class XML_API TreeWalker
  24. /// TreeWalker objects are used to navigate a document tree or subtree using
  25. /// the view of the document defined by their whatToShow flags and filter (if
  26. /// any). Any function which performs navigation using a TreeWalker will automatically
  27. /// support any view defined by a TreeWalker.
  28. ///
  29. /// Omitting nodes from the logical view of a subtree can result in a structure
  30. /// that is substantially different from the same subtree in the complete, unfiltered
  31. /// document. Nodes that are siblings in the TreeWalker view may be children
  32. /// of different, widely separated nodes in the original view. For instance,
  33. /// consider a NodeFilter that skips all nodes except for Text nodes and the
  34. /// root node of a document. In the logical view that results, all text nodes
  35. /// will be siblings and appear as direct children of the root node, no matter
  36. /// how deeply nested the structure of the original document.
  37. ///
  38. /// A TreeWalker can be directly instantiated using one of its constructors -
  39. /// the DocumentTraversal interface is not needed and therefore not implemented.
  40. /// Unlike most other DOM classes, TreeWalker supports value semantics.
  41. ///
  42. /// If the TreeWalker's current node is removed from the document, the
  43. /// result of calling any of the movement methods is undefined. This behavior
  44. /// does not conform to the DOM Level 2 Traversal specification.
  45. {
  46. public:
  47. TreeWalker(Node* root, unsigned long whatToShow, NodeFilter* pFilter = 0);
  48. /// Creates a TreeWalker over the subtree rooted at the specified node.
  49. TreeWalker(const TreeWalker& walker);
  50. /// Creates a TreeWalker by copying another TreeWalker.
  51. TreeWalker& operator = (const TreeWalker& walker);
  52. /// Assignment operator.
  53. ~TreeWalker();
  54. /// Destroys the TreeWalker.
  55. Node* root() const;
  56. /// The root node of the TreeWalker, as specified when it was created.
  57. unsigned long whatToShow() const;
  58. /// This attribute determines which node types are presented via the TreeWalker.
  59. /// The available set of constants is defined in the NodeFilter interface. Nodes
  60. /// not accepted by whatToShow will be skipped, but their children may still
  61. /// be considered. Note that this skip takes precedence over the filter, if
  62. /// any.
  63. NodeFilter* filter() const;
  64. /// The NodeFilter used to screen nodes.
  65. bool expandEntityReferences() const;
  66. /// The value of this flag determines whether the children of entity reference
  67. /// nodes are visible to the iterator. If false, they and their descendants
  68. /// will be rejected. Note that this rejection takes precedence over whatToShow
  69. /// and the filter. Also note that this is currently the only situation where
  70. /// NodeIterators may reject a complete subtree rather than skipping individual
  71. /// nodes.
  72. ///
  73. /// To produce a view of the document that has entity references expanded and
  74. /// does not expose the entity reference node itself, use the whatToShow flags
  75. /// to hide the entity reference node and set expandEntityReferences to true
  76. /// when creating the iterator. To produce a view of the document that has entity
  77. /// reference nodes but no entity expansion, use the whatToShow flags to show
  78. /// the entity reference node and set expandEntityReferences to false.
  79. ///
  80. /// This implementation does not support entity reference expansion and
  81. /// thus always returns false.
  82. Node* currentNode() const;
  83. /// The node at which the TreeWalker is currently positioned.
  84. /// Alterations to the DOM tree may cause the current node to no longer be accepted
  85. /// by the TreeWalker's associated filter. currentNode may also be explicitly
  86. /// set to any node, whether or not it is within the subtree specified by the
  87. /// root node or would be accepted by the filter and whatToShow flags. Further
  88. /// traversal occurs relative to currentNode even if it is not part of the current
  89. /// view, by applying the filters in the requested direction; if no traversal
  90. /// is possible, currentNode is not changed.
  91. Node* getCurrentNode() const;
  92. /// See currentNode().
  93. void setCurrentNode(Node* pNode);
  94. /// Sets the current node.
  95. Node* parentNode();
  96. /// Moves to and returns the closest visible ancestor node of the current node.
  97. /// If the search for parentNode attempts to step upward from the TreeWalker's
  98. /// root node, or if it fails to find a visible ancestor node, this method retains
  99. /// the current position and returns null.
  100. Node* firstChild();
  101. /// Moves the TreeWalker to the first visible child of the current node, and
  102. /// returns the new node. If the current node has no visible children, returns
  103. /// null, and retains the current node.
  104. Node* lastChild();
  105. /// Moves the TreeWalker to the last visible child of the current node, and
  106. /// returns the new node. If the current node has no visible children, returns
  107. /// null, and retains the current node.
  108. Node* previousSibling();
  109. /// Moves the TreeWalker to the previous sibling of the current node, and returns
  110. /// the new node. If the current node has no visible previous sibling, returns
  111. /// null, and retains the current node.
  112. Node* nextSibling();
  113. /// Moves the TreeWalker to the next sibling of the current node, and returns
  114. /// the new node. If the current node has no visible next sibling, returns null,
  115. /// and retains the current node.
  116. Node* previousNode();
  117. /// Moves the TreeWalker to the previous visible node in document order relative
  118. /// to the current node, and returns the new node. If the current node has no
  119. /// previous node, or if the search for previousNode attempts to step upward
  120. /// from the TreeWalker's root node, returns null, and retains the current node.
  121. Node* nextNode();
  122. /// Moves the TreeWalker to the next visible node in document order relative
  123. /// to the current node, and returns the new node. If the current node has no
  124. /// next node, or if the search for nextNode attempts to step upward from the
  125. /// TreeWalker's root node, returns null, and retains the current node.
  126. protected:
  127. int accept(Node* pNode) const;
  128. Node* next(Node* pNode) const;
  129. Node* previous(Node* pNode) const;
  130. private:
  131. TreeWalker();
  132. Node* _pRoot;
  133. unsigned long _whatToShow;
  134. NodeFilter* _pFilter;
  135. Node* _pCurrent;
  136. };
  137. //
  138. // inlines
  139. //
  140. inline Node* TreeWalker::root() const
  141. {
  142. return _pRoot;
  143. }
  144. inline unsigned long TreeWalker::whatToShow() const
  145. {
  146. return _whatToShow;
  147. }
  148. inline NodeFilter* TreeWalker::filter() const
  149. {
  150. return _pFilter;
  151. }
  152. inline bool TreeWalker::expandEntityReferences() const
  153. {
  154. return false;
  155. }
  156. inline Node* TreeWalker::currentNode() const
  157. {
  158. return _pCurrent;
  159. }
  160. inline Node* TreeWalker::getCurrentNode() const
  161. {
  162. return _pCurrent;
  163. }
  164. } } // namespace Poco::XML
  165. #endif // DOM_TreeWalker_INCLUDED