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#if OVPN_TINYXML2_HAS_ERROR_NAME
45 std::string ret = doc.ErrorName();
46#else
47 tinyxml2::XMLError error = doc.ErrorID();
48 std::string ret{"XMLError " + error};
49#endif
50#if OVPN_TINYXML2_HAS_ERROR_STR
51 const char *es = doc.ErrorStr();
52 if (es)
53 {
54 ret += ' ';
55 ret += es;
56 }
57#else
58 const char *es1 = doc.GetErrorStr1();
59 const char *es2 = doc.GetErrorStr2();
60 if (es1)
61 {
62 ret += ' ';
63 ret += es1;
64 }
65 if (es2)
66 {
67 ret += ' ';
68 ret += es2;
69 }
70#endif
71 return ret;
72 }
73
74 template <typename T, typename... Args>
75 static std::string find_text(const tinyxml2::XMLNode *node,
76 const T &first,
77 Args... args)
78 {
79 const tinyxml2::XMLElement *e = find(node, first, args...);
80 if (e)
81 return e->GetText();
82 else
83 return std::string();
84 }
85
86 template <typename T, typename... Args>
87 static const tinyxml2::XMLElement *find(const tinyxml2::XMLNode *node,
88 const T &first,
89 Args... args)
90 {
91 const tinyxml2::XMLElement *e = find(node, first);
92 if (e)
93 e = find(e, args...);
94 return e;
95 }
96
97 static const tinyxml2::XMLElement *find(const tinyxml2::XMLNode *node,
98 const std::string &first)
99 {
100 return node->FirstChildElement(first.c_str());
101 }
102
103 static const tinyxml2::XMLElement *find(const tinyxml2::XMLNode *node,
104 const char *first)
105 {
106 return node->FirstChildElement(first);
107 }
108
109 static const tinyxml2::XMLElement *find(const tinyxml2::XMLElement *elem)
110 {
111 return elem;
112 }
113
114 static const tinyxml2::XMLElement *next_sibling(const tinyxml2::XMLNode *node,
115 const std::string &name)
116 {
117 return node->NextSiblingElement(name.c_str());
118 }
119
120 static const tinyxml2::XMLElement *next_sibling(const tinyxml2::XMLNode *node,
121 const char *name)
122 {
123 return node->NextSiblingElement(name);
124 }
125
126 static const tinyxml2::XMLElement *next_sibling(const tinyxml2::XMLNode *node)
127 {
128 return node->NextSiblingElement();
129 }
130};
131} // 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)
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:87
static const tinyxml2::XMLElement * next_sibling(const tinyxml2::XMLNode *node)
static const tinyxml2::XMLElement * find(const tinyxml2::XMLElement *elem)
static const tinyxml2::XMLElement * next_sibling(const tinyxml2::XMLNode *node, const std::string &name)
static std::string find_text(const tinyxml2::XMLNode *node, const T &first, Args... args)
Definition xmlhelper.hpp:75
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:97
#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