summaryrefslogtreecommitdiff
path: root/src/utils/xml.h
blob: 787504bbb291ddc1d3826e6ad10f09da14bcf19f (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
 *  The Mana Client
 *  Copyright (C) 2004-2009  The Mana World Development Team
 *  Copyright (C) 2009-2012  The Mana Developers
 *
 *  This file is part of The Mana Client.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once

#include "utils/stringutils.h"

#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xmlwriter.h>

#include <string_view>

/**
 * XML helper functions.
 */
namespace XML
{
    class Node
    {
    public:
        Node(xmlNodePtr node = nullptr)
            : node(node)
        {}

        operator bool() const { return node != nullptr; }

        std::string_view name() const { return (const char *) node->name; }
        std::string_view textContent() const;

        template<typename T>
        bool attribute(const char *name, T &value);

        bool hasAttribute(const char *name) const;
        int getProperty(const char *name, int def) const;
        double getFloatProperty(const char *name, double def) const;
        std::string getProperty(const char *name, const std::string &def) const;
        bool getBoolProperty(const char *name, bool def) const;
        Node findFirstChildByName(const char *name) const;

        /**
         * Helper class to iterate over the children of a node. Enables
         * range-based for loops.
         */
        class Children
        {
        public:
            class Iterator
            {
            public:
                explicit Iterator(xmlNodePtr node) : mNode(node) {}

                bool operator!=(const Iterator &other) const { return mNode != other.mNode; }
                void operator++() { mNode = mNode->next; }
                Node operator*() const { return mNode; }

            private:
                xmlNodePtr mNode;
            };

            explicit Children(xmlNodePtr node) : mNode(node) {}

            Iterator begin() const { return Iterator(mNode->children); }
            Iterator end() const { return Iterator(nullptr); }

        private:
            xmlNodePtr mNode;
        };

        Children children() const { return Children(node); }

    private:
        const char *attribute(const char *name) const;

        xmlNodePtr node;
    };

    inline std::string_view Node::textContent() const
    {
        if (node->children && node->children->type == XML_TEXT_NODE)
            return (const char *) node->children->content;
        return {};
    }

    inline const char *Node::attribute(const char *name) const
    {
        if (node->type != XML_ELEMENT_NODE)
            return nullptr;

        for (xmlAttrPtr prop = node->properties; prop; prop = prop->next) {
            if (xmlStrEqual(prop->name, BAD_CAST name)) {
                if (prop->children)
                    return reinterpret_cast<const char*>(prop->children->content);
                else
                    return nullptr;
            }
        }
        return nullptr;
    }

    template<typename T>
    inline bool Node::attribute(const char *name, T &value)
    {
        if (const char *str = attribute(name))
        {
            fromString(str, value);
            return true;
        }
        return false;
    }

    inline bool Node::hasAttribute(const char *name) const
    {
        return attribute(name) != nullptr;
    }

    inline int Node::getProperty(const char *name, int def) const
    {
        int ret = def;
        if (const char *str = attribute(name))
            fromString(str, ret);
        return ret;
    }

    inline double Node::getFloatProperty(const char *name, double def) const
    {
        double ret = def;
        if (const char *str = attribute(name))
            fromString(str, ret);
        return ret;
    }

    inline std::string Node::getProperty(const char *name, const std::string &def) const
    {
        if (const char *str = attribute(name))
            return str;

        return def;
    }

    inline bool Node::getBoolProperty(const char *name, bool def) const
    {
        bool ret = def;
        if (const char *str = attribute(name))
            ret = getBoolFromString(str, def);
        return ret;
    }

    inline Node Node::findFirstChildByName(const char *name) const
    {
        for (auto child : children())
            if (child.name() == name)
                return child;

        return nullptr;
    }

    /**
     * A helper class for parsing an XML document, which also cleans it up
     * again (RAII).
     */
    class Document
    {
    public:
        /**
         * Constructor that attempts to load the given file through the
         * resource manager. Logs errors.
         */
        Document(const std::string &filename, bool useResman = true);

        /**
         * Destructor. Frees the loaded XML file.
         */
        ~Document();

        /**
         * Returns the root node of the document (or NULL if there was a
         * load error).
         */
        Node rootNode();

    private:
        xmlDocPtr mDoc;
    };

    inline Node Document::rootNode()
    {
        return mDoc ? xmlDocGetRootElement(mDoc) : nullptr;
    }

    /**
     * Initializes the XML engine.
     */
    void init();

    /**
     * Helper class for writing out XML data.
     *
     * Based on libxml2's text writing API for XML.
     */
    class Writer
    {
    public:
        Writer(const std::string &fileName);
        ~Writer();

        bool isValid() const { return mWriter != nullptr; }

        void startElement(const char *name);
        void endElement();

        void addAttribute(const char *name, const std::string &value);
        void addAttribute(const char *name, const char *value);
        void addAttribute(const char *name, int value);
        void addAttribute(const char *name, unsigned value);
        void addAttribute(const char *name, float value);
        void addAttribute(const char *name, bool value);

        template<typename Enum, std::enable_if_t<std::is_enum_v<Enum>, bool> = true>
        void addAttribute(const char *name, Enum &value);

        void writeText(const std::string &text);

    private:
        xmlTextWriterPtr mWriter;
    };

    template<typename Enum, std::enable_if_t<std::is_enum_v<Enum>, bool>>
    inline void Writer::addAttribute(const char *name, Enum &value)
    {
        return addAttribute(name, static_cast<int>(value));
    }

    inline void Writer::startElement(const char *name)
    {
        xmlTextWriterStartElement(mWriter, BAD_CAST name);
    }

    inline void Writer::endElement()
    {
        xmlTextWriterEndElement(mWriter);
    }

    inline void Writer::addAttribute(const char *name, const std::string &value)
    {
        addAttribute(name, value.c_str());
    }

    inline void Writer::addAttribute(const char *name, const char *value)
    {
        xmlTextWriterWriteAttribute(mWriter, BAD_CAST name, BAD_CAST value);
    }

    inline void Writer::addAttribute(const char *name, int value)
    {
        xmlTextWriterWriteAttribute(mWriter, BAD_CAST name, BAD_CAST toString(value).c_str());
    }

    inline void Writer::addAttribute(const char *name, unsigned value)
    {
        xmlTextWriterWriteAttribute(mWriter, BAD_CAST name, BAD_CAST toString(value).c_str());
    }

    inline void Writer::addAttribute(const char *name, float value)
    {
        xmlTextWriterWriteAttribute(mWriter, BAD_CAST name, BAD_CAST toString(value).c_str());
    }

    inline void Writer::addAttribute(const char *name, bool value)
    {
        xmlTextWriterWriteAttribute(mWriter, BAD_CAST name, BAD_CAST (value ? "1" : "0"));
    }

    inline void Writer::writeText(const std::string &text)
    {
        xmlTextWriterWriteString(mWriter, BAD_CAST text.c_str());
    }
}