1 | // |
---|
2 | // NodeIterator.cpp |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/XML/src/NodeIterator.cpp#1 $ |
---|
5 | // |
---|
6 | // Library: XML |
---|
7 | // Package: DOM |
---|
8 | // Module: NodeIterator |
---|
9 | // |
---|
10 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
11 | // and Contributors. |
---|
12 | // |
---|
13 | // Permission is hereby granted, free of charge, to any person or organization |
---|
14 | // obtaining a copy of the software and accompanying documentation covered by |
---|
15 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
16 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
17 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
18 | // do so, all subject to the following: |
---|
19 | // |
---|
20 | // The copyright notices in the Software and this entire statement, including |
---|
21 | // the above license grant, this restriction and the following disclaimer, |
---|
22 | // must be included in all copies of the Software, in whole or in part, and |
---|
23 | // all derivative works of the Software, unless such copies or derivative |
---|
24 | // works are solely in the form of machine-executable object code generated by |
---|
25 | // a source language processor. |
---|
26 | // |
---|
27 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
28 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
29 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
30 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
31 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
32 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
33 | // DEALINGS IN THE SOFTWARE. |
---|
34 | // |
---|
35 | |
---|
36 | |
---|
37 | #include <Poco/DOM/NodeIterator.h> |
---|
38 | #include <Poco/DOM/AbstractNode.h> |
---|
39 | #include <Poco/DOM/NodeFilter.h> |
---|
40 | #include <Poco/DOM/DOMException.h> |
---|
41 | |
---|
42 | |
---|
43 | namespace Poco { |
---|
44 | namespace XML { |
---|
45 | |
---|
46 | |
---|
47 | NodeIterator::NodeIterator(Node* root, unsigned long whatToShow, NodeFilter* pFilter): |
---|
48 | _pRoot(root), |
---|
49 | _whatToShow(whatToShow), |
---|
50 | _pFilter(pFilter), |
---|
51 | _pCurrent(0) |
---|
52 | { |
---|
53 | } |
---|
54 | |
---|
55 | |
---|
56 | NodeIterator::NodeIterator(const NodeIterator& iterator): |
---|
57 | _pRoot(iterator._pRoot), |
---|
58 | _whatToShow(iterator._whatToShow), |
---|
59 | _pFilter(iterator._pFilter), |
---|
60 | _pCurrent(iterator._pCurrent) |
---|
61 | { |
---|
62 | } |
---|
63 | |
---|
64 | |
---|
65 | NodeIterator& NodeIterator::operator = (const NodeIterator& iterator) |
---|
66 | { |
---|
67 | if (&iterator != this) |
---|
68 | { |
---|
69 | _pRoot = iterator._pRoot; |
---|
70 | _whatToShow = iterator._whatToShow; |
---|
71 | _pFilter = iterator._pFilter; |
---|
72 | _pCurrent = iterator._pCurrent; |
---|
73 | } |
---|
74 | return *this; |
---|
75 | } |
---|
76 | |
---|
77 | |
---|
78 | NodeIterator::~NodeIterator() |
---|
79 | { |
---|
80 | } |
---|
81 | |
---|
82 | |
---|
83 | Node* NodeIterator::nextNode() |
---|
84 | { |
---|
85 | if (!_pRoot) throw DOMException(DOMException::INVALID_STATE_ERR); |
---|
86 | |
---|
87 | if (_pCurrent) |
---|
88 | _pCurrent = next(); |
---|
89 | else |
---|
90 | _pCurrent = _pRoot; |
---|
91 | while (_pCurrent && !accept(_pCurrent)) |
---|
92 | _pCurrent = next(); |
---|
93 | return _pCurrent; |
---|
94 | } |
---|
95 | |
---|
96 | |
---|
97 | Node* NodeIterator::previousNode() |
---|
98 | { |
---|
99 | if (!_pRoot) throw DOMException(DOMException::INVALID_STATE_ERR); |
---|
100 | |
---|
101 | if (_pCurrent) |
---|
102 | _pCurrent = previous(); |
---|
103 | else |
---|
104 | _pCurrent = last(); |
---|
105 | while (_pCurrent && !accept(_pCurrent)) |
---|
106 | _pCurrent = previous(); |
---|
107 | return _pCurrent; |
---|
108 | } |
---|
109 | |
---|
110 | |
---|
111 | void NodeIterator::detach() |
---|
112 | { |
---|
113 | _pRoot = 0; |
---|
114 | _pCurrent = 0; |
---|
115 | } |
---|
116 | |
---|
117 | |
---|
118 | bool NodeIterator::accept(Node* pNode) const |
---|
119 | { |
---|
120 | bool accept = false; |
---|
121 | switch (pNode->nodeType()) |
---|
122 | { |
---|
123 | case Node::ELEMENT_NODE: |
---|
124 | accept = (_whatToShow & NodeFilter::SHOW_ELEMENT) != 0; break; |
---|
125 | case Node::ATTRIBUTE_NODE: |
---|
126 | accept = (_whatToShow & NodeFilter::SHOW_ATTRIBUTE) != 0; break; |
---|
127 | case Node::TEXT_NODE: |
---|
128 | accept = (_whatToShow & NodeFilter::SHOW_TEXT) != 0; break; |
---|
129 | case Node::CDATA_SECTION_NODE: |
---|
130 | accept = (_whatToShow & NodeFilter::SHOW_CDATA_SECTION) != 0; break; |
---|
131 | case Node::ENTITY_REFERENCE_NODE: |
---|
132 | accept = (_whatToShow & NodeFilter::SHOW_ENTITY_REFERENCE) != 0; break; |
---|
133 | case Node::ENTITY_NODE: |
---|
134 | accept = (_whatToShow & NodeFilter::SHOW_ENTITY) != 0; break; |
---|
135 | case Node::PROCESSING_INSTRUCTION_NODE: |
---|
136 | accept = (_whatToShow & NodeFilter::SHOW_PROCESSING_INSTRUCTION) != 0; break; |
---|
137 | case Node::COMMENT_NODE: |
---|
138 | accept = (_whatToShow & NodeFilter::SHOW_COMMENT) != 0; break; |
---|
139 | case Node::DOCUMENT_NODE: |
---|
140 | accept = (_whatToShow & NodeFilter::SHOW_DOCUMENT) != 0; break; |
---|
141 | case Node::DOCUMENT_TYPE_NODE: |
---|
142 | accept = (_whatToShow & NodeFilter::SHOW_DOCUMENT_TYPE) != 0; break; |
---|
143 | case Node::DOCUMENT_FRAGMENT_NODE: |
---|
144 | accept = (_whatToShow & NodeFilter::SHOW_DOCUMENT_FRAGMENT) != 0; break; |
---|
145 | case Node::NOTATION_NODE: |
---|
146 | accept = (_whatToShow & NodeFilter::SHOW_NOTATION) != 0; break; |
---|
147 | } |
---|
148 | if (accept && _pFilter) |
---|
149 | accept = _pFilter->acceptNode(pNode) == NodeFilter::FILTER_ACCEPT; |
---|
150 | return accept; |
---|
151 | } |
---|
152 | |
---|
153 | |
---|
154 | Node* NodeIterator::next() const |
---|
155 | { |
---|
156 | Node* pNext = _pCurrent->firstChild(); |
---|
157 | if (pNext) return pNext; |
---|
158 | pNext = _pCurrent; |
---|
159 | while (pNext && pNext != _pRoot) |
---|
160 | { |
---|
161 | Node* pSibling = pNext->nextSibling(); |
---|
162 | if (pSibling) return pSibling; |
---|
163 | pNext = pNext->parentNode(); |
---|
164 | } |
---|
165 | return 0; |
---|
166 | } |
---|
167 | |
---|
168 | |
---|
169 | Node* NodeIterator::previous() const |
---|
170 | { |
---|
171 | if (_pCurrent == _pRoot) return 0; |
---|
172 | Node* pPrev = _pCurrent->previousSibling(); |
---|
173 | while (pPrev) |
---|
174 | { |
---|
175 | Node* pLastChild = pPrev->lastChild(); |
---|
176 | if (pLastChild) |
---|
177 | pPrev = pLastChild; |
---|
178 | else |
---|
179 | return pPrev; |
---|
180 | } |
---|
181 | return _pCurrent->parentNode(); |
---|
182 | } |
---|
183 | |
---|
184 | |
---|
185 | Node* NodeIterator::last() |
---|
186 | { |
---|
187 | _pCurrent = _pRoot; |
---|
188 | Node* pLast = 0; |
---|
189 | while (_pCurrent) |
---|
190 | { |
---|
191 | pLast = _pCurrent; |
---|
192 | _pCurrent = next(); |
---|
193 | } |
---|
194 | return pLast; |
---|
195 | } |
---|
196 | |
---|
197 | |
---|
198 | } } // namespace Poco::XML |
---|