1//
2// AttrMap.cpp
3//
4// Library: XML
5// Package: DOM
6// Module: DOM
7//
8// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
9// and Contributors.
10//
11// SPDX-License-Identifier: BSL-1.0
12//
13
14
15#include "Poco/DOM/AttrMap.h"
16#include "Poco/DOM/Attr.h"
17#include "Poco/DOM/Element.h"
18#include "Poco/DOM/Document.h"
19#include "Poco/DOM/DOMException.h"
20
21
22namespace Poco {
23namespace XML {
24
25
26AttrMap::AttrMap(Element* pElement):
27 _pElement(pElement)
28{
29 poco_check_ptr (pElement);
30
31 _pElement->duplicate();
32}
33
34
35AttrMap::~AttrMap()
36{
37 _pElement->release();
38}
39
40
41Node* AttrMap::getNamedItem(const XMLString& name) const
42{
43 return _pElement->getAttributeNode(name);
44}
45
46
47Node* AttrMap::setNamedItem(Node* arg)
48{
49 poco_check_ptr (arg);
50
51 if (arg->nodeType() != Node::ATTRIBUTE_NODE)
52 throw DOMException(DOMException::HIERARCHY_REQUEST_ERR);
53
54 return _pElement->setAttributeNode(static_cast<Attr*>(arg));
55}
56
57
58Node* AttrMap::removeNamedItem(const XMLString& name)
59{
60 Attr* pAttr = _pElement->getAttributeNode(name);
61 if (pAttr)
62 return _pElement->removeAttributeNode(pAttr);
63 else
64 return 0;
65}
66
67
68Node* AttrMap::item(unsigned long index) const
69{
70 AbstractNode* pAttr = _pElement->_pFirstAttr;
71 while (index-- > 0 && pAttr) pAttr = static_cast<AbstractNode*>(pAttr->nextSibling());
72 return pAttr;
73}
74
75
76unsigned long AttrMap::length() const
77{
78 unsigned long result = 0;
79 AbstractNode* pAttr = _pElement->_pFirstAttr;
80 while (pAttr)
81 {
82 pAttr = static_cast<AbstractNode*>(pAttr->nextSibling());
83 ++result;
84 }
85 return result;
86}
87
88
89Node* AttrMap::getNamedItemNS(const XMLString& namespaceURI, const XMLString& localName) const
90{
91 return _pElement->getAttributeNodeNS(namespaceURI, localName);
92}
93
94
95Node* AttrMap::setNamedItemNS(Node* arg)
96{
97 poco_check_ptr (arg);
98
99 if (arg->nodeType() != Node::ATTRIBUTE_NODE)
100 throw DOMException(DOMException::HIERARCHY_REQUEST_ERR);
101
102 return _pElement->setAttributeNodeNS(static_cast<Attr*>(arg));
103}
104
105
106Node* AttrMap::removeNamedItemNS(const XMLString& namespaceURI, const XMLString& localName)
107{
108 Attr* pAttr = _pElement->getAttributeNodeNS(namespaceURI, localName);
109 if (pAttr)
110 return _pElement->removeAttributeNode(pAttr);
111 else
112 return 0;
113}
114
115
116void AttrMap::autoRelease()
117{
118 _pElement->ownerDocument()->autoReleasePool().add(this);
119}
120
121
122} } // namespace Poco::XML
123
124