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