2
// AbstractContainerNode.cpp
4
// $Id: //poco/1.2/XML/src/AbstractContainerNode.cpp#1 $
10
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
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:
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.
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.
37
#include "Poco/DOM/AbstractContainerNode.h"
38
#include "Poco/DOM/Document.h"
39
#include "Poco/DOM/DOMException.h"
46
AbstractContainerNode::AbstractContainerNode(Document* pOwnerDocument):
47
AbstractNode(pOwnerDocument),
53
AbstractContainerNode::AbstractContainerNode(Document* pOwnerDocument, const AbstractContainerNode& node):
54
AbstractNode(pOwnerDocument, node),
60
AbstractContainerNode::~AbstractContainerNode()
62
AbstractNode* pChild = static_cast<AbstractNode*>(_pFirstChild);
65
AbstractNode* pDelNode = pChild;
66
pChild = pChild->_pNext;
68
pDelNode->_pParent = 0;
74
Node* AbstractContainerNode::firstChild() const
80
Node* AbstractContainerNode::lastChild() const
82
AbstractNode* pChild = _pFirstChild;
85
while (pChild->_pNext) pChild = pChild->_pNext;
92
Node* AbstractContainerNode::insertBefore(Node* newChild, Node* refChild)
94
poco_check_ptr (newChild);
96
if (static_cast<AbstractNode*>(newChild)->_pOwner != _pOwner && static_cast<AbstractNode*>(newChild)->_pOwner != this)
97
throw DOMException(DOMException::WRONG_DOCUMENT_ERR);
98
if (refChild && static_cast<AbstractNode*>(refChild)->_pParent != this)
99
throw DOMException(DOMException::NOT_FOUND_ERR);
100
if (newChild == refChild)
102
if (this == newChild)
103
throw DOMException(DOMException::HIERARCHY_REQUEST_ERR);
105
AbstractNode* pFirst = 0;
106
AbstractNode* pLast = 0;
107
if (newChild->nodeType() == Node::DOCUMENT_FRAGMENT_NODE)
109
AbstractContainerNode* pFrag = static_cast<AbstractContainerNode*>(newChild);
110
pFirst = pFrag->_pFirstChild;
114
while (pLast->_pNext)
116
pLast->_pParent = this;
117
pLast = pLast->_pNext;
119
pLast->_pParent = this;
121
pFrag->_pFirstChild = 0;
125
newChild->duplicate();
126
AbstractContainerNode* pParent = static_cast<AbstractNode*>(newChild)->_pParent;
127
if (pParent) pParent->removeChild(newChild);
128
pFirst = static_cast<AbstractNode*>(newChild);
130
pFirst->_pParent = this;
132
if (_pFirstChild && pFirst)
134
AbstractNode* pCur = _pFirstChild;
135
if (pCur == refChild)
137
pLast->_pNext = _pFirstChild;
138
_pFirstChild = pFirst;
142
while (pCur && pCur->_pNext != refChild) pCur = pCur->_pNext;
145
pLast->_pNext = pCur->_pNext;
146
pCur->_pNext = pFirst;
148
else throw DOMException(DOMException::NOT_FOUND_ERR);
151
else _pFirstChild = pFirst;
155
while (pFirst && pFirst != pLast->_pNext)
157
pFirst->dispatchNodeInserted();
158
pFirst->dispatchNodeInsertedIntoDocument();
159
pFirst = pFirst->_pNext;
161
dispatchSubtreeModified();
167
Node* AbstractContainerNode::replaceChild(Node* newChild, Node* oldChild)
169
poco_check_ptr (newChild);
170
poco_check_ptr (oldChild);
172
if (static_cast<AbstractNode*>(newChild)->_pOwner != _pOwner && static_cast<AbstractNode*>(newChild)->_pOwner != this)
173
throw DOMException(DOMException::WRONG_DOCUMENT_ERR);
174
if (static_cast<AbstractNode*>(oldChild)->_pParent != this)
175
throw DOMException(DOMException::NOT_FOUND_ERR);
176
if (newChild == oldChild)
178
if (this == newChild)
179
throw DOMException(DOMException::HIERARCHY_REQUEST_ERR);
181
bool doEvents = events();
182
if (newChild->nodeType() == Node::DOCUMENT_FRAGMENT_NODE)
184
insertBefore(newChild, oldChild);
185
removeChild(oldChild);
189
AbstractContainerNode* pParent = static_cast<AbstractNode*>(newChild)->_pParent;
190
if (pParent) pParent->removeChild(newChild);
192
if (oldChild == _pFirstChild)
196
_pFirstChild->dispatchNodeRemoved();
197
_pFirstChild->dispatchNodeRemovedFromDocument();
199
static_cast<AbstractNode*>(newChild)->_pNext = static_cast<AbstractNode*>(oldChild)->_pNext;
200
static_cast<AbstractNode*>(newChild)->_pParent = this;
201
_pFirstChild->_pNext = 0;
202
_pFirstChild->_pParent = 0;
203
_pFirstChild = static_cast<AbstractNode*>(newChild);
206
static_cast<AbstractNode*>(newChild)->dispatchNodeInserted();
207
static_cast<AbstractNode*>(newChild)->dispatchNodeInsertedIntoDocument();
212
AbstractNode* pCur = _pFirstChild;
213
while (pCur && pCur->_pNext != oldChild) pCur = pCur->_pNext;
216
poco_assert_dbg (pCur->_pNext == oldChild);
220
static_cast<AbstractNode*>(oldChild)->dispatchNodeRemoved();
221
static_cast<AbstractNode*>(oldChild)->dispatchNodeRemovedFromDocument();
223
static_cast<AbstractNode*>(newChild)->_pNext = static_cast<AbstractNode*>(oldChild)->_pNext;
224
static_cast<AbstractNode*>(newChild)->_pParent = this;
225
static_cast<AbstractNode*>(oldChild)->_pNext = 0;
226
static_cast<AbstractNode*>(oldChild)->_pParent = 0;
227
pCur->_pNext = static_cast<AbstractNode*>(newChild);
230
static_cast<AbstractNode*>(newChild)->dispatchNodeInserted();
231
static_cast<AbstractNode*>(newChild)->dispatchNodeInsertedIntoDocument();
234
else throw DOMException(DOMException::NOT_FOUND_ERR);
236
newChild->duplicate();
237
oldChild->autoRelease();
239
if (doEvents) dispatchSubtreeModified();
244
Node* AbstractContainerNode::removeChild(Node* oldChild)
246
poco_check_ptr (oldChild);
248
bool doEvents = events();
249
if (oldChild == _pFirstChild)
253
static_cast<AbstractNode*>(oldChild)->dispatchNodeRemoved();
254
static_cast<AbstractNode*>(oldChild)->dispatchNodeRemovedFromDocument();
256
_pFirstChild = _pFirstChild->_pNext;
257
static_cast<AbstractNode*>(oldChild)->_pNext = 0;
258
static_cast<AbstractNode*>(oldChild)->_pParent = 0;
262
AbstractNode* pCur = _pFirstChild;
263
while (pCur && pCur->_pNext != oldChild) pCur = pCur->_pNext;
268
static_cast<AbstractNode*>(oldChild)->dispatchNodeRemoved();
269
static_cast<AbstractNode*>(oldChild)->dispatchNodeRemovedFromDocument();
271
pCur->_pNext = pCur->_pNext->_pNext;
272
static_cast<AbstractNode*>(oldChild)->_pNext = 0;
273
static_cast<AbstractNode*>(oldChild)->_pParent = 0;
275
else throw DOMException(DOMException::NOT_FOUND_ERR);
277
oldChild->autoRelease();
278
if (doEvents) dispatchSubtreeModified();
283
Node* AbstractContainerNode::appendChild(Node* newChild)
285
return insertBefore(newChild, 0);
289
void AbstractContainerNode::dispatchNodeRemovedFromDocument()
291
AbstractNode::dispatchNodeRemovedFromDocument();
292
Node* pChild = firstChild();
295
static_cast<AbstractNode*>(pChild)->dispatchNodeRemovedFromDocument();
296
pChild = pChild->nextSibling();
301
void AbstractContainerNode::dispatchNodeInsertedIntoDocument()
303
AbstractNode::dispatchNodeInsertedIntoDocument();
304
Node* pChild = firstChild();
307
static_cast<AbstractNode*>(pChild)->dispatchNodeInsertedIntoDocument();
308
pChild = pChild->nextSibling();
313
bool AbstractContainerNode::hasChildNodes() const
315
return _pFirstChild != 0;
319
bool AbstractContainerNode::hasAttributes() const
325
} } // namespace Poco::XML