/src/poco/XML/include/Poco/SAX/AttributesImpl.h
Line | Count | Source |
1 | | // |
2 | | // AttributesImpl.h |
3 | | // |
4 | | // Library: XML |
5 | | // Package: SAX |
6 | | // Module: SAX |
7 | | // |
8 | | // Implementation of the SAX2 Attributes Interface. |
9 | | // |
10 | | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
11 | | // and Contributors. |
12 | | // |
13 | | // SPDX-License-Identifier: BSL-1.0 |
14 | | // |
15 | | |
16 | | |
17 | | #ifndef SAX_AttributesImpl_INCLUDED |
18 | | #define SAX_AttributesImpl_INCLUDED |
19 | | |
20 | | |
21 | | #include "Poco/XML/XML.h" |
22 | | #include "Poco/SAX/Attributes.h" |
23 | | #include <vector> |
24 | | |
25 | | |
26 | | namespace Poco::XML { |
27 | | |
28 | | |
29 | | class XML_API AttributesImpl: public Attributes |
30 | | /// This class provides a default implementation of the SAX2 Attributes interface, |
31 | | /// with the addition of manipulators so that the list can be modified or reused. |
32 | | /// |
33 | | /// There are two typical uses of this class: |
34 | | /// 1. to take a persistent snapshot of an Attributes object in a startElement event; or |
35 | | /// 2. to construct or modify an Attributes object in a SAX2 driver or filter. |
36 | | { |
37 | | public: |
38 | | struct Attribute |
39 | | { |
40 | | XMLString localName; |
41 | | XMLString namespaceURI; |
42 | | XMLString qname; |
43 | | XMLString value; |
44 | | XMLString type; |
45 | | bool specified; |
46 | | }; |
47 | | using AttributeVec = std::vector<Attribute>; |
48 | | using iterator = AttributeVec::const_iterator; |
49 | | |
50 | | AttributesImpl(); |
51 | | /// Creates the AttributesImpl. |
52 | | |
53 | | AttributesImpl(const Attributes& attributes); |
54 | | /// Creates the AttributesImpl by copying another one. |
55 | | |
56 | | AttributesImpl(const AttributesImpl& attributes); |
57 | | /// Creates the AttributesImpl by copying another one. |
58 | | |
59 | | AttributesImpl(AttributesImpl&& attributes) noexcept; |
60 | | /// Creates the AttributesImpl by copying another one. |
61 | | |
62 | | ~AttributesImpl(); |
63 | | /// Destroys the AttributesImpl. |
64 | | |
65 | | AttributesImpl& operator = (const AttributesImpl& attributes); |
66 | | /// Assignment operator. |
67 | | |
68 | | AttributesImpl& operator = (AttributesImpl&& attributes) noexcept; |
69 | | /// Assignment operator. |
70 | | |
71 | | [[nodiscard]] int getIndex(const XMLString& name) const; |
72 | | [[nodiscard]] int getIndex(const XMLString& namespaceURI, const XMLString& localName) const; |
73 | | [[nodiscard]] int getLength() const; |
74 | | [[nodiscard]] const XMLString& getLocalName(int i) const; |
75 | | [[nodiscard]] const XMLString& getQName(int i) const; |
76 | | [[nodiscard]] const XMLString& getType(int i) const; |
77 | | [[nodiscard]] const XMLString& getType(const XMLString& qname) const; |
78 | | [[nodiscard]] const XMLString& getType(const XMLString& namespaceURI, const XMLString& localName) const; |
79 | | [[nodiscard]] const XMLString& getValue(int i) const; |
80 | | [[nodiscard]] const XMLString& getValue(const XMLString& qname) const; |
81 | | [[nodiscard]] const XMLString& getValue(const XMLString& namespaceURI, const XMLString& localName) const; |
82 | | [[nodiscard]] const XMLString& getURI(int i) const; |
83 | | |
84 | | [[nodiscard]] bool isSpecified(int i) const; |
85 | | /// Returns true unless the attribute value was provided by DTD defaulting. |
86 | | /// Extension from Attributes2 interface. |
87 | | |
88 | | [[nodiscard]] bool isSpecified(const XMLString& qname) const; |
89 | | /// Returns true unless the attribute value was provided by DTD defaulting. |
90 | | /// Extension from Attributes2 interface. |
91 | | |
92 | | [[nodiscard]] bool isSpecified(const XMLString& namespaceURI, const XMLString& localName) const; |
93 | | /// Returns true unless the attribute value was provided by DTD defaulting. |
94 | | /// Extension from Attributes2 interface. |
95 | | |
96 | | void setValue(int i, const XMLString& value); |
97 | | /// Sets the value of an attribute. |
98 | | |
99 | | void setValue(const XMLString& qname, const XMLString& value); |
100 | | /// Sets the value of an attribute. |
101 | | |
102 | | void setValue(const XMLString& namespaceURI, const XMLString& localName, const XMLString& value); |
103 | | /// Sets the value of an attribute. |
104 | | |
105 | | void setAttributes(const Attributes& attributes); |
106 | | /// Copies the attributes from another Attributes object. |
107 | | |
108 | | void setAttribute(int i, const XMLString& namespaceURI, const XMLString& localName, const XMLString& qname, const XMLString& type, const XMLString& value); |
109 | | /// Sets an attribute. |
110 | | |
111 | | void addAttribute(const XMLString& namespaceURI, const XMLString& localName, const XMLString& qname, const XMLString& type, const XMLString& value); |
112 | | /// Adds an attribute to the end of the list. |
113 | | |
114 | | void addAttribute(const XMLString& namespaceURI, const XMLString& localName, const XMLString& qname, const XMLString& type, const XMLString& value, bool specified); |
115 | | /// Adds an attribute to the end of the list. |
116 | | |
117 | | void addAttribute(const XMLChar* namespaceURI, const XMLChar* localName, const XMLChar* qname, const XMLChar* type, const XMLChar* value, bool specified); |
118 | | /// Adds an attribute to the end of the list. |
119 | | |
120 | | Attribute& addAttribute(); |
121 | | /// Add an (empty) attribute to the end of the list. |
122 | | /// For internal use only. |
123 | | /// The returned Attribute element must be filled by the caller. |
124 | | |
125 | | void removeAttribute(int i); |
126 | | /// Removes an attribute. |
127 | | |
128 | | void removeAttribute(const XMLString& qname); |
129 | | /// Removes an attribute. |
130 | | |
131 | | void removeAttribute(const XMLString& namespaceURI, const XMLString& localName); |
132 | | /// Removes an attribute. |
133 | | |
134 | | void clear(); |
135 | | /// Removes all attributes. |
136 | | |
137 | | void reserve(std::size_t capacity); |
138 | | /// Reserves capacity in the internal vector. |
139 | | |
140 | | void setLocalName(int i, const XMLString& localName); |
141 | | /// Sets the local name of an attribute. |
142 | | |
143 | | void setQName(int i, const XMLString& qname); |
144 | | /// Sets the qualified name of an attribute. |
145 | | |
146 | | void setType(int i, const XMLString& type); |
147 | | /// Sets the type of an attribute. |
148 | | |
149 | | void setURI(int i, const XMLString& namespaceURI); |
150 | | /// Sets the namespace URI of an attribute. |
151 | | |
152 | | [[nodiscard]] iterator begin() const; |
153 | | /// Iterator support. |
154 | | |
155 | | [[nodiscard]] iterator end() const; |
156 | | /// Iterator support. |
157 | | |
158 | | protected: |
159 | | [[nodiscard]] Attribute* find(const XMLString& qname) const; |
160 | | [[nodiscard]] Attribute* find(const XMLString& namespaceURI, const XMLString& localName) const; |
161 | | |
162 | | struct EmptyAttribute: Attribute |
163 | | { |
164 | | EmptyAttribute(); |
165 | | }; |
166 | | |
167 | | private: |
168 | | AttributeVec _attributes; |
169 | | static EmptyAttribute _empty; |
170 | | }; |
171 | | |
172 | | |
173 | | // |
174 | | // inlines |
175 | | // |
176 | | inline AttributesImpl::iterator AttributesImpl::begin() const |
177 | 202k | { |
178 | 202k | return _attributes.begin(); |
179 | 202k | } |
180 | | |
181 | | |
182 | | inline AttributesImpl::iterator AttributesImpl::end() const |
183 | 202k | { |
184 | 202k | return _attributes.end(); |
185 | 202k | } |
186 | | |
187 | | |
188 | | inline AttributesImpl::Attribute& AttributesImpl::addAttribute() |
189 | 1.69M | { |
190 | 1.69M | _attributes.push_back(_empty); |
191 | 1.69M | return _attributes.back(); |
192 | 1.69M | } |
193 | | |
194 | | |
195 | | inline int AttributesImpl::getLength() const |
196 | 0 | { |
197 | 0 | return (int) _attributes.size(); |
198 | 0 | } |
199 | | |
200 | | |
201 | | inline const XMLString& AttributesImpl::getLocalName(int i) const |
202 | 0 | { |
203 | 0 | poco_assert (0 <= i && i < static_cast<int>(_attributes.size())); |
204 | 0 | return _attributes[i].localName; |
205 | 0 | } |
206 | | |
207 | | |
208 | | inline const XMLString& AttributesImpl::getQName(int i) const |
209 | 0 | { |
210 | 0 | poco_assert (0 <= i && i < static_cast<int>(_attributes.size())); |
211 | 0 | return _attributes[i].qname; |
212 | 0 | } |
213 | | |
214 | | |
215 | | inline const XMLString& AttributesImpl::getType(int i) const |
216 | 0 | { |
217 | 0 | poco_assert (0 <= i && i < static_cast<int>(_attributes.size())); |
218 | 0 | return _attributes[i].type; |
219 | 0 | } |
220 | | |
221 | | |
222 | | inline const XMLString& AttributesImpl::getType(const XMLString& qname) const |
223 | 0 | { |
224 | 0 | Attribute* pAttr = find(qname); |
225 | 0 | if (pAttr) |
226 | 0 | return pAttr->type; |
227 | 0 | else |
228 | 0 | return _empty.type; |
229 | 0 | } |
230 | | |
231 | | |
232 | | inline const XMLString& AttributesImpl::getType(const XMLString& namespaceURI, const XMLString& localName) const |
233 | 0 | { |
234 | 0 | Attribute* pAttr = find(namespaceURI, localName); |
235 | 0 | if (pAttr) |
236 | 0 | return pAttr->type; |
237 | 0 | else |
238 | 0 | return _empty.type; |
239 | 0 | } |
240 | | |
241 | | |
242 | | inline const XMLString& AttributesImpl::getValue(int i) const |
243 | 0 | { |
244 | 0 | poco_assert (0 <= i && i < static_cast<int>(_attributes.size())); |
245 | 0 | return _attributes[i].value; |
246 | 0 | } |
247 | | |
248 | | |
249 | | inline const XMLString& AttributesImpl::getValue(const XMLString& qname) const |
250 | 0 | { |
251 | 0 | Attribute* pAttr = find(qname); |
252 | 0 | if (pAttr) |
253 | 0 | return pAttr->value; |
254 | 0 | else |
255 | 0 | return _empty.value; |
256 | 0 | } |
257 | | |
258 | | |
259 | | inline const XMLString& AttributesImpl::getValue(const XMLString& namespaceURI, const XMLString& localName) const |
260 | 0 | { |
261 | 0 | Attribute* pAttr = find(namespaceURI, localName); |
262 | 0 | if (pAttr) |
263 | 0 | return pAttr->value; |
264 | 0 | else |
265 | 0 | return _empty.value; |
266 | 0 | } |
267 | | |
268 | | |
269 | | inline const XMLString& AttributesImpl::getURI(int i) const |
270 | 0 | { |
271 | 0 | poco_assert (0 <= i && i < static_cast<int>(_attributes.size())); |
272 | 0 | return _attributes[i].namespaceURI; |
273 | 0 | } |
274 | | |
275 | | |
276 | | inline bool AttributesImpl::isSpecified(int i) const |
277 | 0 | { |
278 | 0 | poco_assert (0 <= i && i < static_cast<int>(_attributes.size())); |
279 | 0 | return _attributes[i].specified; |
280 | 0 | } |
281 | | |
282 | | |
283 | | inline bool AttributesImpl::isSpecified(const XMLString& qname) const |
284 | 0 | { |
285 | 0 | Attribute* pAttr = find(qname); |
286 | 0 | if (pAttr) |
287 | 0 | return pAttr->specified; |
288 | 0 | else |
289 | 0 | return false; |
290 | 0 | } |
291 | | |
292 | | |
293 | | inline bool AttributesImpl::isSpecified(const XMLString& namespaceURI, const XMLString& localName) const |
294 | 0 | { |
295 | 0 | Attribute* pAttr = find(namespaceURI, localName); |
296 | 0 | if (pAttr) |
297 | 0 | return pAttr->specified; |
298 | 0 | else |
299 | 0 | return false; |
300 | 0 | } |
301 | | |
302 | | |
303 | | } // namespace Poco::XML |
304 | | |
305 | | |
306 | | #endif // SAX_AttributesImpl_INCLUDED |