1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
// OpenVPN -- An application to securely tunnel IP networks
// over a single port, with support for SSL/TLS-based
// session authentication and key exchange,
// packet encryption, packet authentication, and
// packet compression.
//
// Copyright (C) 2012-2022 OpenVPN Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License Version 3
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program in the COPYING file.
// If not, see <http://www.gnu.org/licenses/>.
#pragma once
#include <string>
#include <tinyxml2.h>
namespace openvpn {
class Xml
{
public:
OPENVPN_EXCEPTION(xml_parse);
struct Document : public tinyxml2::XMLDocument
{
Document(const std::string &str,
const std::string &title)
{
if (tinyxml2::XMLDocument::Parse(str.c_str()))
OPENVPN_THROW(xml_parse, title << " : " << format_error(*this));
}
};
static std::string to_string(const tinyxml2::XMLDocument &doc)
{
tinyxml2::XMLPrinter printer;
doc.Print(&printer);
return printer.CStr();
}
static std::string format_error(const tinyxml2::XMLDocument &doc)
{
#if OVPN_TINYXML2_HAS_ERROR_NAME
std::string ret = doc.ErrorName();
#else
tinyxml2::XMLError error = doc.ErrorID();
std::string ret{"XMLError " + error};
#endif
#if OVPN_TINYXML2_HAS_ERROR_STR
const char *es = doc.ErrorStr();
if (es)
{
ret += ' ';
ret += es;
}
#else
const char *es1 = doc.GetErrorStr1();
const char *es2 = doc.GetErrorStr2();
if (es1)
{
ret += ' ';
ret += es1;
}
if (es2)
{
ret += ' ';
ret += es2;
}
#endif
return ret;
}
template <typename T, typename... Args>
static std::string find_text(const tinyxml2::XMLNode *node,
const T &first,
Args... args)
{
const tinyxml2::XMLElement *e = find(node, first, args...);
if (e)
return e->GetText();
else
return std::string();
}
template <typename T, typename... Args>
static const tinyxml2::XMLElement *find(const tinyxml2::XMLNode *node,
const T &first,
Args... args)
{
const tinyxml2::XMLElement *e = find(node, first);
if (e)
e = find(e, args...);
return e;
}
static const tinyxml2::XMLElement *find(const tinyxml2::XMLNode *node,
const std::string &first)
{
return node->FirstChildElement(first.c_str());
}
static const tinyxml2::XMLElement *find(const tinyxml2::XMLNode *node,
const char *first)
{
return node->FirstChildElement(first);
}
static const tinyxml2::XMLElement *find(const tinyxml2::XMLElement *elem)
{
return elem;
}
static const tinyxml2::XMLElement *next_sibling(const tinyxml2::XMLNode *node,
const std::string &name)
{
return node->NextSiblingElement(name.c_str());
}
static const tinyxml2::XMLElement *next_sibling(const tinyxml2::XMLNode *node,
const char *name)
{
return node->NextSiblingElement(name);
}
static const tinyxml2::XMLElement *next_sibling(const tinyxml2::XMLNode *node)
{
return node->NextSiblingElement();
}
};
} // namespace openvpn
|