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

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

#include <boost/lexical_cast.hpp>
#include <boost/optional.hpp>

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

using namespace Swift;

ResultSetParser::ResultSetParser() : level_(TopLevel) {
}

void ResultSetParser::handleStartElement(const std::string& element, const std::string& ns, const AttributeMap& attributes) {
    currentText_ = "";
    if (level_ == PayloadLevel) {
        if (element == "first" && ns == "http://jabber.org/protocol/rsm") {
            if (boost::optional<std::string> attributeValue = attributes.getAttributeValue("index")) {
                try {
                    getPayloadInternal()->setFirstIDIndex(boost::lexical_cast<int>(*attributeValue));
                } catch(boost::bad_lexical_cast&) {
                }
            }
        }
    }
    ++level_;
}

void ResultSetParser::handleEndElement(const std::string& element, const std::string&) {
    --level_;
    if (level_ == PayloadLevel) {
        if (element == "max") {
            try {
                getPayloadInternal()->setMaxItems(boost::lexical_cast<int>(currentText_));
            } catch(boost::bad_lexical_cast&) {
            }
        } else if (element == "count") {
            try {
                getPayloadInternal()->setCount(boost::lexical_cast<int>(currentText_));
            } catch(boost::bad_lexical_cast&) {
            }
        } else if (element == "index") {
            try {
                getPayloadInternal()->setIndex(boost::lexical_cast<int>(currentText_));
            } catch(boost::bad_lexical_cast&) {
            }
        } else if (element == "first") {
            getPayloadInternal()->setFirstID(currentText_);
        } else if (element == "last") {
            getPayloadInternal()->setLastID(currentText_);
        } else if (element == "before") {
            getPayloadInternal()->setBefore(currentText_);
        } else if (element == "after") {
            getPayloadInternal()->setAfter(currentText_);
        }
    }
}

void ResultSetParser::handleCharacterData(const std::string& data) {
    currentText_ += data;
}