1//
2// AttributesImpl.cpp
3//
4// Library: XML
5// Package: SAX
6// Module: SAX
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/SAX/AttributesImpl.h"
16
17
18namespace Poco {
19namespace XML {
20
21
22AttributesImpl::AttributesImpl()
23{
24 _empty.specified = false;
25 _empty.type = XML_LIT("CDATA");
26}
27
28
29AttributesImpl::AttributesImpl(const Attributes& attributes)
30{
31 _empty.specified = false;
32 _empty.type = XML_LIT("CDATA");
33 setAttributes(attributes);
34}
35
36
37AttributesImpl::AttributesImpl(const AttributesImpl& attributes):
38 _attributes(attributes._attributes),
39 _empty(attributes._empty)
40{
41}
42
43
44AttributesImpl::~AttributesImpl()
45{
46}
47
48
49AttributesImpl& AttributesImpl::operator = (const AttributesImpl& attributes)
50{
51 if (&attributes != this)
52 {
53 _attributes = attributes._attributes;
54 }
55 return *this;
56}
57
58
59int AttributesImpl::getIndex(const XMLString& qname) const
60{
61 int i = 0;
62 AttributeVec::const_iterator it;
63 for (it = _attributes.begin(); it != _attributes.end(); ++it)
64 {
65 if (it->qname == qname) return i;
66 ++i;
67 }
68 return -1;
69}
70
71
72int AttributesImpl::getIndex(const XMLString& namespaceURI, const XMLString& localName) const
73{
74 int i = 0;
75 AttributeVec::const_iterator it;
76 for (it = _attributes.begin(); it != _attributes.end(); ++it)
77 {
78 if (it->namespaceURI == namespaceURI && it->localName == localName) return i;
79 ++i;
80 }
81 return -1;
82}
83
84
85void AttributesImpl::setValue(int i, const XMLString& value)
86{
87 poco_assert (0 <= i && i < static_cast<int>(_attributes.size()));
88 _attributes[i].value = value;
89 _attributes[i].specified = true;
90}
91
92
93void AttributesImpl::setValue(const XMLString& qname, const XMLString& value)
94{
95 Attribute* pAttr = find(qname);
96 if (pAttr)
97 {
98 pAttr->value = value;
99 pAttr->specified = true;
100 }
101}
102
103
104void AttributesImpl::setValue(const XMLString& namespaceURI, const XMLString& localName, const XMLString& value)
105{
106 Attribute* pAttr = find(namespaceURI, localName);
107 if (pAttr)
108 {
109 pAttr->value = value;
110 pAttr->specified = true;
111 }
112}
113
114
115void AttributesImpl::setAttributes(const Attributes& attributes)
116{
117 if (&attributes != this)
118 {
119 int count = attributes.getLength();
120 _attributes.clear();
121 _attributes.reserve(count);
122 for (int i = 0; i < count; i++)
123 {
124 addAttribute(attributes.getURI(i), attributes.getLocalName(i), attributes.getQName(i), attributes.getType(i), attributes.getValue(i));
125 }
126 }
127}
128
129
130void AttributesImpl::setAttribute(int i, const XMLString& namespaceURI, const XMLString& localName, const XMLString& qname, const XMLString& type, const XMLString& value)
131{
132 poco_assert (0 <= i && i < static_cast<int>(_attributes.size()));
133 _attributes[i].namespaceURI = namespaceURI;
134 _attributes[i].localName = localName;
135 _attributes[i].qname = qname;
136 _attributes[i].type = type;
137 _attributes[i].value = value;
138 _attributes[i].specified = true;
139}
140
141
142void AttributesImpl::addAttribute(const XMLString& namespaceURI, const XMLString& localName, const XMLString& qname, const XMLString& type, const XMLString& value)
143{
144 AttributeVec::iterator it = _attributes.insert(_attributes.end(), Attribute());
145 it->namespaceURI = namespaceURI;
146 it->localName = localName;
147 it->qname = qname;
148 it->value = value;
149 it->type = type;
150 it->specified = true;
151}
152
153
154void AttributesImpl::addAttribute(const XMLString& namespaceURI, const XMLString& localName, const XMLString& qname, const XMLString& type, const XMLString& value, bool specified)
155{
156 AttributeVec::iterator it = _attributes.insert(_attributes.end(), Attribute());
157 it->namespaceURI = namespaceURI;
158 it->localName = localName;
159 it->qname = qname;
160 it->value = value;
161 it->type = type;
162 it->specified = specified;
163}
164
165
166void AttributesImpl::addAttribute(const XMLChar* namespaceURI, const XMLChar* localName, const XMLChar* qname, const XMLChar* type, const XMLChar* value, bool specified)
167{
168 AttributeVec::iterator it = _attributes.insert(_attributes.end(), Attribute());
169 it->namespaceURI = namespaceURI;
170 it->localName = localName;
171 it->qname = qname;
172 it->value = value;
173 it->type = type;
174 it->specified = specified;
175}
176
177
178void AttributesImpl::removeAttribute(int i)
179{
180 int cur = 0;
181 for (AttributeVec::iterator it = _attributes.begin(); it != _attributes.end(); ++it, ++cur)
182 {
183 if (cur == i)
184 {
185 _attributes.erase(it);
186 break;
187 }
188 }
189}
190
191
192void AttributesImpl::removeAttribute(const XMLString& qname)
193{
194 for (AttributeVec::iterator it = _attributes.begin(); it != _attributes.end(); ++it)
195 {
196 if (it->qname == qname)
197 {
198 _attributes.erase(it);
199 break;
200 }
201 }
202}
203
204
205void AttributesImpl::removeAttribute(const XMLString& namespaceURI, const XMLString& localName)
206{
207 for (AttributeVec::iterator it = _attributes.begin(); it != _attributes.end(); ++it)
208 {
209 if (it->namespaceURI == namespaceURI && it->localName == localName)
210 {
211 _attributes.erase(it);
212 break;
213 }
214 }
215}
216
217
218void AttributesImpl::clear()
219{
220 _attributes.clear();
221}
222
223
224void AttributesImpl::reserve(std::size_t capacity)
225{
226 _attributes.reserve(capacity);
227}
228
229
230void AttributesImpl::setLocalName(int i, const XMLString& localName)
231{
232 poco_assert (0 <= i && i < static_cast<int>(_attributes.size()));
233 _attributes[i].localName = localName;
234}
235
236
237void AttributesImpl::setQName(int i, const XMLString& qname)
238{
239 poco_assert (0 <= i && i < static_cast<int>(_attributes.size()));
240 _attributes[i].qname = qname;
241}
242
243
244void AttributesImpl::setType(int i, const XMLString& type)
245{
246 poco_assert (0 <= i && i < static_cast<int>(_attributes.size()));
247 _attributes[i].type = type;
248}
249
250
251void AttributesImpl::setURI(int i, const XMLString& namespaceURI)
252{
253 poco_assert (0 <= i && i < static_cast<int>(_attributes.size()));
254 _attributes[i].namespaceURI = namespaceURI;
255}
256
257
258AttributesImpl::Attribute* AttributesImpl::find(const XMLString& qname) const
259{
260 for (AttributeVec::const_iterator it = _attributes.begin(); it != _attributes.end(); ++it)
261 {
262 if (it->qname == qname)
263 return const_cast<Attribute*>(&(*it));
264 }
265 return 0;
266}
267
268
269AttributesImpl::Attribute* AttributesImpl::find(const XMLString& namespaceURI, const XMLString& localName) const
270{
271 for (AttributeVec::const_iterator it = _attributes.begin(); it != _attributes.end(); ++it)
272 {
273 if (it->namespaceURI == namespaceURI && it->localName == localName)
274 return const_cast<Attribute*>(&(*it));
275 }
276 return 0;
277}
278
279
280} } // namespace Poco::XML
281