summaryrefslogtreecommitdiffstats
blob: 1e0e1bb953c2ac512c8e155e464eff2ee960f83c (plain)
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
/*
 * Copyright (c) 2013-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Sluift/ElementConvertors/DOMElementConvertor.h>

#include <iostream>
#include <memory>

#include <lua.hpp>

#include <Swiften/Base/foreach.h>
#include <Swiften/Elements/RawXMLPayload.h>
#include <Swiften/Parser/Attribute.h>
#include <Swiften/Parser/AttributeMap.h>
#include <Swiften/Parser/XMLParser.h>
#include <Swiften/Parser/XMLParserClient.h>
#include <Swiften/Serializer/PayloadSerializer.h>
#include <Swiften/Serializer/XML/XMLElement.h>
#include <Swiften/Serializer/XML/XMLRawTextNode.h>
#include <Swiften/Serializer/XML/XMLTextNode.h>

#include <Sluift/Lua/Check.h>
#include <Sluift/Lua/Debug.h>
#include <Sluift/Lua/LuaUtils.h>

using namespace Swift;

namespace {
    class ParserClient : public XMLParserClient {
        public:
            ParserClient(lua_State* L) : L(L), currentIndex(1) {
            }

            virtual void handleStartElement(
                    const std::string& element, const std::string& ns,
                    const AttributeMap& attributes) SWIFTEN_OVERRIDE {
                lua_checkstack(L, 6);
                lua_pushnumber(L, currentIndex);
                lua_newtable(L);
                lua_pushstring(L, element.c_str());
                lua_setfield(L, -2, "tag");
                if (!ns.empty()) {
                    lua_pushstring(L, ns.c_str());
                    lua_setfield(L, -2, "ns");
                }
                if (!attributes.getEntries().empty()) {
                    lua_newtable(L);
                    int i = 1;
                    foreach(const AttributeMap::Entry& entry, attributes.getEntries()) {
                        lua_pushnumber(L, i);
                        lua_newtable(L);
                        lua_pushstring(L, entry.getAttribute().getName().c_str());
                        lua_setfield(L, -2, "name");
                        if (!entry.getAttribute().getNamespace().empty()) {
                            lua_pushstring(L, entry.getAttribute().getNamespace().c_str());
                            lua_setfield(L, -2, "ns");
                        }
                        lua_pushstring(L, entry.getValue().c_str());
                        lua_setfield(L, -2, "value");
                        lua_settable(L, -3);
                        ++i;
                    }
                    lua_setfield(L, -2, "attributes");
                }

                indexStack.push_back(currentIndex);
                currentIndex = 1;
                lua_newtable(L);
            }

            virtual void handleEndElement(
                    const std::string&, const std::string&) SWIFTEN_OVERRIDE {
                lua_setfield(L, -2, "children");
                lua_settable(L, -3);
                currentIndex = indexStack.back();
                indexStack.pop_back();
                currentIndex++;
            }

            virtual void handleCharacterData(const std::string& data) SWIFTEN_OVERRIDE {
                lua_checkstack(L, 2);
                lua_pushnumber(L, currentIndex);
                lua_pushstring(L, data.c_str());
                lua_settable(L, -3);
                currentIndex++;
            }

        private:
            lua_State* L;
            std::vector<int> indexStack;
            int currentIndex;
    };

    std::string serializeElement(lua_State* L) {
        std::string tag;
        lua_getfield(L, -1, "tag");
        if (lua_isstring(L, -1)) {
            tag = lua_tostring(L, -1);
        }
        lua_pop(L, 1);

        std::string ns;
        lua_getfield(L, -1, "ns");
        if (lua_isstring(L, -1)) {
            ns = lua_tostring(L, -1);
        }
        lua_pop(L, 1);

        XMLElement element(tag, ns);

        lua_getfield(L, -1, "attributes");
        if (lua_istable(L, -1)) {
            int index = Lua::absoluteOffset(L, -1);
            for (lua_pushnil(L); lua_next(L, index) != 0; ) {
                if (lua_istable(L, -1)) {
                    std::string attributeName;
                    lua_getfield(L, -1, "name");
                    if (lua_isstring(L, -1)) {
                        attributeName = lua_tostring(L, -1);
                    }
                    lua_pop(L, 1);

                    std::string attributeValue;
                    lua_getfield(L, -1, "value");
                    if (lua_isstring(L, -1)) {
                        attributeValue = lua_tostring(L, -1);
                    }
                    lua_pop(L, 1);

                    if (!attributeName.empty()) {
                        element.setAttribute(attributeName, attributeValue);
                    }
                }
                lua_pop(L, 1); // value
            }
        }
        lua_pop(L, 1); // children

        lua_getfield(L, -1, "children");
        if (lua_istable(L, -1)) {
            int index = Lua::absoluteOffset(L, -1);
            for (lua_pushnil(L); lua_next(L, index) != 0; ) {
                if (lua_isstring(L, -1)) {
                    element.addNode(std::make_shared<XMLTextNode>(lua_tostring(L, -1)));
                }
                else if (lua_istable(L, -1)) {
                    element.addNode(std::make_shared<XMLRawTextNode>(serializeElement(L)));
                }
                lua_pop(L, 1); // value
            }
        }
        lua_pop(L, 1); // children

        return element.serialize();
    }
}

DOMElementConvertor::DOMElementConvertor() {
}

DOMElementConvertor::~DOMElementConvertor() {
}

std::shared_ptr<Element> DOMElementConvertor::convertFromLua(lua_State* L, int index, const std::string& type) {
    if (!lua_istable(L, index) || type != "dom") {
        return std::shared_ptr<Payload>();
    }
    return std::make_shared<RawXMLPayload>(serializeElement(L).c_str());
}

boost::optional<std::string> DOMElementConvertor::convertToLua(
        lua_State* L, std::shared_ptr<Element> element) {
    // Serialize payload to XML
    std::shared_ptr<Payload> payload = std::dynamic_pointer_cast<Payload>(element);
    if (!payload) {
        return boost::optional<std::string>();
    }

    PayloadSerializer* serializer = serializers.getPayloadSerializer(payload);
    assert(serializer);
    std::string serializedPayload = serializer->serialize(payload);

    lua_newtable(L);

    // Parse the payload again
    ParserClient parserClient(L);
    std::shared_ptr<XMLParser> parser(parsers.createXMLParser(&parserClient));
    bool result = parser->parse(serializedPayload);
    assert(result);

    // There can only be one element, so stripping the list
    lua_pushnil(L);
    lua_next(L, -2);
    Lua::registerTableToString(L, -1);

    lua_replace(L, -3);
    lua_settop(L, -2);

    return std::string("dom");
}