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