henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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/base/stringencode.h" |
| 35 | #include "talk/xmllite/xmlelement.h" // Needed to delete ParseError.extra. |
| 36 | |
| 37 | namespace cricket { |
| 38 | |
| 39 | typedef std::vector<buzz::XmlElement*> XmlElements; |
| 40 | |
| 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. |
| 44 | |
| 45 | // The error type for parsing. |
| 46 | struct ParseError { |
| 47 | public: |
| 48 | // explains the error |
| 49 | std::string text; |
| 50 | // provide details about what wasn't parsable |
| 51 | const buzz::XmlElement* extra; |
| 52 | |
| 53 | ParseError() : extra(NULL) {} |
| 54 | |
| 55 | ~ParseError() { |
| 56 | delete extra; |
| 57 | } |
| 58 | |
| 59 | void SetText(const std::string& text) { |
| 60 | this->text = text; |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | // The error type for writing. |
| 65 | struct WriteError { |
| 66 | std::string text; |
| 67 | |
| 68 | void SetText(const std::string& text) { |
| 69 | this->text = text; |
| 70 | } |
| 71 | }; |
| 72 | |
| 73 | // Convenience method for returning a message when parsing fails. |
| 74 | bool BadParse(const std::string& text, ParseError* err); |
| 75 | |
| 76 | // Convenience method for returning a message when writing fails. |
| 77 | bool BadWrite(const std::string& text, WriteError* error); |
| 78 | |
| 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, |
| 85 | const char* def); |
| 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); |
| 91 | |
| 92 | template <class T> |
| 93 | bool GetXmlAttr(const buzz::XmlElement* elem, |
| 94 | const buzz::QName& name, |
| 95 | T* val_out) { |
| 96 | if (!elem->HasAttr(name)) { |
| 97 | return false; |
| 98 | } |
| 99 | std::string unparsed = elem->Attr(name); |
| 100 | return talk_base::FromString(unparsed, val_out); |
| 101 | } |
| 102 | |
| 103 | template <class T> |
| 104 | bool GetXmlAttr(const buzz::XmlElement* elem, |
| 105 | const buzz::QName& name, |
| 106 | const T& def, |
| 107 | T* val_out) { |
| 108 | if (!elem->HasAttr(name)) { |
| 109 | *val_out = def; |
| 110 | return true; |
| 111 | } |
| 112 | return GetXmlAttr(elem, name, val_out); |
| 113 | } |
| 114 | |
| 115 | template <class T> |
| 116 | bool AddXmlAttr(buzz::XmlElement* elem, |
| 117 | const buzz::QName& name, const T& val) { |
| 118 | std::string buf; |
| 119 | if (!talk_base::ToString(val, &buf)) { |
| 120 | return false; |
| 121 | } |
| 122 | elem->AddAttr(name, buf); |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | template <class T> |
| 127 | bool SetXmlBody(buzz::XmlElement* elem, const T& val) { |
| 128 | std::string buf; |
| 129 | if (!talk_base::ToString(val, &buf)) { |
| 130 | return false; |
| 131 | } |
| 132 | elem->SetBodyText(buf); |
| 133 | return true; |
| 134 | } |
| 135 | |
| 136 | const buzz::XmlElement* GetXmlChild(const buzz::XmlElement* parent, |
| 137 | const std::string& name); |
| 138 | |
| 139 | bool RequireXmlChild(const buzz::XmlElement* parent, |
| 140 | const std::string& name, |
| 141 | const buzz::XmlElement** child, |
| 142 | ParseError* error); |
| 143 | bool RequireXmlAttr(const buzz::XmlElement* elem, |
| 144 | const buzz::QName& name, |
| 145 | std::string* value, |
| 146 | ParseError* error); |
| 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); |
| 154 | |
| 155 | } // namespace cricket |
| 156 | |
| 157 | #endif // TALK_P2P_BASE_PARSING_H_ |