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

#include <Swiften/Parser/PayloadParsers/ReferencePayloadParser.h>

#include <cassert>
#include <iostream>

#include <Swiften/Parser/PayloadParserFactory.h>
#include <Swiften/Parser/PayloadParserFactoryCollection.h>

namespace Swift {

ReferencePayloadParser::ReferencePayloadParser(PayloadParserFactoryCollection* factories) : factories_(factories) {
}

ReferencePayload::Type ReferencePayloadParser::getTypeFromString(const std::string& typeString) const {
    if (typeString == "data") {
        return ReferencePayload::Type::Data;
    }
    else if (typeString == "mention") {
        return ReferencePayload::Type::Mention;
    }
    else if (typeString == "pubsub") {
        return ReferencePayload::Type::PubSub;
    }
    else {
        return ReferencePayload::Type::Unknown;
    }
}

void ReferencePayloadParser::handleStartElement(const std::string& element, const std::string& ns, const AttributeMap& attributes) {
    if (level_ == topLevel_) {
        if (element == "reference") {
            getPayloadInternal()->setType(getTypeFromString(attributes.getAttribute("type")));
            getPayloadInternal()->setUri(attributes.getAttributeValue("uri"));
            getPayloadInternal()->setBegin(attributes.getAttributeValue("begin"));
            getPayloadInternal()->setEnd(attributes.getAttributeValue("end"));
            getPayloadInternal()->setAnchor(attributes.getAttributeValue("anchor"));
        }
    }
    else if (level_ == payloadLevel_)   {
        PayloadParserFactory* payloadParserFactory = factories_->getPayloadParserFactory(element, ns, attributes);
        if (payloadParserFactory) {
            currentPayloadParser_.reset(payloadParserFactory->createPayloadParser());
        }
    }

    if (level_ >= payloadLevel_ && currentPayloadParser_) {
        currentPayloadParser_->handleStartElement(element, ns, attributes);
    }

    ++level_;
}

void ReferencePayloadParser::handleEndElement(const std::string& element, const std::string& ns) {
    --level_;
    if (currentPayloadParser_) {
        if (level_ >= payloadLevel_) {
            currentPayloadParser_->handleEndElement(element, ns);
        }

        if (level_ == payloadLevel_) {
            getPayloadInternal()->addPayload(currentPayloadParser_->getPayload());
            currentPayloadParser_.reset();
        }
    }
}

void ReferencePayloadParser::handleCharacterData(const std::string& data) {
    if (level_ > payloadLevel_ && currentPayloadParser_) {
        currentPayloadParser_->handleCharacterData(data);
    }
}

}