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 TALK_P2P_BASE_PARSING_H_
29
#define TALK_P2P_BASE_PARSING_H_
33
#include "talk/base/basictypes.h"
34
#include "talk/base/stringencode.h"
35
#include "talk/xmllite/xmlelement.h" // Needed to delete ParseError.extra.
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 AddXmlAttr(buzz::XmlElement* elem,
94
const buzz::QName& name, const T& val) {
96
if (!talk_base::ToString(val, &buf)) {
99
elem->AddAttr(name, buf);
104
bool SetXmlBody(buzz::XmlElement* elem, const T& val) {
106
if (!talk_base::ToString(val, &buf)) {
109
elem->SetBodyText(buf);
113
const buzz::XmlElement* GetXmlChild(const buzz::XmlElement* parent,
114
const std::string& name);
116
bool RequireXmlChild(const buzz::XmlElement* parent,
117
const std::string& name,
118
const buzz::XmlElement** child,
120
bool RequireXmlAttr(const buzz::XmlElement* elem,
121
const buzz::QName& name,
124
void AddXmlAttrIfNonEmpty(buzz::XmlElement* elem,
125
const buzz::QName name,
126
const std::string& value);
127
void AddXmlChildren(buzz::XmlElement* parent,
128
const std::vector<buzz::XmlElement*>& children);
129
void CopyXmlChildren(const buzz::XmlElement* source, buzz::XmlElement* dest);
130
std::vector<buzz::XmlElement*> CopyOfXmlChildren(const buzz::XmlElement* elem);
132
} // namespace cricket
134
#endif // TALK_P2P_BASE_PARSING_H_