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

#include <Swiften/Parser/SerializingParser.h>

#include <cassert>
#include <memory>

#include <Swiften/Base/foreach.h>
#include <Swiften/Serializer/XML/XMLTextNode.h>

namespace Swift {

SerializingParser::SerializingParser() {
}

void SerializingParser::handleStartElement(const std::string& tag, const std::string&  ns, const AttributeMap& attributes) {
    std::shared_ptr<XMLElement> element = std::make_shared<XMLElement>(tag, ns);
    // FIXME: Ignoring attribute namespace
    foreach (const AttributeMap::Entry& e, attributes.getEntries()) {
        element->setAttribute(e.getAttribute().getName(), e.getValue());
    }

    if (elementStack_.empty()) {
        rootElement_ = element;
    }
    else {
        (*(elementStack_.end() - 1))->addNode(element);
    }
    elementStack_.push_back(element);
}

void SerializingParser::handleEndElement(const std::string&, const std::string&) {
    assert(!elementStack_.empty());
    elementStack_.pop_back();
}

void SerializingParser::handleCharacterData(const std::string& data) {
    if (!elementStack_.empty()) {
        (*(elementStack_.end()-1))->addNode(std::make_shared<XMLTextNode>(data));
    }
}

std::string SerializingParser::getResult() const {
    return (rootElement_ ? rootElement_->serialize() : "");
}

}