Use the Expat library directly instead of relying on expatpp

And now we handle namespaces, yay.
And a nice little test.
This commit is contained in:
Florent Le Coz
2013-11-10 02:30:22 +01:00
parent 8acd7a02ae
commit 3d92360310
7 changed files with 140 additions and 58 deletions
+23 -23
View File
@@ -1,35 +1,43 @@
#ifndef XMPP_PARSER_INCLUDED
# define XMPP_PARSER_INCLUDED
#include <functional>
#include <stack>
#include <xmpp/xmpp_stanza.hpp>
#include <expatpp.h>
#include <functional>
#include <expat.h>
/**
* A SAX XML parser that builds XML nodes and spawns events when a complete
* stanza is received (an element of level 2), or when the document is
* opened (an element of level 1)
* opened/closed (an element of level 1)
*
* After a stanza_event has been spawned, we delete the whole stanza. This
* means that even with a very long document (in XMPP the document is
* potentially infinite), the memory then is never exhausted as long as each
* potentially infinite), the memory is never exhausted as long as each
* stanza is reasonnably short.
*
* The element names generated by expat contain the namespace of the
* element, a colon (':') and then the actual name of the element. To get
* an element "x" with a namespace of "http://jabber.org/protocol/muc", you
* just look for an XmlNode named "http://jabber.org/protocol/muc:x"
*
* TODO: enforce the size-limit for the stanza (limit the number of childs
* it can contain). For example forbid the parser going further than level
* 20 (arbitrary number here), and each XML node to have more than 15 childs
* (arbitrary number again).
*/
class XmppParser: public expatpp
class XmppParser
{
public:
explicit XmppParser();
~XmppParser();
public:
/**
* Feed the parser with some XML data
*/
void feed(const char* data, const int len, const bool is_final);
/**
* Add one callback for the various events that this parser can spawn.
*/
@@ -37,7 +45,6 @@ public:
void add_stream_open_callback(std::function<void(const XmlNode&)>&& callback);
void add_stream_close_callback(std::function<void(const XmlNode&)>&& callback);
private:
/**
* Called when a new XML element has been opened. We instanciate a new
* XmlNode and set it as our current node. The parent of this new node is
@@ -46,7 +53,7 @@ private:
*
* We spawn a stream_event with this node if this is a level-1 element.
*/
void startElement(const XML_Char* name, const XML_Char** attribute);
void start_element(const XML_Char* name, const XML_Char** attribute);
/**
* Called when an XML element has been closed. We close the current_node,
* set our current_node as the parent of the current_node, and if that was
@@ -55,19 +62,11 @@ private:
* And we then delete the stanza (and everything under it, its children,
* attribute, etc).
*/
void endElement(const XML_Char* name);
void end_element(const XML_Char* name);
/**
* Some inner or tail data has been parsed
*/
void charData(const XML_Char* data, int len);
/**
* TODO use that.
*/
void startNamespace(const XML_Char* prefix, const XML_Char* uri);
/**
* TODO and that.
*/
void endNamespace(const XML_Char* prefix);
void char_data(const XML_Char* data, int len);
/**
* Calls all the stanza_callbacks one by one.
*/
@@ -82,6 +81,11 @@ private:
*/
void stream_close_event(const XmlNode& node) const;
private:
/**
* Expat structure.
*/
XML_Parser parser;
/**
* The current depth in the XML document
*/
@@ -98,10 +102,6 @@ private:
std::vector<std::function<void(const Stanza&)>> stanza_callbacks;
std::vector<std::function<void(const XmlNode&)>> stream_open_callbacks;
std::vector<std::function<void(const XmlNode&)>> stream_close_callbacks;
/**
* TODO: also use that.
*/
std::stack<std::pair<std::string, std::string>> namespaces;
};
#endif // XMPP_PARSER_INCLUDED