aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Common/XMLUtils.cpp
blob: db84e00adce5408d53fc0786af7312c3ae39611e (plain) (blame)
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
#include <string>
#include <Common/XMLUtils.h>
#include <Poco/DOM/Document.h>
#include <Poco/Util/AbstractConfiguration.h>

namespace DB::XMLUtils
{

using namespace Poco;
using namespace Poco::XML;

using XMLDocumentPtr = Poco::AutoPtr<Document>;

Node * getRootNode(Document * document)
{
    for (Node * child = document->firstChild(); child; child = child->nextSibling())
    {
        /// Besides the root element there can be comment nodes on the top level.
        /// Skip them.
        if (child->nodeType() == Node::ELEMENT_NODE)
            return child;
    }

    throw Poco::Exception("No root node in document");
}


/// This class is used to access protected parseXXX static methods from AbstractConfiguration
class ParseHelper : private Util::AbstractConfiguration
{
public:
    ParseHelper() = delete;

    using Util::AbstractConfiguration::parseInt;
    using Util::AbstractConfiguration::parseUInt;
    using Util::AbstractConfiguration::parseInt64;
    using Util::AbstractConfiguration::parseUInt64;
    using Util::AbstractConfiguration::parseBool;

    static std::string parseString(const std::string & s)
    {
        return s;
    }

    template <typename ValueType, bool ReturnDefault, typename ParseFunction>
    static ValueType getValue(const Node * node, const std::string & path,
        const ValueType & default_value, const ParseFunction & parse_function)
    {
        const auto * value_node = node->getNodeByPath(path);
        if (!value_node)
        {
            if constexpr (ReturnDefault)
                return default_value;
            else
                throw Poco::NotFoundException(path);
        }
        return parse_function(value_node->innerText());
    }
};


std::string getString(const Node * node, const std::string & path)
{
    return ParseHelper::getValue<std::string, false>(node, path, {}, ParseHelper::parseString);
}

std::string getString(const Node * node, const std::string & path, const std::string & default_value)
{
    return ParseHelper::getValue<std::string, true>(node, path, default_value, ParseHelper::parseString);
}

Int64 getInt64(const Node * node, const std::string & path)
{
    return ParseHelper::getValue<Int64, false>(node, path, {}, ParseHelper::parseInt64);
}

Int64 getInt64(const Node * node, const std::string & path, Int64 default_value)
{
    return ParseHelper::getValue<Int64, true>(node, path, default_value, ParseHelper::parseInt64);
}

UInt64 getUInt64(const Node * node, const std::string & path)
{
    return ParseHelper::getValue<UInt64, false>(node, path, {}, ParseHelper::parseUInt64);
}

UInt64 getUInt64(const Node * node, const std::string & path, UInt64 default_value)
{
    return ParseHelper::getValue<UInt64, true>(node, path, default_value, ParseHelper::parseUInt64);
}

int getInt(const Node * node, const std::string & path)
{
    return ParseHelper::getValue<int, false>(node, path, {}, ParseHelper::parseInt);
}

int getInt(const Node * node, const std::string & path, int default_value)
{
    return ParseHelper::getValue<int, true>(node, path, default_value, ParseHelper::parseInt);
}

unsigned getUInt(const Node * node, const std::string & path)
{
    return ParseHelper::getValue<unsigned, false>(node, path, {}, ParseHelper::parseUInt);
}

unsigned getUInt(const Node * node, const std::string & path, unsigned default_value)
{
    return ParseHelper::getValue<unsigned, true>(node, path, default_value, ParseHelper::parseUInt);
}

bool getBool(const Node * node, const std::string & path)
{
    return ParseHelper::getValue<bool, false>(node, path, {}, ParseHelper::parseBool);
}

bool getBool(const Node * node, const std::string & path, bool default_value)
{
    return ParseHelper::getValue<bool, true>(node, path, default_value, ParseHelper::parseBool);
}

}