Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(82)

Side by Side Diff: third_party/xmllite/xmlelement.cc

Issue 2443903004: Add xmllite and xmpp sources to third_party/ (Closed)
Patch Set: Restored includes in jingle/ as well Created 3 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2004 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "third_party/xmllite/xmlelement.h"
6
7 #include <ostream>
8 #include <sstream>
9 #include <string>
10 #include <vector>
11
12 #include "third_party/xmllite/qname.h"
13 #include "third_party/xmllite/xmlbuilder.h"
14 #include "third_party/xmllite/xmlconstants.h"
15 #include "third_party/xmllite/xmlparser.h"
16 #include "third_party/xmllite/xmlprinter.h"
17 #include "webrtc/base/common.h"
18
19 namespace buzz {
20
21 XmlChild::~XmlChild() {
22 }
23
24 bool XmlText::IsTextImpl() const {
25 return true;
26 }
27
28 XmlElement* XmlText::AsElementImpl() const {
29 return NULL;
30 }
31
32 XmlText* XmlText::AsTextImpl() const {
33 return const_cast<XmlText *>(this);
34 }
35
36 void XmlText::SetText(const std::string& text) {
37 text_ = text;
38 }
39
40 void XmlText::AddParsedText(const char* buf, int len) {
41 text_.append(buf, len);
42 }
43
44 void XmlText::AddText(const std::string& text) {
45 text_ += text;
46 }
47
48 XmlText::~XmlText() {
49 }
50
51 XmlElement::XmlElement(const QName& name) :
52 name_(name),
53 first_attr_(NULL),
54 last_attr_(NULL),
55 first_child_(NULL),
56 last_child_(NULL),
57 cdata_(false) {
58 }
59
60 XmlElement::XmlElement(const XmlElement& elt) :
61 XmlChild(),
62 name_(elt.name_),
63 first_attr_(NULL),
64 last_attr_(NULL),
65 first_child_(NULL),
66 last_child_(NULL),
67 cdata_(false) {
68
69 // copy attributes
70 XmlAttr* attr;
71 XmlAttr ** plast_attr = &first_attr_;
72 XmlAttr* newAttr = NULL;
73 for (attr = elt.first_attr_; attr; attr = attr->NextAttr()) {
74 newAttr = new XmlAttr(*attr);
75 *plast_attr = newAttr;
76 plast_attr = &(newAttr->next_attr_);
77 }
78 last_attr_ = newAttr;
79
80 // copy children
81 XmlChild* pChild;
82 XmlChild ** ppLast = &first_child_;
83 XmlChild* newChild = NULL;
84
85 for (pChild = elt.first_child_; pChild; pChild = pChild->NextChild()) {
86 if (pChild->IsText()) {
87 newChild = new XmlText(*(pChild->AsText()));
88 } else {
89 newChild = new XmlElement(*(pChild->AsElement()));
90 }
91 *ppLast = newChild;
92 ppLast = &(newChild->next_child_);
93 }
94 last_child_ = newChild;
95
96 cdata_ = elt.cdata_;
97 }
98
99 XmlElement::XmlElement(const QName& name, bool useDefaultNs) :
100 name_(name),
101 first_attr_(useDefaultNs ? new XmlAttr(QN_XMLNS, name.Namespace()) : NULL),
102 last_attr_(first_attr_),
103 first_child_(NULL),
104 last_child_(NULL),
105 cdata_(false) {
106 }
107
108 bool XmlElement::IsTextImpl() const {
109 return false;
110 }
111
112 XmlElement* XmlElement::AsElementImpl() const {
113 return const_cast<XmlElement *>(this);
114 }
115
116 XmlText* XmlElement::AsTextImpl() const {
117 return NULL;
118 }
119
120 const std::string XmlElement::BodyText() const {
121 if (first_child_ && first_child_->IsText() && last_child_ == first_child_) {
122 return first_child_->AsText()->Text();
123 }
124
125 return std::string();
126 }
127
128 void XmlElement::SetBodyText(const std::string& text) {
129 if (text.empty()) {
130 ClearChildren();
131 } else if (first_child_ == NULL) {
132 AddText(text);
133 } else if (first_child_->IsText() && last_child_ == first_child_) {
134 first_child_->AsText()->SetText(text);
135 } else {
136 ClearChildren();
137 AddText(text);
138 }
139 }
140
141 const QName XmlElement::FirstElementName() const {
142 const XmlElement* element = FirstElement();
143 if (element == NULL)
144 return QName();
145 return element->Name();
146 }
147
148 XmlAttr* XmlElement::FirstAttr() {
149 return first_attr_;
150 }
151
152 const std::string XmlElement::Attr(const StaticQName& name) const {
153 XmlAttr* attr;
154 for (attr = first_attr_; attr; attr = attr->next_attr_) {
155 if (attr->name_ == name)
156 return attr->value_;
157 }
158 return std::string();
159 }
160
161 const std::string XmlElement::Attr(const QName& name) const {
162 XmlAttr* attr;
163 for (attr = first_attr_; attr; attr = attr->next_attr_) {
164 if (attr->name_ == name)
165 return attr->value_;
166 }
167 return std::string();
168 }
169
170 bool XmlElement::HasAttr(const StaticQName& name) const {
171 XmlAttr* attr;
172 for (attr = first_attr_; attr; attr = attr->next_attr_) {
173 if (attr->name_ == name)
174 return true;
175 }
176 return false;
177 }
178
179 bool XmlElement::HasAttr(const QName& name) const {
180 XmlAttr* attr;
181 for (attr = first_attr_; attr; attr = attr->next_attr_) {
182 if (attr->name_ == name)
183 return true;
184 }
185 return false;
186 }
187
188 void XmlElement::SetAttr(const QName& name, const std::string& value) {
189 XmlAttr* attr;
190 for (attr = first_attr_; attr; attr = attr->next_attr_) {
191 if (attr->name_ == name)
192 break;
193 }
194 if (!attr) {
195 attr = new XmlAttr(name, value);
196 if (last_attr_)
197 last_attr_->next_attr_ = attr;
198 else
199 first_attr_ = attr;
200 last_attr_ = attr;
201 return;
202 }
203 attr->value_ = value;
204 }
205
206 void XmlElement::ClearAttr(const QName& name) {
207 XmlAttr* attr;
208 XmlAttr* last_attr = NULL;
209 for (attr = first_attr_; attr; attr = attr->next_attr_) {
210 if (attr->name_ == name)
211 break;
212 last_attr = attr;
213 }
214 if (!attr)
215 return;
216 if (!last_attr)
217 first_attr_ = attr->next_attr_;
218 else
219 last_attr->next_attr_ = attr->next_attr_;
220 if (last_attr_ == attr)
221 last_attr_ = last_attr;
222 delete attr;
223 }
224
225 XmlChild* XmlElement::FirstChild() {
226 return first_child_;
227 }
228
229 XmlElement* XmlElement::FirstElement() {
230 XmlChild* pChild;
231 for (pChild = first_child_; pChild; pChild = pChild->next_child_) {
232 if (!pChild->IsText())
233 return pChild->AsElement();
234 }
235 return NULL;
236 }
237
238 XmlElement* XmlElement::NextElement() {
239 XmlChild* pChild;
240 for (pChild = next_child_; pChild; pChild = pChild->next_child_) {
241 if (!pChild->IsText())
242 return pChild->AsElement();
243 }
244 return NULL;
245 }
246
247 XmlElement* XmlElement::FirstWithNamespace(const std::string& ns) {
248 XmlChild* pChild;
249 for (pChild = first_child_; pChild; pChild = pChild->next_child_) {
250 if (!pChild->IsText() && pChild->AsElement()->Name().Namespace() == ns)
251 return pChild->AsElement();
252 }
253 return NULL;
254 }
255
256 XmlElement *
257 XmlElement::NextWithNamespace(const std::string& ns) {
258 XmlChild* pChild;
259 for (pChild = next_child_; pChild; pChild = pChild->next_child_) {
260 if (!pChild->IsText() && pChild->AsElement()->Name().Namespace() == ns)
261 return pChild->AsElement();
262 }
263 return NULL;
264 }
265
266 XmlElement *
267 XmlElement::FirstNamed(const QName& name) {
268 XmlChild* pChild;
269 for (pChild = first_child_; pChild; pChild = pChild->next_child_) {
270 if (!pChild->IsText() && pChild->AsElement()->Name() == name)
271 return pChild->AsElement();
272 }
273 return NULL;
274 }
275
276 XmlElement *
277 XmlElement::FirstNamed(const StaticQName& name) {
278 XmlChild* pChild;
279 for (pChild = first_child_; pChild; pChild = pChild->next_child_) {
280 if (!pChild->IsText() && pChild->AsElement()->Name() == name)
281 return pChild->AsElement();
282 }
283 return NULL;
284 }
285
286 XmlElement *
287 XmlElement::NextNamed(const QName& name) {
288 XmlChild* pChild;
289 for (pChild = next_child_; pChild; pChild = pChild->next_child_) {
290 if (!pChild->IsText() && pChild->AsElement()->Name() == name)
291 return pChild->AsElement();
292 }
293 return NULL;
294 }
295
296 XmlElement *
297 XmlElement::NextNamed(const StaticQName& name) {
298 XmlChild* pChild;
299 for (pChild = next_child_; pChild; pChild = pChild->next_child_) {
300 if (!pChild->IsText() && pChild->AsElement()->Name() == name)
301 return pChild->AsElement();
302 }
303 return NULL;
304 }
305
306 XmlElement* XmlElement::FindOrAddNamedChild(const QName& name) {
307 XmlElement* child = FirstNamed(name);
308 if (!child) {
309 child = new XmlElement(name);
310 AddElement(child);
311 }
312
313 return child;
314 }
315
316 const std::string XmlElement::TextNamed(const QName& name) const {
317 XmlChild* pChild;
318 for (pChild = first_child_; pChild; pChild = pChild->next_child_) {
319 if (!pChild->IsText() && pChild->AsElement()->Name() == name)
320 return pChild->AsElement()->BodyText();
321 }
322 return std::string();
323 }
324
325 void XmlElement::InsertChildAfter(XmlChild* predecessor, XmlChild* next) {
326 if (predecessor == NULL) {
327 next->next_child_ = first_child_;
328 first_child_ = next;
329 }
330 else {
331 next->next_child_ = predecessor->next_child_;
332 predecessor->next_child_ = next;
333 }
334 }
335
336 void XmlElement::RemoveChildAfter(XmlChild* predecessor) {
337 XmlChild* next;
338
339 if (predecessor == NULL) {
340 next = first_child_;
341 first_child_ = next->next_child_;
342 }
343 else {
344 next = predecessor->next_child_;
345 predecessor->next_child_ = next->next_child_;
346 }
347
348 if (last_child_ == next)
349 last_child_ = predecessor;
350
351 delete next;
352 }
353
354 void XmlElement::AddAttr(const QName& name, const std::string& value) {
355 ASSERT(!HasAttr(name));
356
357 XmlAttr ** pprev = last_attr_ ? &(last_attr_->next_attr_) : &first_attr_;
358 last_attr_ = (*pprev = new XmlAttr(name, value));
359 }
360
361 void XmlElement::AddAttr(const QName& name, const std::string& value,
362 int depth) {
363 XmlElement* element = this;
364 while (depth--) {
365 element = element->last_child_->AsElement();
366 }
367 element->AddAttr(name, value);
368 }
369
370 void XmlElement::AddParsedText(const char* cstr, int len) {
371 if (len == 0)
372 return;
373
374 if (last_child_ && last_child_->IsText()) {
375 last_child_->AsText()->AddParsedText(cstr, len);
376 return;
377 }
378 XmlChild ** pprev = last_child_ ? &(last_child_->next_child_) : &first_child_;
379 last_child_ = *pprev = new XmlText(cstr, len);
380 }
381
382 void XmlElement::AddCDATAText(const char* buf, int len) {
383 cdata_ = true;
384 AddParsedText(buf, len);
385 }
386
387 void XmlElement::AddText(const std::string& text) {
388 if (text == STR_EMPTY)
389 return;
390
391 if (last_child_ && last_child_->IsText()) {
392 last_child_->AsText()->AddText(text);
393 return;
394 }
395 XmlChild ** pprev = last_child_ ? &(last_child_->next_child_) : &first_child_;
396 last_child_ = *pprev = new XmlText(text);
397 }
398
399 void XmlElement::AddText(const std::string& text, int depth) {
400 // note: the first syntax is ambigious for msvc 6
401 // XmlElement* pel(this);
402 XmlElement* element = this;
403 while (depth--) {
404 element = element->last_child_->AsElement();
405 }
406 element->AddText(text);
407 }
408
409 void XmlElement::AddElement(XmlElement *child) {
410 if (child == NULL)
411 return;
412
413 XmlChild ** pprev = last_child_ ? &(last_child_->next_child_) : &first_child_;
414 *pprev = child;
415 last_child_ = child;
416 child->next_child_ = NULL;
417 }
418
419 void XmlElement::AddElement(XmlElement *child, int depth) {
420 XmlElement* element = this;
421 while (depth--) {
422 element = element->last_child_->AsElement();
423 }
424 element->AddElement(child);
425 }
426
427 void XmlElement::ClearNamedChildren(const QName& name) {
428 XmlChild* prev_child = NULL;
429 XmlChild* next_child;
430 XmlChild* child;
431 for (child = FirstChild(); child; child = next_child) {
432 next_child = child->NextChild();
433 if (!child->IsText() && child->AsElement()->Name() == name)
434 {
435 RemoveChildAfter(prev_child);
436 continue;
437 }
438 prev_child = child;
439 }
440 }
441
442 void XmlElement::ClearAttributes() {
443 XmlAttr* attr;
444 for (attr = first_attr_; attr; ) {
445 XmlAttr* to_delete = attr;
446 attr = attr->next_attr_;
447 delete to_delete;
448 }
449 first_attr_ = last_attr_ = NULL;
450 }
451
452 void XmlElement::ClearChildren() {
453 XmlChild* pchild;
454 for (pchild = first_child_; pchild; ) {
455 XmlChild* to_delete = pchild;
456 pchild = pchild->next_child_;
457 delete to_delete;
458 }
459 first_child_ = last_child_ = NULL;
460 }
461
462 std::string XmlElement::Str() const {
463 std::stringstream ss;
464 XmlPrinter::PrintXml(&ss, this);
465 return ss.str();
466 }
467
468 XmlElement* XmlElement::ForStr(const std::string& str) {
469 XmlBuilder builder;
470 XmlParser::ParseXml(&builder, str);
471 return builder.CreateElement();
472 }
473
474 XmlElement::~XmlElement() {
475 XmlAttr* attr;
476 for (attr = first_attr_; attr; ) {
477 XmlAttr* to_delete = attr;
478 attr = attr->next_attr_;
479 delete to_delete;
480 }
481
482 XmlChild* pchild;
483 for (pchild = first_child_; pchild; ) {
484 XmlChild* to_delete = pchild;
485 pchild = pchild->next_child_;
486 delete to_delete;
487 }
488 }
489
490 } // namespace buzz
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698