3 * Copyright 2004--2005, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #ifndef TALK_XMLLITE_XMLELEMENT_H_
29 #define TALK_XMLLITE_XMLELEMENT_H_
34 #include "talk/xmllite/qname.h"
35 #include "webrtc/base/scoped_ptr.h"
46 XmlChild* NextChild() { return next_child_; }
47 const XmlChild* NextChild() const { return next_child_; }
49 bool IsText() const { return IsTextImpl(); }
51 XmlElement* AsElement() { return AsElementImpl(); }
52 const XmlElement* AsElement() const { return AsElementImpl(); }
54 XmlText* AsText() { return AsTextImpl(); }
55 const XmlText* AsText() const { return AsTextImpl(); }
63 virtual bool IsTextImpl() const = 0;
64 virtual XmlElement* AsElementImpl() const = 0;
65 virtual XmlText* AsTextImpl() const = 0;
71 friend class XmlElement;
73 XmlChild(const XmlChild& noimpl);
75 XmlChild* next_child_;
78 class XmlText : public XmlChild {
80 explicit XmlText(const std::string& text) :
84 explicit XmlText(const XmlText& t) :
88 explicit XmlText(const char* cstr, size_t len) :
94 const std::string& Text() const { return text_; }
95 void SetText(const std::string& text);
96 void AddParsedText(const char* buf, int len);
97 void AddText(const std::string& text);
100 virtual bool IsTextImpl() const;
101 virtual XmlElement* AsElementImpl() const;
102 virtual XmlText* AsTextImpl() const;
110 XmlAttr* NextAttr() const { return next_attr_; }
111 const QName& Name() const { return name_; }
112 const std::string& Value() const { return value_; }
115 friend class XmlElement;
117 explicit XmlAttr(const QName& name, const std::string& value) :
122 explicit XmlAttr(const XmlAttr& att) :
133 class XmlElement : public XmlChild {
135 explicit XmlElement(const QName& name);
136 explicit XmlElement(const QName& name, bool useDefaultNs);
137 explicit XmlElement(const XmlElement& elt);
139 virtual ~XmlElement();
141 const QName& Name() const { return name_; }
142 void SetName(const QName& name) { name_ = name; }
144 const std::string BodyText() const;
145 void SetBodyText(const std::string& text);
147 const QName FirstElementName() const;
149 XmlAttr* FirstAttr();
150 const XmlAttr* FirstAttr() const
151 { return const_cast<XmlElement *>(this)->FirstAttr(); }
153 // Attr will return an empty string if the attribute isn't there:
154 // use HasAttr to test presence of an attribute.
155 const std::string Attr(const StaticQName& name) const;
156 const std::string Attr(const QName& name) const;
157 bool HasAttr(const StaticQName& name) const;
158 bool HasAttr(const QName& name) const;
159 void SetAttr(const QName& name, const std::string& value);
160 void ClearAttr(const QName& name);
162 XmlChild* FirstChild();
163 const XmlChild* FirstChild() const {
164 return const_cast<XmlElement *>(this)->FirstChild();
167 XmlElement* FirstElement();
168 const XmlElement* FirstElement() const {
169 return const_cast<XmlElement *>(this)->FirstElement();
172 XmlElement* NextElement();
173 const XmlElement* NextElement() const {
174 return const_cast<XmlElement *>(this)->NextElement();
177 XmlElement* FirstWithNamespace(const std::string& ns);
178 const XmlElement* FirstWithNamespace(const std::string& ns) const {
179 return const_cast<XmlElement *>(this)->FirstWithNamespace(ns);
182 XmlElement* NextWithNamespace(const std::string& ns);
183 const XmlElement* NextWithNamespace(const std::string& ns) const {
184 return const_cast<XmlElement *>(this)->NextWithNamespace(ns);
187 XmlElement* FirstNamed(const StaticQName& name);
188 const XmlElement* FirstNamed(const StaticQName& name) const {
189 return const_cast<XmlElement *>(this)->FirstNamed(name);
192 XmlElement* FirstNamed(const QName& name);
193 const XmlElement* FirstNamed(const QName& name) const {
194 return const_cast<XmlElement *>(this)->FirstNamed(name);
197 XmlElement* NextNamed(const StaticQName& name);
198 const XmlElement* NextNamed(const StaticQName& name) const {
199 return const_cast<XmlElement *>(this)->NextNamed(name);
202 XmlElement* NextNamed(const QName& name);
203 const XmlElement* NextNamed(const QName& name) const {
204 return const_cast<XmlElement *>(this)->NextNamed(name);
207 // Finds the first element named 'name'. If that element can't be found then
208 // adds one and returns it.
209 XmlElement* FindOrAddNamedChild(const QName& name);
211 const std::string TextNamed(const QName& name) const;
213 void InsertChildAfter(XmlChild* predecessor, XmlChild* new_child);
214 void RemoveChildAfter(XmlChild* predecessor);
216 void AddParsedText(const char* buf, int len);
217 // Note: CDATA is not supported by XMPP, therefore using this function will
218 // generate non-XMPP compatible XML.
219 void AddCDATAText(const char* buf, int len);
220 void AddText(const std::string& text);
221 void AddText(const std::string& text, int depth);
222 void AddElement(XmlElement* child);
223 void AddElement(XmlElement* child, int depth);
224 void AddAttr(const QName& name, const std::string& value);
225 void AddAttr(const QName& name, const std::string& value, int depth);
226 void ClearNamedChildren(const QName& name);
227 void ClearAttributes();
228 void ClearChildren();
230 static XmlElement* ForStr(const std::string& str);
231 std::string Str() const;
233 bool IsCDATA() const { return cdata_; }
236 virtual bool IsTextImpl() const;
237 virtual XmlElement* AsElementImpl() const;
238 virtual XmlText* AsTextImpl() const;
242 XmlAttr* first_attr_;
244 XmlChild* first_child_;
245 XmlChild* last_child_;
251 #endif // TALK_XMLLITE_XMLELEMENT_H_