OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2010 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 #include "webrtc/libjingle/session/parsing.h" | |
12 | |
13 #include <stdlib.h> | |
14 #include <algorithm> | |
15 #include "webrtc/base/stringutils.h" | |
16 | |
17 namespace { | |
18 static const char kTrue[] = "true"; | |
19 static const char kOne[] = "1"; | |
20 } | |
21 | |
22 namespace cricket { | |
23 | |
24 bool BadParse(const std::string& text, ParseError* err) { | |
25 if (err != NULL) { | |
26 err->text = text; | |
27 } | |
28 return false; | |
29 } | |
30 | |
31 bool BadWrite(const std::string& text, WriteError* err) { | |
32 if (err != NULL) { | |
33 err->text = text; | |
34 } | |
35 return false; | |
36 } | |
37 | |
38 std::string GetXmlAttr(const buzz::XmlElement* elem, | |
39 const buzz::QName& name, | |
40 const std::string& def) { | |
41 std::string val = elem->Attr(name); | |
42 return val.empty() ? def : val; | |
43 } | |
44 | |
45 std::string GetXmlAttr(const buzz::XmlElement* elem, | |
46 const buzz::QName& name, | |
47 const char* def) { | |
48 return GetXmlAttr(elem, name, std::string(def)); | |
49 } | |
50 | |
51 bool GetXmlAttr(const buzz::XmlElement* elem, | |
52 const buzz::QName& name, bool def) { | |
53 std::string val = elem->Attr(name); | |
54 std::transform(val.begin(), val.end(), val.begin(), tolower); | |
55 | |
56 return val.empty() ? def : (val == kTrue || val == kOne); | |
57 } | |
58 | |
59 int GetXmlAttr(const buzz::XmlElement* elem, | |
60 const buzz::QName& name, int def) { | |
61 std::string val = elem->Attr(name); | |
62 return val.empty() ? def : atoi(val.c_str()); | |
63 } | |
64 | |
65 const buzz::XmlElement* GetXmlChild(const buzz::XmlElement* parent, | |
66 const std::string& name) { | |
67 for (const buzz::XmlElement* child = parent->FirstElement(); | |
68 child != NULL; | |
69 child = child->NextElement()) { | |
70 if (child->Name().LocalPart() == name) { | |
71 return child; | |
72 } | |
73 } | |
74 return NULL; | |
75 } | |
76 | |
77 bool RequireXmlChild(const buzz::XmlElement* parent, | |
78 const std::string& name, | |
79 const buzz::XmlElement** child, | |
80 ParseError* error) { | |
81 *child = GetXmlChild(parent, name); | |
82 if (*child == NULL) { | |
83 return BadParse("element '" + parent->Name().Merged() + | |
84 "' missing required child '" + name, | |
85 error); | |
86 } else { | |
87 return true; | |
88 } | |
89 } | |
90 | |
91 bool RequireXmlAttr(const buzz::XmlElement* elem, | |
92 const buzz::QName& name, | |
93 std::string* value, | |
94 ParseError* error) { | |
95 if (!elem->HasAttr(name)) { | |
96 return BadParse("element '" + elem->Name().Merged() + | |
97 "' missing required attribute '" | |
98 + name.Merged() + "'", | |
99 error); | |
100 } else { | |
101 *value = elem->Attr(name); | |
102 return true; | |
103 } | |
104 } | |
105 | |
106 void AddXmlAttrIfNonEmpty(buzz::XmlElement* elem, | |
107 const buzz::QName name, | |
108 const std::string& value) { | |
109 if (!value.empty()) { | |
110 elem->AddAttr(name, value); | |
111 } | |
112 } | |
113 | |
114 void AddXmlChildren(buzz::XmlElement* parent, | |
115 const std::vector<buzz::XmlElement*>& children) { | |
116 for (std::vector<buzz::XmlElement*>::const_iterator iter = children.begin(); | |
117 iter != children.end(); | |
118 iter++) { | |
119 parent->AddElement(*iter); | |
120 } | |
121 } | |
122 | |
123 void CopyXmlChildren(const buzz::XmlElement* source, buzz::XmlElement* dest) { | |
124 for (const buzz::XmlElement* child = source->FirstElement(); | |
125 child != NULL; | |
126 child = child->NextElement()) { | |
127 dest->AddElement(new buzz::XmlElement(*child)); | |
128 } | |
129 } | |
130 | |
131 std::vector<buzz::XmlElement*> CopyOfXmlChildren(const buzz::XmlElement* elem) { | |
132 std::vector<buzz::XmlElement*> children; | |
133 for (const buzz::XmlElement* child = elem->FirstElement(); | |
134 child != NULL; | |
135 child = child->NextElement()) { | |
136 children.push_back(new buzz::XmlElement(*child)); | |
137 } | |
138 return children; | |
139 } | |
140 | |
141 } // namespace cricket | |
OLD | NEW |