xmpp_parser.cpp 3.88 KB
Newer Older
1 2 3
#include <xmpp/xmpp_parser.hpp>
#include <xmpp/xmpp_stanza.hpp>

4 5
#include <logger/logger.hpp>

6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/**
 * Expat handlers. Called by the Expat library, never by ourself.
 * They just forward the call to the XmppParser corresponding methods.
 */

static void start_element_handler(void* user_data, const XML_Char* name, const XML_Char** atts)
{
  static_cast<XmppParser*>(user_data)->start_element(name, atts);
}

static void end_element_handler(void* user_data, const XML_Char* name)
{
  static_cast<XmppParser*>(user_data)->end_element(name);
}

static void character_data_handler(void *user_data, const XML_Char *s, int len)
{
  static_cast<XmppParser*>(user_data)->char_data(s, len);
}

/**
 * XmppParser class
 */
29 30 31 32 33

XmppParser::XmppParser():
  level(0),
  current_node(nullptr)
{
34 35 36 37 38 39 40
  // Create the expat parser
  this->parser = XML_ParserCreateNS("UTF-8", ':');
  XML_SetUserData(this->parser, static_cast<void*>(this));

  // Install Expat handlers
  XML_SetElementHandler(this->parser, &start_element_handler, &end_element_handler);
  XML_SetCharacterDataHandler(this->parser, &character_data_handler);
41 42 43 44 45 46
}

XmppParser::~XmppParser()
{
  if (this->current_node)
    delete this->current_node;
47
  XML_ParserFree(this->parser);
48 49
}

50 51 52 53
int XmppParser::feed(const char* data, const int len, const bool is_final)
{
  int res = XML_Parse(this->parser, data, len, is_final);
  if (res == 0)
54 55
    log_error("Xml_Parse encountered an error: " <<
              XML_ErrorString(XML_GetErrorCode(this->parser)))
56 57 58 59 60 61 62
  return res;
}

int XmppParser::parse(const int len, const bool is_final)
{
  int res = XML_ParseBuffer(this->parser, len, is_final);
  if (res == 0)
63 64
    log_error("Xml_Parsebuffer encountered an error: " <<
              XML_ErrorString(XML_GetErrorCode(this->parser)));
65 66 67 68
  return res;
}

void* XmppParser::get_buffer(const size_t size) const
69
{
70
  return XML_GetBuffer(this->parser, static_cast<int>(size));
71 72 73
}

void XmppParser::start_element(const XML_Char* name, const XML_Char** attribute)
74 75 76 77 78 79 80 81 82 83 84 85 86
{
  level++;

  XmlNode* new_node = new XmlNode(name, this->current_node);
  if (this->current_node)
    this->current_node->add_child(new_node);
  this->current_node = new_node;
  for (size_t i = 0; attribute[i]; i += 2)
    this->current_node->set_attribute(attribute[i], attribute[i+1]);
  if (this->level == 1)
    this->stream_open_event(*this->current_node);
}

87
void XmppParser::end_element(const XML_Char* name)
88
{
89
  (void)name;
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
  level--;
  this->current_node->close();
  if (level == 1)
    {
      this->stanza_event(*this->current_node);
    }
  if (level == 0)
    {
      this->stream_close_event(*this->current_node);
      delete this->current_node;
      this->current_node = nullptr;
    }
  else
    this->current_node = this->current_node->get_parent();
  if (level == 1)
    this->current_node->delete_all_children();
}

108
void XmppParser::char_data(const XML_Char* data, int len)
109 110
{
  if (this->current_node->has_children())
111
    this->current_node->get_last_child()->add_to_tail(std::string(data, len));
112
  else
113
    this->current_node->add_to_inner(std::string(data, len));
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
}

void XmppParser::stanza_event(const Stanza& stanza) const
{
  for (const auto& callback: this->stanza_callbacks)
    callback(stanza);
}

void XmppParser::stream_open_event(const XmlNode& node) const
{
  for (const auto& callback: this->stream_open_callbacks)
    callback(node);
}

void XmppParser::stream_close_event(const XmlNode& node) const
{
  for (const auto& callback: this->stream_close_callbacks)
    callback(node);
}

void XmppParser::add_stanza_callback(std::function<void(const Stanza&)>&& callback)
{
  this->stanza_callbacks.emplace_back(std::move(callback));
}

void XmppParser::add_stream_open_callback(std::function<void(const XmlNode&)>&& callback)
{
  this->stream_open_callbacks.emplace_back(std::move(callback));
}

void XmppParser::add_stream_close_callback(std::function<void(const XmlNode&)>&& callback)
{
  this->stream_close_callbacks.emplace_back(std::move(callback));
}