xmlelement.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright 2004 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef THIRD_PARTY_LIBJINGLE_XMPP_XMLLITE_XMLELEMENT_H_
  11. #define THIRD_PARTY_LIBJINGLE_XMPP_XMLLITE_XMLELEMENT_H_
  12. #include <iosfwd>
  13. #include <string>
  14. #include "third_party/libjingle_xmpp/xmllite/qname.h"
  15. namespace jingle_xmpp {
  16. class XmlChild;
  17. class XmlText;
  18. class XmlElement;
  19. class XmlAttr;
  20. class XmlChild {
  21. public:
  22. XmlChild* NextChild() { return next_child_; }
  23. const XmlChild* NextChild() const { return next_child_; }
  24. bool IsText() const { return IsTextImpl(); }
  25. XmlElement* AsElement() { return AsElementImpl(); }
  26. const XmlElement* AsElement() const { return AsElementImpl(); }
  27. XmlText* AsText() { return AsTextImpl(); }
  28. const XmlText* AsText() const { return AsTextImpl(); }
  29. protected:
  30. XmlChild() :
  31. next_child_(NULL) {
  32. }
  33. virtual bool IsTextImpl() const = 0;
  34. virtual XmlElement* AsElementImpl() const = 0;
  35. virtual XmlText* AsTextImpl() const = 0;
  36. virtual ~XmlChild();
  37. private:
  38. friend class XmlElement;
  39. XmlChild(const XmlChild& noimpl);
  40. XmlChild* next_child_;
  41. };
  42. class XmlText : public XmlChild {
  43. public:
  44. explicit XmlText(const std::string& text) :
  45. XmlChild(),
  46. text_(text) {
  47. }
  48. explicit XmlText(const XmlText& t) :
  49. XmlChild(),
  50. text_(t.text_) {
  51. }
  52. explicit XmlText(const char* cstr, size_t len) :
  53. XmlChild(),
  54. text_(cstr, len) {
  55. }
  56. virtual ~XmlText();
  57. const std::string& Text() const { return text_; }
  58. void SetText(const std::string& text);
  59. void AddParsedText(const char* buf, int len);
  60. void AddText(const std::string& text);
  61. protected:
  62. virtual bool IsTextImpl() const;
  63. virtual XmlElement* AsElementImpl() const;
  64. virtual XmlText* AsTextImpl() const;
  65. private:
  66. std::string text_;
  67. };
  68. class XmlAttr {
  69. public:
  70. XmlAttr* NextAttr() const { return next_attr_; }
  71. const QName& Name() const { return name_; }
  72. const std::string& Value() const { return value_; }
  73. private:
  74. friend class XmlElement;
  75. explicit XmlAttr(const QName& name, const std::string& value) :
  76. next_attr_(NULL),
  77. name_(name),
  78. value_(value) {
  79. }
  80. explicit XmlAttr(const XmlAttr& att) :
  81. next_attr_(NULL),
  82. name_(att.name_),
  83. value_(att.value_) {
  84. }
  85. XmlAttr* next_attr_;
  86. QName name_;
  87. std::string value_;
  88. };
  89. class XmlElement : public XmlChild {
  90. public:
  91. explicit XmlElement(const QName& name);
  92. explicit XmlElement(const QName& name, bool useDefaultNs);
  93. explicit XmlElement(const XmlElement& elt);
  94. virtual ~XmlElement();
  95. const QName& Name() const { return name_; }
  96. void SetName(const QName& name) { name_ = name; }
  97. const std::string BodyText() const;
  98. void SetBodyText(const std::string& text);
  99. const QName FirstElementName() const;
  100. XmlAttr* FirstAttr();
  101. const XmlAttr* FirstAttr() const
  102. { return const_cast<XmlElement *>(this)->FirstAttr(); }
  103. // Attr will return an empty string if the attribute isn't there:
  104. // use HasAttr to test presence of an attribute.
  105. const std::string Attr(const StaticQName& name) const;
  106. const std::string Attr(const QName& name) const;
  107. bool HasAttr(const StaticQName& name) const;
  108. bool HasAttr(const QName& name) const;
  109. void SetAttr(const QName& name, const std::string& value);
  110. void ClearAttr(const QName& name);
  111. XmlChild* FirstChild();
  112. const XmlChild* FirstChild() const {
  113. return const_cast<XmlElement *>(this)->FirstChild();
  114. }
  115. XmlElement* FirstElement();
  116. const XmlElement* FirstElement() const {
  117. return const_cast<XmlElement *>(this)->FirstElement();
  118. }
  119. XmlElement* NextElement();
  120. const XmlElement* NextElement() const {
  121. return const_cast<XmlElement *>(this)->NextElement();
  122. }
  123. XmlElement* FirstWithNamespace(const std::string& ns);
  124. const XmlElement* FirstWithNamespace(const std::string& ns) const {
  125. return const_cast<XmlElement *>(this)->FirstWithNamespace(ns);
  126. }
  127. XmlElement* NextWithNamespace(const std::string& ns);
  128. const XmlElement* NextWithNamespace(const std::string& ns) const {
  129. return const_cast<XmlElement *>(this)->NextWithNamespace(ns);
  130. }
  131. XmlElement* FirstNamed(const StaticQName& name);
  132. const XmlElement* FirstNamed(const StaticQName& name) const {
  133. return const_cast<XmlElement *>(this)->FirstNamed(name);
  134. }
  135. XmlElement* FirstNamed(const QName& name);
  136. const XmlElement* FirstNamed(const QName& name) const {
  137. return const_cast<XmlElement *>(this)->FirstNamed(name);
  138. }
  139. XmlElement* NextNamed(const StaticQName& name);
  140. const XmlElement* NextNamed(const StaticQName& name) const {
  141. return const_cast<XmlElement *>(this)->NextNamed(name);
  142. }
  143. XmlElement* NextNamed(const QName& name);
  144. const XmlElement* NextNamed(const QName& name) const {
  145. return const_cast<XmlElement *>(this)->NextNamed(name);
  146. }
  147. // Finds the first element named 'name'. If that element can't be found then
  148. // adds one and returns it.
  149. XmlElement* FindOrAddNamedChild(const QName& name);
  150. const std::string TextNamed(const QName& name) const;
  151. void InsertChildAfter(XmlChild* predecessor, XmlChild* new_child);
  152. void RemoveChildAfter(XmlChild* predecessor);
  153. void AddParsedText(const char* buf, int len);
  154. // Note: CDATA is not supported by XMPP, therefore using this function will
  155. // generate non-XMPP compatible XML.
  156. void AddCDATAText(const char* buf, int len);
  157. void AddText(const std::string& text);
  158. void AddText(const std::string& text, int depth);
  159. void AddElement(XmlElement* child);
  160. void AddElement(XmlElement* child, int depth);
  161. void AddAttr(const QName& name, const std::string& value);
  162. void AddAttr(const QName& name, const std::string& value, int depth);
  163. void ClearNamedChildren(const QName& name);
  164. void ClearAttributes();
  165. void ClearChildren();
  166. static XmlElement* ForStr(const std::string& str);
  167. std::string Str() const;
  168. bool IsCDATA() const { return cdata_; }
  169. protected:
  170. virtual bool IsTextImpl() const;
  171. virtual XmlElement* AsElementImpl() const;
  172. virtual XmlText* AsTextImpl() const;
  173. private:
  174. QName name_;
  175. XmlAttr* first_attr_;
  176. XmlAttr* last_attr_;
  177. XmlChild* first_child_;
  178. XmlChild* last_child_;
  179. bool cdata_;
  180. };
  181. } // namespace jingle_xmpp
  182. #endif // THIRD_PARTY_LIBJINGLE_XMPP_XMLLITE_XMLELEMENT_H_