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 #ifndef WEBRTC_LIBJINGLE_SESSION_PARSING_H_ | |
12 #define WEBRTC_LIBJINGLE_SESSION_PARSING_H_ | |
13 | |
14 #include <string> | |
15 #include <vector> | |
16 #include "webrtc/libjingle/xmllite/xmlelement.h" // Needed to delete ParseError
.extra. | |
17 #include "webrtc/base/basictypes.h" | |
18 #include "webrtc/base/stringencode.h" | |
19 | |
20 namespace cricket { | |
21 | |
22 typedef std::vector<buzz::XmlElement*> XmlElements; | |
23 | |
24 // We decided "bool Parse(in, out*, error*)" is generally the best | |
25 // parse signature. "out Parse(in)" doesn't allow for errors. | |
26 // "error* Parse(in, out*)" doesn't allow flexible memory management. | |
27 | |
28 // The error type for parsing. | |
29 struct ParseError { | |
30 public: | |
31 // explains the error | |
32 std::string text; | |
33 // provide details about what wasn't parsable | |
34 const buzz::XmlElement* extra; | |
35 | |
36 ParseError() : extra(NULL) {} | |
37 | |
38 ~ParseError() { | |
39 delete extra; | |
40 } | |
41 | |
42 void SetText(const std::string& text) { | |
43 this->text = text; | |
44 } | |
45 }; | |
46 | |
47 // The error type for writing. | |
48 struct WriteError { | |
49 std::string text; | |
50 | |
51 void SetText(const std::string& text) { | |
52 this->text = text; | |
53 } | |
54 }; | |
55 | |
56 // Convenience method for returning a message when parsing fails. | |
57 bool BadParse(const std::string& text, ParseError* err); | |
58 | |
59 // Convenience method for returning a message when writing fails. | |
60 bool BadWrite(const std::string& text, WriteError* error); | |
61 | |
62 // helper XML functions | |
63 std::string GetXmlAttr(const buzz::XmlElement* elem, | |
64 const buzz::QName& name, | |
65 const std::string& def); | |
66 std::string GetXmlAttr(const buzz::XmlElement* elem, | |
67 const buzz::QName& name, | |
68 const char* def); | |
69 // Return true if the value is "true" or "1". | |
70 bool GetXmlAttr(const buzz::XmlElement* elem, | |
71 const buzz::QName& name, bool def); | |
72 int GetXmlAttr(const buzz::XmlElement* elem, | |
73 const buzz::QName& name, int def); | |
74 | |
75 template <class T> | |
76 bool GetXmlAttr(const buzz::XmlElement* elem, | |
77 const buzz::QName& name, | |
78 T* val_out) { | |
79 if (!elem->HasAttr(name)) { | |
80 return false; | |
81 } | |
82 std::string unparsed = elem->Attr(name); | |
83 return rtc::FromString(unparsed, val_out); | |
84 } | |
85 | |
86 template <class T> | |
87 bool GetXmlAttr(const buzz::XmlElement* elem, | |
88 const buzz::QName& name, | |
89 const T& def, | |
90 T* val_out) { | |
91 if (!elem->HasAttr(name)) { | |
92 *val_out = def; | |
93 return true; | |
94 } | |
95 return GetXmlAttr(elem, name, val_out); | |
96 } | |
97 | |
98 template <class T> | |
99 bool AddXmlAttr(buzz::XmlElement* elem, | |
100 const buzz::QName& name, const T& val) { | |
101 std::string buf; | |
102 if (!rtc::ToString(val, &buf)) { | |
103 return false; | |
104 } | |
105 elem->AddAttr(name, buf); | |
106 return true; | |
107 } | |
108 | |
109 template <class T> | |
110 bool SetXmlBody(buzz::XmlElement* elem, const T& val) { | |
111 std::string buf; | |
112 if (!rtc::ToString(val, &buf)) { | |
113 return false; | |
114 } | |
115 elem->SetBodyText(buf); | |
116 return true; | |
117 } | |
118 | |
119 const buzz::XmlElement* GetXmlChild(const buzz::XmlElement* parent, | |
120 const std::string& name); | |
121 | |
122 bool RequireXmlChild(const buzz::XmlElement* parent, | |
123 const std::string& name, | |
124 const buzz::XmlElement** child, | |
125 ParseError* error); | |
126 bool RequireXmlAttr(const buzz::XmlElement* elem, | |
127 const buzz::QName& name, | |
128 std::string* value, | |
129 ParseError* error); | |
130 void AddXmlAttrIfNonEmpty(buzz::XmlElement* elem, | |
131 const buzz::QName name, | |
132 const std::string& value); | |
133 void AddXmlChildren(buzz::XmlElement* parent, | |
134 const std::vector<buzz::XmlElement*>& children); | |
135 void CopyXmlChildren(const buzz::XmlElement* source, buzz::XmlElement* dest); | |
136 std::vector<buzz::XmlElement*> CopyOfXmlChildren(const buzz::XmlElement* elem); | |
137 | |
138 } // namespace cricket | |
139 | |
140 #endif // WEBRTC_LIBJINGLE_SESSION_PARSING_H_ | |
OLD | NEW |