3 * Copyright 2010, 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 WEBRTC_P2P_BASE_PARSING_H_
29 #define WEBRTC_P2P_BASE_PARSING_H_
33 #include "webrtc/libjingle/xmllite/xmlelement.h" // Needed to delete ParseError.extra.
34 #include "webrtc/base/basictypes.h"
35 #include "webrtc/base/stringencode.h"
39 typedef std::vector<buzz::XmlElement*> XmlElements;
41 // We decided "bool Parse(in, out*, error*)" is generally the best
42 // parse signature. "out Parse(in)" doesn't allow for errors.
43 // "error* Parse(in, out*)" doesn't allow flexible memory management.
45 // The error type for parsing.
50 // provide details about what wasn't parsable
51 const buzz::XmlElement* extra;
53 ParseError() : extra(NULL) {}
59 void SetText(const std::string& text) {
64 // The error type for writing.
68 void SetText(const std::string& text) {
73 // Convenience method for returning a message when parsing fails.
74 bool BadParse(const std::string& text, ParseError* err);
76 // Convenience method for returning a message when writing fails.
77 bool BadWrite(const std::string& text, WriteError* error);
79 // helper XML functions
80 std::string GetXmlAttr(const buzz::XmlElement* elem,
81 const buzz::QName& name,
82 const std::string& def);
83 std::string GetXmlAttr(const buzz::XmlElement* elem,
84 const buzz::QName& name,
86 // Return true if the value is "true" or "1".
87 bool GetXmlAttr(const buzz::XmlElement* elem,
88 const buzz::QName& name, bool def);
89 int GetXmlAttr(const buzz::XmlElement* elem,
90 const buzz::QName& name, int def);
93 bool GetXmlAttr(const buzz::XmlElement* elem,
94 const buzz::QName& name,
96 if (!elem->HasAttr(name)) {
99 std::string unparsed = elem->Attr(name);
100 return rtc::FromString(unparsed, val_out);
104 bool GetXmlAttr(const buzz::XmlElement* elem,
105 const buzz::QName& name,
108 if (!elem->HasAttr(name)) {
112 return GetXmlAttr(elem, name, val_out);
116 bool AddXmlAttr(buzz::XmlElement* elem,
117 const buzz::QName& name, const T& val) {
119 if (!rtc::ToString(val, &buf)) {
122 elem->AddAttr(name, buf);
127 bool SetXmlBody(buzz::XmlElement* elem, const T& val) {
129 if (!rtc::ToString(val, &buf)) {
132 elem->SetBodyText(buf);
136 const buzz::XmlElement* GetXmlChild(const buzz::XmlElement* parent,
137 const std::string& name);
139 bool RequireXmlChild(const buzz::XmlElement* parent,
140 const std::string& name,
141 const buzz::XmlElement** child,
143 bool RequireXmlAttr(const buzz::XmlElement* elem,
144 const buzz::QName& name,
147 void AddXmlAttrIfNonEmpty(buzz::XmlElement* elem,
148 const buzz::QName name,
149 const std::string& value);
150 void AddXmlChildren(buzz::XmlElement* parent,
151 const std::vector<buzz::XmlElement*>& children);
152 void CopyXmlChildren(const buzz::XmlElement* source, buzz::XmlElement* dest);
153 std::vector<buzz::XmlElement*> CopyOfXmlChildren(const buzz::XmlElement* elem);
155 } // namespace cricket
157 #endif // WEBRTC_P2P_BASE_PARSING_H_