• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * libjingle
3  * Copyright 2010, Google Inc.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
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.
15  *
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.
26  */
27 
28 #ifndef TALK_P2P_BASE_PARSING_H_
29 #define TALK_P2P_BASE_PARSING_H_
30 
31 #include <string>
32 #include <vector>
33 #include "talk/base/basictypes.h"
34 #include "talk/xmllite/xmlelement.h"  // Needed to delete ParseError.extra.
35 
36 namespace cricket {
37 
38 // We decided "bool Parse(in, out*, error*)" is generally the best
39 // parse signature.  "out Parse(in)" doesn't allow for errors.
40 // "error* Parse(in, out*)" doesn't allow flexible memory management.
41 
42 // The error type for parsing.
43 struct ParseError {
44  public:
45   // explains the error
46   std::string text;
47   // provide details about what wasn't parsable
48   const buzz::XmlElement* extra;
49 
ParseErrorParseError50   ParseError() : extra(NULL) {}
51 
~ParseErrorParseError52   ~ParseError() {
53     delete extra;
54   }
55 
SetTextParseError56   void SetText(const std::string& text) {
57     this->text = text;
58   }
59 };
60 
61 // The error type for writing.
62 struct WriteError {
63   std::string text;
64 
SetTextWriteError65   void SetText(const std::string& text) {
66     this->text = text;
67   }
68 };
69 
70 // Convenience method for returning a message when parsing fails.
71 bool BadParse(const std::string& text, ParseError* err);
72 
73 // Convenience method for returning a message when writing fails.
74 bool BadWrite(const std::string& text, WriteError* error);
75 
76 // helper XML functions
77 std::string GetXmlAttr(const buzz::XmlElement* elem,
78                        const buzz::QName& name,
79                        const std::string& def);
80 std::string GetXmlAttr(const buzz::XmlElement* elem,
81                        const buzz::QName& name,
82                        const char* def);
83 // Return true if the value is "true" or "1".
84 bool GetXmlAttr(const buzz::XmlElement* elem,
85                 const buzz::QName& name, bool def);
86 int GetXmlAttr(const buzz::XmlElement* elem,
87                const buzz::QName& name, int def);
88 void AddXmlAttr(buzz::XmlElement* elem,
89                 const buzz::QName& name, int n);
90 void SetXmlBody(buzz::XmlElement* elem, uint32 u);
91 const buzz::XmlElement* GetXmlChild(const buzz::XmlElement* parent,
92                                     const std::string& name);
93 bool RequireXmlChild(const buzz::XmlElement* parent,
94                      const std::string& name,
95                      const buzz::XmlElement** child,
96                      ParseError* error);
97 bool RequireXmlAttr(const buzz::XmlElement* elem,
98                     const buzz::QName& name,
99                     std::string* value,
100                     ParseError* error);
101 void AddXmlChildren(buzz::XmlElement* parent,
102                     const std::vector<buzz::XmlElement*>& children);
103 void CopyXmlChildren(const buzz::XmlElement* source, buzz::XmlElement* dest);
104 std::vector<buzz::XmlElement*> CopyOfXmlChildren(const buzz::XmlElement* elem);
105 
106 }  // namespace cricket
107 
108 #endif  // TALK_P2P_BASE_PARSING_H_
109