AttributesImpl.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. #ifndef SAX_AttributesImpl_INCLUDED
  16. #define SAX_AttributesImpl_INCLUDED
  17. #include "Poco/XML/XML.h"
  18. #include "Poco/SAX/Attributes.h"
  19. #include <vector>
  20. namespace Poco {
  21. namespace XML {
  22. class XML_API AttributesImpl: public Attributes
  23. /// This class provides a default implementation of the SAX2 Attributes interface,
  24. /// with the addition of manipulators so that the list can be modified or reused.
  25. ///
  26. /// There are two typical uses of this class:
  27. /// 1. to take a persistent snapshot of an Attributes object in a startElement event; or
  28. /// 2. to construct or modify an Attributes object in a SAX2 driver or filter.
  29. {
  30. public:
  31. struct Attribute
  32. {
  33. XMLString localName;
  34. XMLString namespaceURI;
  35. XMLString qname;
  36. XMLString value;
  37. XMLString type;
  38. bool specified;
  39. };
  40. typedef std::vector<Attribute> AttributeVec;
  41. typedef AttributeVec::const_iterator iterator;
  42. AttributesImpl();
  43. /// Creates the AttributesImpl.
  44. AttributesImpl(const Attributes& attributes);
  45. /// Creates the AttributesImpl by copying another one.
  46. AttributesImpl(const AttributesImpl& attributes);
  47. /// Creates the AttributesImpl by copying another one.
  48. ~AttributesImpl();
  49. /// Destroys the AttributesImpl.
  50. AttributesImpl& operator = (const AttributesImpl& attributes);
  51. /// Assignment operator.
  52. int getIndex(const XMLString& name) const;
  53. int getIndex(const XMLString& namespaceURI, const XMLString& localName) const;
  54. int getLength() const;
  55. const XMLString& getLocalName(int i) const;
  56. const XMLString& getQName(int i) const;
  57. const XMLString& getType(int i) const;
  58. const XMLString& getType(const XMLString& qname) const;
  59. const XMLString& getType(const XMLString& namespaceURI, const XMLString& localName) const;
  60. const XMLString& getValue(int i) const;
  61. const XMLString& getValue(const XMLString& qname) const;
  62. const XMLString& getValue(const XMLString& namespaceURI, const XMLString& localName) const;
  63. const XMLString& getURI(int i) const;
  64. bool isSpecified(int i) const;
  65. /// Returns true unless the attribute value was provided by DTD defaulting.
  66. /// Extension from Attributes2 interface.
  67. bool isSpecified(const XMLString& qname) const;
  68. /// Returns true unless the attribute value was provided by DTD defaulting.
  69. /// Extension from Attributes2 interface.
  70. bool isSpecified(const XMLString& namespaceURI, const XMLString& localName) const;
  71. /// Returns true unless the attribute value was provided by DTD defaulting.
  72. /// Extension from Attributes2 interface.
  73. void setValue(int i, const XMLString& value);
  74. /// Sets the value of an attribute.
  75. void setValue(const XMLString& qname, const XMLString& value);
  76. /// Sets the value of an attribute.
  77. void setValue(const XMLString& namespaceURI, const XMLString& localName, const XMLString& value);
  78. /// Sets the value of an attribute.
  79. void setAttributes(const Attributes& attributes);
  80. /// Copies the attributes from another Attributes object.
  81. void setAttribute(int i, const XMLString& namespaceURI, const XMLString& localName, const XMLString& qname, const XMLString& type, const XMLString& value);
  82. /// Sets an attribute.
  83. void addAttribute(const XMLString& namespaceURI, const XMLString& localName, const XMLString& qname, const XMLString& type, const XMLString& value);
  84. /// Adds an attribute to the end of the list.
  85. void addAttribute(const XMLString& namespaceURI, const XMLString& localName, const XMLString& qname, const XMLString& type, const XMLString& value, bool specified);
  86. /// Adds an attribute to the end of the list.
  87. void addAttribute(const XMLChar* namespaceURI, const XMLChar* localName, const XMLChar* qname, const XMLChar* type, const XMLChar* value, bool specified);
  88. /// Adds an attribute to the end of the list.
  89. Attribute& addAttribute();
  90. /// Add an (empty) attribute to the end of the list.
  91. /// For internal use only.
  92. /// The returned Attribute element must be filled by the caller.
  93. void removeAttribute(int i);
  94. /// Removes an attribute.
  95. void removeAttribute(const XMLString& qname);
  96. /// Removes an attribute.
  97. void removeAttribute(const XMLString& namespaceURI, const XMLString& localName);
  98. /// Removes an attribute.
  99. void clear();
  100. /// Removes all attributes.
  101. void reserve(std::size_t capacity);
  102. /// Reserves capacity in the internal vector.
  103. void setLocalName(int i, const XMLString& localName);
  104. /// Sets the local name of an attribute.
  105. void setQName(int i, const XMLString& qname);
  106. /// Sets the qualified name of an attribute.
  107. void setType(int i, const XMLString& type);
  108. /// Sets the type of an attribute.
  109. void setURI(int i, const XMLString& namespaceURI);
  110. /// Sets the namespace URI of an attribute.
  111. iterator begin() const;
  112. /// Iterator support.
  113. iterator end() const;
  114. /// Iterator support.
  115. protected:
  116. Attribute* find(const XMLString& qname) const;
  117. Attribute* find(const XMLString& namespaceURI, const XMLString& localName) const;
  118. private:
  119. AttributeVec _attributes;
  120. Attribute _empty;
  121. };
  122. //
  123. // inlines
  124. //
  125. inline AttributesImpl::iterator AttributesImpl::begin() const
  126. {
  127. return _attributes.begin();
  128. }
  129. inline AttributesImpl::iterator AttributesImpl::end() const
  130. {
  131. return _attributes.end();
  132. }
  133. inline AttributesImpl::Attribute& AttributesImpl::addAttribute()
  134. {
  135. _attributes.push_back(_empty);
  136. return _attributes.back();
  137. }
  138. inline int AttributesImpl::getLength() const
  139. {
  140. return (int) _attributes.size();
  141. }
  142. inline const XMLString& AttributesImpl::getLocalName(int i) const
  143. {
  144. poco_assert (0 <= i && i < static_cast<int>(_attributes.size()));
  145. return _attributes[i].localName;
  146. }
  147. inline const XMLString& AttributesImpl::getQName(int i) const
  148. {
  149. poco_assert (0 <= i && i < static_cast<int>(_attributes.size()));
  150. return _attributes[i].qname;
  151. }
  152. inline const XMLString& AttributesImpl::getType(int i) const
  153. {
  154. poco_assert (0 <= i && i < static_cast<int>(_attributes.size()));
  155. return _attributes[i].type;
  156. }
  157. inline const XMLString& AttributesImpl::getType(const XMLString& qname) const
  158. {
  159. Attribute* pAttr = find(qname);
  160. if (pAttr)
  161. return pAttr->type;
  162. else
  163. return _empty.type;
  164. }
  165. inline const XMLString& AttributesImpl::getType(const XMLString& namespaceURI, const XMLString& localName) const
  166. {
  167. Attribute* pAttr = find(namespaceURI, localName);
  168. if (pAttr)
  169. return pAttr->type;
  170. else
  171. return _empty.type;
  172. }
  173. inline const XMLString& AttributesImpl::getValue(int i) const
  174. {
  175. poco_assert (0 <= i && i < static_cast<int>(_attributes.size()));
  176. return _attributes[i].value;
  177. }
  178. inline const XMLString& AttributesImpl::getValue(const XMLString& qname) const
  179. {
  180. Attribute* pAttr = find(qname);
  181. if (pAttr)
  182. return pAttr->value;
  183. else
  184. return _empty.value;
  185. }
  186. inline const XMLString& AttributesImpl::getValue(const XMLString& namespaceURI, const XMLString& localName) const
  187. {
  188. Attribute* pAttr = find(namespaceURI, localName);
  189. if (pAttr)
  190. return pAttr->value;
  191. else
  192. return _empty.value;
  193. }
  194. inline const XMLString& AttributesImpl::getURI(int i) const
  195. {
  196. poco_assert (0 <= i && i < static_cast<int>(_attributes.size()));
  197. return _attributes[i].namespaceURI;
  198. }
  199. inline bool AttributesImpl::isSpecified(int i) const
  200. {
  201. poco_assert (0 <= i && i < static_cast<int>(_attributes.size()));
  202. return _attributes[i].specified;
  203. }
  204. inline bool AttributesImpl::isSpecified(const XMLString& qname) const
  205. {
  206. Attribute* pAttr = find(qname);
  207. if (pAttr)
  208. return pAttr->specified;
  209. else
  210. return false;
  211. }
  212. inline bool AttributesImpl::isSpecified(const XMLString& namespaceURI, const XMLString& localName) const
  213. {
  214. Attribute* pAttr = find(namespaceURI, localName);
  215. if (pAttr)
  216. return pAttr->specified;
  217. else
  218. return false;
  219. }
  220. } } // namespace Poco::XML
  221. #endif // SAX_AttributesImpl_INCLUDED