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

#include <Swiften/Parser/Tree/TreeReparser.h>

#include <deque>
#include <utility>

#include <boost/lexical_cast.hpp>

#include <Swiften/Elements/MUCOccupant.h>
#include <Swiften/Parser/PayloadParser.h>
#include <Swiften/Parser/PayloadParserFactory.h>
#include <Swiften/Parser/PayloadParserFactoryCollection.h>

namespace Swift {

typedef std::pair<ParserElement::ref, bool> ElementState;

std::shared_ptr<Payload> TreeReparser::parseTree(ParserElement::ref root, PayloadParserFactoryCollection* collection) {
    PayloadParser* parser = collection->getPayloadParserFactory(root->getName(), root->getNamespace(), root->getAttributes())->createPayloadParser();
    std::deque<ElementState > stack;
    stack.push_back(ElementState(root, true));
    while (!stack.empty()) {
        ElementState current = stack.back();
        stack.pop_back();
        if (current.second) {
            stack.push_back(ElementState(current.first, false));
            parser->handleStartElement(current.first->getName(), current.first->getNamespace(), current.first->getAttributes());
            for (const auto& child : current.first->getAllChildren()) {
                stack.push_back(ElementState(child, true));
            }
        } else {
            parser->handleCharacterData(current.first->getText());
            parser->handleEndElement(current.first->getName(), current.first->getNamespace());
        }

    }

    std::shared_ptr<Payload> payload = parser->getPayload();
    delete parser;
    return payload;
}

}