OLD | NEW |
(Empty) | |
| 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 |
| 11 #ifndef WEBRTC_LIBJINGLE_XMLLITE_XMLELEMENT_H_ |
| 12 #define WEBRTC_LIBJINGLE_XMLLITE_XMLELEMENT_H_ |
| 13 |
| 14 #include <iosfwd> |
| 15 #include <string> |
| 16 |
| 17 #include "webrtc/libjingle/xmllite/qname.h" |
| 18 |
| 19 namespace buzz { |
| 20 |
| 21 class XmlChild; |
| 22 class XmlText; |
| 23 class XmlElement; |
| 24 class XmlAttr; |
| 25 |
| 26 class XmlChild { |
| 27 public: |
| 28 XmlChild* NextChild() { return next_child_; } |
| 29 const XmlChild* NextChild() const { return next_child_; } |
| 30 |
| 31 bool IsText() const { return IsTextImpl(); } |
| 32 |
| 33 XmlElement* AsElement() { return AsElementImpl(); } |
| 34 const XmlElement* AsElement() const { return AsElementImpl(); } |
| 35 |
| 36 XmlText* AsText() { return AsTextImpl(); } |
| 37 const XmlText* AsText() const { return AsTextImpl(); } |
| 38 |
| 39 |
| 40 protected: |
| 41 XmlChild() : |
| 42 next_child_(NULL) { |
| 43 } |
| 44 |
| 45 virtual bool IsTextImpl() const = 0; |
| 46 virtual XmlElement* AsElementImpl() const = 0; |
| 47 virtual XmlText* AsTextImpl() const = 0; |
| 48 |
| 49 |
| 50 virtual ~XmlChild(); |
| 51 |
| 52 private: |
| 53 friend class XmlElement; |
| 54 |
| 55 XmlChild(const XmlChild& noimpl); |
| 56 |
| 57 XmlChild* next_child_; |
| 58 }; |
| 59 |
| 60 class XmlText : public XmlChild { |
| 61 public: |
| 62 explicit XmlText(const std::string& text) : |
| 63 XmlChild(), |
| 64 text_(text) { |
| 65 } |
| 66 explicit XmlText(const XmlText& t) : |
| 67 XmlChild(), |
| 68 text_(t.text_) { |
| 69 } |
| 70 explicit XmlText(const char* cstr, size_t len) : |
| 71 XmlChild(), |
| 72 text_(cstr, len) { |
| 73 } |
| 74 virtual ~XmlText(); |
| 75 |
| 76 const std::string& Text() const { return text_; } |
| 77 void SetText(const std::string& text); |
| 78 void AddParsedText(const char* buf, int len); |
| 79 void AddText(const std::string& text); |
| 80 |
| 81 protected: |
| 82 virtual bool IsTextImpl() const; |
| 83 virtual XmlElement* AsElementImpl() const; |
| 84 virtual XmlText* AsTextImpl() const; |
| 85 |
| 86 private: |
| 87 std::string text_; |
| 88 }; |
| 89 |
| 90 class XmlAttr { |
| 91 public: |
| 92 XmlAttr* NextAttr() const { return next_attr_; } |
| 93 const QName& Name() const { return name_; } |
| 94 const std::string& Value() const { return value_; } |
| 95 |
| 96 private: |
| 97 friend class XmlElement; |
| 98 |
| 99 explicit XmlAttr(const QName& name, const std::string& value) : |
| 100 next_attr_(NULL), |
| 101 name_(name), |
| 102 value_(value) { |
| 103 } |
| 104 explicit XmlAttr(const XmlAttr& att) : |
| 105 next_attr_(NULL), |
| 106 name_(att.name_), |
| 107 value_(att.value_) { |
| 108 } |
| 109 |
| 110 XmlAttr* next_attr_; |
| 111 QName name_; |
| 112 std::string value_; |
| 113 }; |
| 114 |
| 115 class XmlElement : public XmlChild { |
| 116 public: |
| 117 explicit XmlElement(const QName& name); |
| 118 explicit XmlElement(const QName& name, bool useDefaultNs); |
| 119 explicit XmlElement(const XmlElement& elt); |
| 120 |
| 121 virtual ~XmlElement(); |
| 122 |
| 123 const QName& Name() const { return name_; } |
| 124 void SetName(const QName& name) { name_ = name; } |
| 125 |
| 126 const std::string BodyText() const; |
| 127 void SetBodyText(const std::string& text); |
| 128 |
| 129 const QName FirstElementName() const; |
| 130 |
| 131 XmlAttr* FirstAttr(); |
| 132 const XmlAttr* FirstAttr() const |
| 133 { return const_cast<XmlElement *>(this)->FirstAttr(); } |
| 134 |
| 135 // Attr will return an empty string if the attribute isn't there: |
| 136 // use HasAttr to test presence of an attribute. |
| 137 const std::string Attr(const StaticQName& name) const; |
| 138 const std::string Attr(const QName& name) const; |
| 139 bool HasAttr(const StaticQName& name) const; |
| 140 bool HasAttr(const QName& name) const; |
| 141 void SetAttr(const QName& name, const std::string& value); |
| 142 void ClearAttr(const QName& name); |
| 143 |
| 144 XmlChild* FirstChild(); |
| 145 const XmlChild* FirstChild() const { |
| 146 return const_cast<XmlElement *>(this)->FirstChild(); |
| 147 } |
| 148 |
| 149 XmlElement* FirstElement(); |
| 150 const XmlElement* FirstElement() const { |
| 151 return const_cast<XmlElement *>(this)->FirstElement(); |
| 152 } |
| 153 |
| 154 XmlElement* NextElement(); |
| 155 const XmlElement* NextElement() const { |
| 156 return const_cast<XmlElement *>(this)->NextElement(); |
| 157 } |
| 158 |
| 159 XmlElement* FirstWithNamespace(const std::string& ns); |
| 160 const XmlElement* FirstWithNamespace(const std::string& ns) const { |
| 161 return const_cast<XmlElement *>(this)->FirstWithNamespace(ns); |
| 162 } |
| 163 |
| 164 XmlElement* NextWithNamespace(const std::string& ns); |
| 165 const XmlElement* NextWithNamespace(const std::string& ns) const { |
| 166 return const_cast<XmlElement *>(this)->NextWithNamespace(ns); |
| 167 } |
| 168 |
| 169 XmlElement* FirstNamed(const StaticQName& name); |
| 170 const XmlElement* FirstNamed(const StaticQName& name) const { |
| 171 return const_cast<XmlElement *>(this)->FirstNamed(name); |
| 172 } |
| 173 |
| 174 XmlElement* FirstNamed(const QName& name); |
| 175 const XmlElement* FirstNamed(const QName& name) const { |
| 176 return const_cast<XmlElement *>(this)->FirstNamed(name); |
| 177 } |
| 178 |
| 179 XmlElement* NextNamed(const StaticQName& name); |
| 180 const XmlElement* NextNamed(const StaticQName& name) const { |
| 181 return const_cast<XmlElement *>(this)->NextNamed(name); |
| 182 } |
| 183 |
| 184 XmlElement* NextNamed(const QName& name); |
| 185 const XmlElement* NextNamed(const QName& name) const { |
| 186 return const_cast<XmlElement *>(this)->NextNamed(name); |
| 187 } |
| 188 |
| 189 // Finds the first element named 'name'. If that element can't be found then |
| 190 // adds one and returns it. |
| 191 XmlElement* FindOrAddNamedChild(const QName& name); |
| 192 |
| 193 const std::string TextNamed(const QName& name) const; |
| 194 |
| 195 void InsertChildAfter(XmlChild* predecessor, XmlChild* new_child); |
| 196 void RemoveChildAfter(XmlChild* predecessor); |
| 197 |
| 198 void AddParsedText(const char* buf, int len); |
| 199 // Note: CDATA is not supported by XMPP, therefore using this function will |
| 200 // generate non-XMPP compatible XML. |
| 201 void AddCDATAText(const char* buf, int len); |
| 202 void AddText(const std::string& text); |
| 203 void AddText(const std::string& text, int depth); |
| 204 void AddElement(XmlElement* child); |
| 205 void AddElement(XmlElement* child, int depth); |
| 206 void AddAttr(const QName& name, const std::string& value); |
| 207 void AddAttr(const QName& name, const std::string& value, int depth); |
| 208 void ClearNamedChildren(const QName& name); |
| 209 void ClearAttributes(); |
| 210 void ClearChildren(); |
| 211 |
| 212 static XmlElement* ForStr(const std::string& str); |
| 213 std::string Str() const; |
| 214 |
| 215 bool IsCDATA() const { return cdata_; } |
| 216 |
| 217 protected: |
| 218 virtual bool IsTextImpl() const; |
| 219 virtual XmlElement* AsElementImpl() const; |
| 220 virtual XmlText* AsTextImpl() const; |
| 221 |
| 222 private: |
| 223 QName name_; |
| 224 XmlAttr* first_attr_; |
| 225 XmlAttr* last_attr_; |
| 226 XmlChild* first_child_; |
| 227 XmlChild* last_child_; |
| 228 bool cdata_; |
| 229 }; |
| 230 |
| 231 } // namespace buzz |
| 232 |
| 233 #endif // WEBRTC_LIBJINGLE_XMLLITE_XMLELEMENT_H_ |
OLD | NEW |