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

#include <Swiften/Parser/LibXMLParser.h>

#include <cassert>
#include <cstring>
#include <limits>
#include <memory>
#include <string>

#include <libxml/parser.h>

#include <Swiften/Base/Log.h>
#include <Swiften/Parser/XMLParserClient.h>

namespace Swift {

struct LibXMLParser::Private {
    xmlSAXHandler handler_;
    xmlParserCtxtPtr context_;
};

static void handleStartElement(void* parser, const xmlChar* name, const xmlChar*, const xmlChar* xmlns, int, const xmlChar**, int nbAttributes, int nbDefaulted, const xmlChar ** attributes) {
    AttributeMap attributeValues;
    if (nbDefaulted != 0) {
        // Just because i don't understand what this means yet :-)
        SWIFT_LOG(error) << "Unexpected nbDefaulted on XML element" << std::endl;
    }
    for (int i = 0; i < nbAttributes*5; i += 5) {
        std::string attributeNS = "";
        if (attributes[i+2]) {
            attributeNS = std::string(reinterpret_cast<const char*>(attributes[i+2]));
        }
        assert(attributes[i+4] >= attributes[i+3]);
        attributeValues.addAttribute(
                std::string(reinterpret_cast<const char*>(attributes[i])),
                attributeNS,
                std::string(reinterpret_cast<const char*>(attributes[i+3]),
                    static_cast<size_t>(attributes[i+4]-attributes[i+3])));
    }
    static_cast<XMLParser*>(parser)->getClient()->handleStartElement(reinterpret_cast<const char*>(name), (xmlns ? reinterpret_cast<const char*>(xmlns) : std::string()), attributeValues);
}

static void handleEndElement(void *parser, const xmlChar* name, const xmlChar*, const xmlChar* xmlns) {
    static_cast<XMLParser*>(parser)->getClient()->handleEndElement(reinterpret_cast<const char*>(name), (xmlns ? reinterpret_cast<const char*>(xmlns) : std::string()));
}

static void handleCharacterData(void* parser, const xmlChar* data, int len) {
    assert(len >= 0);
    static_cast<XMLParser*>(parser)->getClient()->handleCharacterData(std::string(reinterpret_cast<const char*>(data), static_cast<size_t>(len)));
}

static void handleError(void*, const char* /*m*/, ... ) {
    /*
    va_list args;
    va_start(args, m);
    vfprintf(stdout, m, args);
    va_end(args);
    */
}

static void handleWarning(void*, const char*, ... ) {
}

bool LibXMLParser::initialized = false;

LibXMLParser::LibXMLParser(XMLParserClient* client) : XMLParser(client), p(new Private()) {
    // Initialize libXML for multithreaded applications
    if (!initialized) {
        xmlInitParser();
        initialized = true;
    }

    memset(&p->handler_, 0, sizeof(p->handler_) );
    p->handler_.initialized = XML_SAX2_MAGIC;
    p->handler_.startElementNs = &handleStartElement;
    p->handler_.endElementNs = &handleEndElement;
    p->handler_.characters = &handleCharacterData;
    p->handler_.warning = &handleWarning;
    p->handler_.error = &handleError;

    p->context_ = xmlCreatePushParserCtxt(&p->handler_, this, nullptr, 0, nullptr);
    xmlCtxtUseOptions(p->context_, XML_PARSE_NOENT);
    assert(p->context_);
}

LibXMLParser::~LibXMLParser() {
    if (p->context_) {
        xmlFreeParserCtxt(p->context_);
    }
}

bool LibXMLParser::parse(const std::string& data) {
    if (data.size() > std::numeric_limits<int>::max()) {
        return false;
    }
    if (xmlParseChunk(p->context_, data.c_str(), static_cast<int>(data.size()), false) == XML_ERR_OK) {
        return true;
    }
    xmlError* error = xmlCtxtGetLastError(p->context_);
    if (error->code == XML_WAR_NS_URI || error->code == XML_WAR_NS_URI_RELATIVE) {
        xmlCtxtResetLastError(p->context_);
        p->context_->errNo = XML_ERR_OK;
        return true;
    }
    return false;
}

}