File: xmlutils.cpp

package info (click to toggle)
wxformbuilder 4.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,908 kB
  • sloc: cpp: 37,318; xml: 6,611; javascript: 1,353; python: 94; sh: 62; makefile: 62
file content (215 lines) | stat: -rw-r--r-- 5,964 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
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
#include "xmlutils.h"

#include <cassert>
#include <utility>

#include <wx/ffile.h>
#include <wx/intl.h>
#include <wx/log.h>


namespace
{

class CompactPrinter : public tinyxml2::XMLPrinter
{
public:
    CompactPrinter(std::FILE* file = nullptr, bool compact = false, int depth = 0) :
        tinyxml2::XMLPrinter(file, compact, depth)
    {
    }

protected:
    void PrintSpace(int depth) override
    {
        for (int i = 0; i < depth; ++i) {
            Write("  ");
        }
    }
};


const std::pair<wxUniChar, wxString> entities[] = {
  {'&', "&amp;"}, {'<', "&lt;"}, {'>', "&gt;"}, {'\'', "&apos;"}, {'"', "&quot;"}, {'\n', "&#x0A;"},
};


wxString encodeEntities(const wxString& input)
{
    if (input.find_first_of("&<>'\"\n") == wxString::npos) {
        return input;
    }

    wxString output;
    output.reserve(input.size() * 1.2);
    auto isEntity = false;
    for (const auto& c : input) {
        for (const auto& [ch, entity] : entities) {
            if (c == ch) {
                output.append(entity);
                isEntity = true;
                break;
            }
        }
        if (!isEntity) {
            output.Append(c);
        } else {
            isEntity = false;
        }
    }

    return output;
}

wxString decodeEntities(const wxString& input)
{
    if (input.find_first_of("&") == wxString::npos) {
        return input;
    }

    wxString output;
    output.reserve(input.size());
    wxString sequence;
    sequence.reserve(8);
    enum class State {
        SEARCH,
        BUILD,
    };
    State state = State::SEARCH;
    for (const auto& c : input) {
        switch (state) {
            case State::SEARCH:
                if (c.GetValue() == '&') {
                    sequence = c;
                    state = State::BUILD;
                } else {
                    output.Append(c);
                }
                break;
            case State::BUILD:
                sequence.Append(c);
                if (c.GetValue() == ';') {
                    auto isEntity = false;
                    for (const auto& [ch, entity] : entities) {
                        if (sequence == entity) {
                            output.Append(ch);
                            isEntity = true;
                            break;
                        }
                    }
                    if (!isEntity) {
                        auto isNumeric = false;
                        if (sequence[1] == '#') {
                            unsigned long value = 0;
                            if (sequence[2] == 'x') {
                                if ((isNumeric = sequence.substr(3, sequence.length() - 3 - 1).ToCULong(&value, 16))) {
                                    output.Append(wxUniChar(value));
                                }
                            } else {
                                if ((isNumeric = sequence.substr(2, sequence.length() - 2 - 1).ToCULong(&value, 10))) {
                                    output.Append(wxUniChar(value));
                                }
                            }
                        }
                        if (!isNumeric) {
                            output.append(sequence);
                        }
                    }
                    sequence.clear();
                    state = State::SEARCH;
                }
                break;
        }
    }
    if (!sequence.empty()) {
        output.append(sequence);
    }

    return output;
}

}  // namespace


namespace XMLUtils
{

std::unique_ptr<tinyxml2::XMLDocument> LoadXMLFile(const wxString& path, bool collapseWhitespace)
{
    wxLogDebug("Loading XML file: %s", path);
    // Use wxFFile to get full unicode support for path on Windows
    wxFFile file;
    if (wxLogNull noLog; !file.Open(path, "rb")) {
        return std::unique_ptr<tinyxml2::XMLDocument>();
    }
    auto doc = std::make_unique<tinyxml2::XMLDocument>(
      false, collapseWhitespace ? tinyxml2::COLLAPSE_WHITESPACE : tinyxml2::PRESERVE_WHITESPACE);
    doc->LoadFile(file.fp());

    return doc;
}

bool SaveXMLFile(const wxString& path, const tinyxml2::XMLDocument& document, bool compact)
{
    assert(!document.ProcessEntities());

    // Use wxFFile to get full unicode support for path on Windows
    wxFFile file;
    if (wxLogNull noLog; !file.Open(path, "w")) {
        return false;
    }
    CompactPrinter printer(file.fp(), compact);
    document.Print(&printer);

    return true;
}

wxString SaveXMLString(const tinyxml2::XMLDocument& document, bool compact)
{
    assert(!document.ProcessEntities());

    CompactPrinter printer(nullptr, compact);
    document.Print(&printer);

    return wxString(printer.CStr(), wxConvUTF8);
}

wxString StringAttribute(const tinyxml2::XMLElement* element, const wxString& name, const wxString& defaultValue)
{
    const auto* value = element->Attribute(name.utf8_str());

    return (value ? decodeEntities(wxString(value, wxConvUTF8)) : defaultValue);
}

void SetAttribute(tinyxml2::XMLElement* element, const wxString& name, const wxString& value)
{
    element->SetAttribute(name.utf8_str(), encodeEntities(value).utf8_str());
}

wxString GetText(const tinyxml2::XMLElement* element, const wxString& defaultValue, bool deepSearch)
{
    const char* value = nullptr;
    if (!deepSearch) {
        value = element->GetText();
    } else {
        for (const auto* node = element->FirstChild(); node; node = node->NextSibling()) {
            if (node->ToText()) {
                value = node->Value();
                break;
            }
        }
    }

    return (value ? decodeEntities(wxString(value, wxConvUTF8)) : defaultValue);
}

void SetText(tinyxml2::XMLElement* element, const wxString& value, bool insertElement)
{
    if (!insertElement) {
        element->SetText(encodeEntities(value).utf8_str());
    } else {
        element->InsertNewText(encodeEntities(value).utf8_str());
    }
}

}  // namespace XMLUtils