1//
2// NodeAppender.cpp
3//
4// Library: XML
5// Package: DOM
6// Module: NodeAppender
7//
8// Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
9// and Contributors.
10//
11// SPDX-License-Identifier: BSL-1.0
12//
13
14
15#include "Poco/DOM/NodeAppender.h"
16#include "Poco/DOM/Element.h"
17#include "Poco/DOM/DOMException.h"
18
19
20namespace Poco {
21namespace XML {
22
23
24NodeAppender::NodeAppender(Element* parent):
25 _pParent(parent),
26 _pLast(0)
27{
28 poco_check_ptr (parent);
29
30 _pLast = static_cast<AbstractNode*>(_pParent->lastChild());
31}
32
33
34NodeAppender::~NodeAppender()
35{
36}
37
38
39void NodeAppender::appendChild(Node* newChild)
40{
41 poco_check_ptr (newChild);
42 poco_assert (_pLast == 0 || _pLast->_pNext == 0);
43
44 if (static_cast<AbstractNode*>(newChild)->_pOwner != _pParent->_pOwner)
45 throw DOMException(DOMException::WRONG_DOCUMENT_ERR);
46
47 if (newChild->nodeType() == Node::DOCUMENT_FRAGMENT_NODE)
48 {
49 AbstractContainerNode* pFrag = static_cast<AbstractContainerNode*>(newChild);
50 AbstractNode* pChild = pFrag->_pFirstChild;
51 if (pChild)
52 {
53 if (_pLast)
54 _pLast->_pNext = pChild;
55 else
56 _pParent->_pFirstChild = pChild;
57 while (pChild)
58 {
59 _pLast = pChild;
60 pChild->_pParent = _pParent;
61 pChild = pChild->_pNext;
62 }
63 pFrag->_pFirstChild = 0;
64 }
65 }
66 else
67 {
68 AbstractNode* pAN = static_cast<AbstractNode*>(newChild);
69 pAN->duplicate();
70 if (pAN->_pParent)
71 pAN->_pParent->removeChild(pAN);
72 pAN->_pParent = _pParent;
73 if (_pLast)
74 _pLast->_pNext = pAN;
75 else
76 _pParent->_pFirstChild = pAN;
77 _pLast = pAN;
78 }
79}
80
81
82} } // namespace Poco::XML
83