1 | // |
---|
2 | // TreeWalker.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/XML/include/Poco/DOM/TreeWalker.h#1 $ |
---|
5 | // |
---|
6 | // Library: XML |
---|
7 | // Package: DOM |
---|
8 | // Module: TreeWalker |
---|
9 | // |
---|
10 | // Definition of the DOM TreeWalker class. |
---|
11 | // |
---|
12 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef DOM_TreeWalker_INCLUDED |
---|
40 | #define DOM_TreeWalker_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/XML/XML.h" |
---|
44 | #include "Poco/XML/XMLString.h" |
---|
45 | |
---|
46 | |
---|
47 | namespace Poco { |
---|
48 | namespace XML { |
---|
49 | |
---|
50 | |
---|
51 | class Node; |
---|
52 | class NodeFilter; |
---|
53 | |
---|
54 | |
---|
55 | class XML_API TreeWalker |
---|
56 | /// TreeWalker objects are used to navigate a document tree or subtree using |
---|
57 | /// the view of the document defined by their whatToShow flags and filter (if |
---|
58 | /// any). Any function which performs navigation using a TreeWalker will automatically |
---|
59 | /// support any view defined by a TreeWalker. |
---|
60 | /// |
---|
61 | /// Omitting nodes from the logical view of a subtree can result in a structure |
---|
62 | /// that is substantially different from the same subtree in the complete, unfiltered |
---|
63 | /// document. Nodes that are siblings in the TreeWalker view may be children |
---|
64 | /// of different, widely separated nodes in the original view. For instance, |
---|
65 | /// consider a NodeFilter that skips all nodes except for Text nodes and the |
---|
66 | /// root node of a document. In the logical view that results, all text nodes |
---|
67 | /// will be siblings and appear as direct children of the root node, no matter |
---|
68 | /// how deeply nested the structure of the original document. |
---|
69 | /// |
---|
70 | /// A TreeWalker can be directly instantiated using one of its constructors - |
---|
71 | /// the DocumentTraversal interface is not needed and therefore not implemented. |
---|
72 | /// Unlike most other DOM classes, TreeWalker supports value semantics. |
---|
73 | /// |
---|
74 | /// If the TreeWalker's current node is removed from the document, the |
---|
75 | /// result of calling any of the movement methods is undefined. This behavior |
---|
76 | /// does not conform to the DOM Level 2 Traversal specification. |
---|
77 | { |
---|
78 | public: |
---|
79 | TreeWalker(Node* root, unsigned long whatToShow, NodeFilter* pFilter = 0); |
---|
80 | /// Creates a TreeWalker over the subtree rooted at the specified node. |
---|
81 | |
---|
82 | TreeWalker(const TreeWalker& walker); |
---|
83 | /// Creates a TreeWalker by copying another NodeIterator. |
---|
84 | |
---|
85 | TreeWalker& operator = (const TreeWalker& walker); |
---|
86 | /// Assignment operator. |
---|
87 | |
---|
88 | ~TreeWalker(); |
---|
89 | /// Destroys the TreeWalker. |
---|
90 | |
---|
91 | Node* root() const; |
---|
92 | /// The root node of the TreeWalker, as specified when it was created. |
---|
93 | |
---|
94 | unsigned long whatToShow() const; |
---|
95 | /// This attribute determines which node types are presented via the TreeWalker. |
---|
96 | /// The available set of constants is defined in the NodeFilter interface. Nodes |
---|
97 | /// not accepted by whatToShow will be skipped, but their children may still |
---|
98 | /// be considered. Note that this skip takes precedence over the filter, if |
---|
99 | /// any. |
---|
100 | |
---|
101 | NodeFilter* filter() const; |
---|
102 | /// The NodeFilter used to screen nodes. |
---|
103 | |
---|
104 | bool expandEntityReferences() const; |
---|
105 | /// The value of this flag determines whether the children of entity reference |
---|
106 | /// nodes are visible to the iterator. If false, they and their descendants |
---|
107 | /// will be rejected. Note that this rejection takes precedence over whatToShow |
---|
108 | /// and the filter. Also note that this is currently the only situation where |
---|
109 | /// NodeIterators may reject a complete subtree rather than skipping individual |
---|
110 | /// nodes. |
---|
111 | /// |
---|
112 | /// To produce a view of the document that has entity references expanded and |
---|
113 | /// does not expose the entity reference node itself, use the whatToShow flags |
---|
114 | /// to hide the entity reference node and set expandEntityReferences to true |
---|
115 | /// when creating the iterator. To produce a view of the document that has entity |
---|
116 | /// reference nodes but no entity expansion, use the whatToShow flags to show |
---|
117 | /// the entity reference node and set expandEntityReferences to false. |
---|
118 | /// |
---|
119 | /// This implementation does not support entity reference expansion and |
---|
120 | /// thus always returns false. |
---|
121 | |
---|
122 | Node* currentNode() const; |
---|
123 | /// The node at which the TreeWalker is currently positioned. |
---|
124 | /// Alterations to the DOM tree may cause the current node to no longer be accepted |
---|
125 | /// by the TreeWalker's associated filter. currentNode may also be explicitly |
---|
126 | /// set to any node, whether or not it is within the subtree specified by the |
---|
127 | /// root node or would be accepted by the filter and whatToShow flags. Further |
---|
128 | /// traversal occurs relative to currentNode even if it is not part of the current |
---|
129 | /// view, by applying the filters in the requested direction; if no traversal |
---|
130 | /// is possible, currentNode is not changed. |
---|
131 | |
---|
132 | Node* getCurrentNode() const; |
---|
133 | /// See currentNode(). |
---|
134 | |
---|
135 | void setCurrentNode(Node* pNode); |
---|
136 | /// Sets the current node. |
---|
137 | |
---|
138 | Node* parentNode(); |
---|
139 | /// Moves to and returns the closest visible ancestor node of the current node. |
---|
140 | /// If the search for parentNode attempts to step upward from the TreeWalker's |
---|
141 | /// root node, or if it fails to find a visible ancestor node, this method retains |
---|
142 | /// the current position and returns null. |
---|
143 | |
---|
144 | Node* firstChild(); |
---|
145 | /// Moves the TreeWalker to the first visible child of the current node, and |
---|
146 | /// returns the new node. If the current node has no visible children, returns |
---|
147 | /// null, and retains the current node. |
---|
148 | |
---|
149 | Node* lastChild(); |
---|
150 | /// Moves the TreeWalker to the last visible child of the current node, and |
---|
151 | /// returns the new node. If the current node has no visible children, returns |
---|
152 | /// null, and retains the current node. |
---|
153 | |
---|
154 | Node* previousSibling(); |
---|
155 | /// Moves the TreeWalker to the previous sibling of the current node, and returns |
---|
156 | /// the new node. If the current node has no visible previous sibling, returns |
---|
157 | /// null, and retains the current node. |
---|
158 | |
---|
159 | Node* nextSibling(); |
---|
160 | /// Moves the TreeWalker to the next sibling of the current node, and returns |
---|
161 | /// the new node. If the current node has no visible next sibling, returns null, |
---|
162 | /// and retains the current node. |
---|
163 | |
---|
164 | Node* previousNode(); |
---|
165 | /// Moves the TreeWalker to the previous visible node in document order relative |
---|
166 | /// to the current node, and returns the new node. If the current node has no |
---|
167 | /// previous node, or if the search for previousNode attempts to step upward |
---|
168 | /// from the TreeWalker's root node, returns null, and retains the current node. |
---|
169 | |
---|
170 | Node* nextNode(); |
---|
171 | /// Moves the TreeWalker to the next visible node in document order relative |
---|
172 | /// to the current node, and returns the new node. If the current node has no |
---|
173 | /// next node, or if the search for nextNode attempts to step upward from the |
---|
174 | /// TreeWalker's root node, returns null, and retains the current node. |
---|
175 | |
---|
176 | protected: |
---|
177 | int accept(Node* pNode) const; |
---|
178 | Node* next(Node* pNode) const; |
---|
179 | Node* previous(Node* pNode) const; |
---|
180 | |
---|
181 | private: |
---|
182 | TreeWalker(); |
---|
183 | |
---|
184 | Node* _pRoot; |
---|
185 | unsigned long _whatToShow; |
---|
186 | NodeFilter* _pFilter; |
---|
187 | Node* _pCurrent; |
---|
188 | }; |
---|
189 | |
---|
190 | |
---|
191 | // |
---|
192 | // inlines |
---|
193 | // |
---|
194 | inline Node* TreeWalker::root() const |
---|
195 | { |
---|
196 | return _pRoot; |
---|
197 | } |
---|
198 | |
---|
199 | |
---|
200 | inline unsigned long TreeWalker::whatToShow() const |
---|
201 | { |
---|
202 | return _whatToShow; |
---|
203 | } |
---|
204 | |
---|
205 | |
---|
206 | inline NodeFilter* TreeWalker::filter() const |
---|
207 | { |
---|
208 | return _pFilter; |
---|
209 | } |
---|
210 | |
---|
211 | |
---|
212 | inline bool TreeWalker::expandEntityReferences() const |
---|
213 | { |
---|
214 | return false; |
---|
215 | } |
---|
216 | |
---|
217 | |
---|
218 | inline Node* TreeWalker::currentNode() const |
---|
219 | { |
---|
220 | return _pCurrent; |
---|
221 | } |
---|
222 | |
---|
223 | |
---|
224 | inline Node* TreeWalker::getCurrentNode() const |
---|
225 | { |
---|
226 | return _pCurrent; |
---|
227 | } |
---|
228 | |
---|
229 | |
---|
230 | } } // namespace Poco::XML |
---|
231 | |
---|
232 | |
---|
233 | #endif // DOM_TreeWalker_INCLUDED |
---|