File: xmlhelper.hpp

package info (click to toggle)
openvpn3-client 25%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,276 kB
  • sloc: cpp: 190,085; python: 7,218; ansic: 1,866; sh: 1,361; java: 402; lisp: 81; makefile: 17
file content (131 lines) | stat: -rw-r--r-- 3,650 bytes parent folder | download
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
//    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- OpenVPN Inc.
//
//    SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
//

#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