OpenVPN 3 Core Library
Loading...
Searching...
No Matches
xmlhelper.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12#pragma once
13
14#include <string>
15
16#include <tinyxml2.h>
17
18namespace openvpn {
19
20class Xml
21{
22 public:
24
25 struct Document : public tinyxml2::XMLDocument
26 {
27 Document(const std::string &str,
28 const std::string &title)
29 {
30 if (tinyxml2::XMLDocument::Parse(str.c_str()))
31 OPENVPN_THROW(xml_parse, title << " : " << format_error(*this));
32 }
33 };
34
35 static std::string to_string(const tinyxml2::XMLDocument &doc)
36 {
37 tinyxml2::XMLPrinter printer;
38 doc.Print(&printer);
39 return printer.CStr();
40 }
41
42 static std::string format_error(const tinyxml2::XMLDocument &doc)
43 {
44 std::string ret = doc.ErrorName();
45 const char *es = doc.ErrorStr();
46 if (es)
47 {
48 ret += ' ';
49 ret += es;
50 }
51 return ret;
52 }
53
54 template <typename T, typename... Args>
55 static std::string find_text(const tinyxml2::XMLNode *node,
56 const T &first,
57 Args... args)
58 {
59 const tinyxml2::XMLElement *e = find(node, first, args...);
60 if (e)
61 return e->GetText();
62 else
63 return std::string();
64 }
65
66 template <typename T, typename... Args>
67 static const tinyxml2::XMLElement *find(const tinyxml2::XMLNode *node,
68 const T &first,
69 Args... args)
70 {
71 const tinyxml2::XMLElement *e = find(node, first);
72 if (e)
73 e = find(e, args...);
74 return e;
75 }
76
77 static const tinyxml2::XMLElement *find(const tinyxml2::XMLNode *node,
78 const std::string &first)
79 {
80 return node->FirstChildElement(first.c_str());
81 }
82
83 static const tinyxml2::XMLElement *find(const tinyxml2::XMLNode *node,
84 const char *first)
85 {
86 return node->FirstChildElement(first);
87 }
88
89 static const tinyxml2::XMLElement *find(const tinyxml2::XMLElement *elem)
90 {
91 return elem;
92 }
93
94 static const tinyxml2::XMLElement *next_sibling(const tinyxml2::XMLNode *node,
95 const std::string &name)
96 {
97 return node->NextSiblingElement(name.c_str());
98 }
99
100 static const tinyxml2::XMLElement *next_sibling(const tinyxml2::XMLNode *node,
101 const char *name)
102 {
103 return node->NextSiblingElement(name);
104 }
105
106 static const tinyxml2::XMLElement *next_sibling(const tinyxml2::XMLNode *node)
107 {
108 return node->NextSiblingElement();
109 }
110};
111} // namespace openvpn
static std::string format_error(const tinyxml2::XMLDocument &doc)
Definition xmlhelper.hpp:42
static const tinyxml2::XMLElement * find(const tinyxml2::XMLNode *node, const char *first)
Definition xmlhelper.hpp:83
OPENVPN_EXCEPTION(xml_parse)
static const tinyxml2::XMLElement * next_sibling(const tinyxml2::XMLNode *node, const char *name)
static const tinyxml2::XMLElement * find(const tinyxml2::XMLNode *node, const T &first, Args... args)
Definition xmlhelper.hpp:67
static const tinyxml2::XMLElement * next_sibling(const tinyxml2::XMLNode *node)
static const tinyxml2::XMLElement * find(const tinyxml2::XMLElement *elem)
Definition xmlhelper.hpp:89
static const tinyxml2::XMLElement * next_sibling(const tinyxml2::XMLNode *node, const std::string &name)
Definition xmlhelper.hpp:94
static std::string find_text(const tinyxml2::XMLNode *node, const T &first, Args... args)
Definition xmlhelper.hpp:55
static std::string to_string(const tinyxml2::XMLDocument &doc)
Definition xmlhelper.hpp:35
static const tinyxml2::XMLElement * find(const tinyxml2::XMLNode *node, const std::string &first)
Definition xmlhelper.hpp:77
#define OPENVPN_THROW(exc, stuff)
Document(const std::string &str, const std::string &title)
Definition xmlhelper.hpp:27
os<< "Session Name: "<< tbc-> session_name<< '\n';os<< "Layer: "<< tbc-> layer str()<< '\n'
std::string ret