diff options
author | Remko Tronçon <git@el-tramo.be> | 2010-09-17 10:16:22 (GMT) |
---|---|---|
committer | Remko Tronçon <git@el-tramo.be> | 2010-09-17 10:16:22 (GMT) |
commit | 49cf3d398309c933460e2be2de31c9b2217d9ab8 (patch) | |
tree | da17d48cbd2d9e8d3a204cae87f255373f008998 | |
parent | 83d55c4e8ffdc5e0efb41a2b4f0cd76cc1e5d8d1 (diff) | |
download | swift-contrib-49cf3d398309c933460e2be2de31c9b2217d9ab8.zip swift-contrib-49cf3d398309c933460e2be2de31c9b2217d9ab8.tar.bz2 |
Cleaning up XMPP parser a bit.
-rw-r--r-- | Swiften/Parser/XMPPParser.cpp | 28 | ||||
-rw-r--r-- | Swiften/Parser/XMPPParser.h | 15 |
2 files changed, 19 insertions, 24 deletions
diff --git a/Swiften/Parser/XMPPParser.cpp b/Swiften/Parser/XMPPParser.cpp index 034077e..795bee6 100644 --- a/Swiften/Parser/XMPPParser.cpp +++ b/Swiften/Parser/XMPPParser.cpp @@ -49,7 +49,7 @@ XMPPParser::XMPPParser( xmlParser_(0), client_(client), payloadParserFactories_(payloadParserFactories), - currentDepth_(0), + level_(0), currentElementParser_(0), parseErrorOccurred_(false) { xmlParser_ = PlatformXMLParserFactory().createXMLParser(this); @@ -66,7 +66,7 @@ bool XMPPParser::parse(const String& data) { } void XMPPParser::handleStartElement(const String& element, const String& ns, const AttributeMap& attributes) { - if (!inStream()) { + if (level_ == TopLevel) { if (element == "stream" && ns == "http://etherx.jabber.org/streams") { ProtocolHeader header; header.setFrom(attributes.getAttribute("from")); @@ -80,33 +80,31 @@ void XMPPParser::handleStartElement(const String& element, const String& ns, con } } else { - if (!inElement()) { + if (level_ == StreamLevel) { assert(!currentElementParser_); - delete currentElementParser_; currentElementParser_ = createElementParser(element, ns); } currentElementParser_->handleStartElement(element, ns, attributes); } - ++currentDepth_; + ++level_; } void XMPPParser::handleEndElement(const String& element, const String& ns) { - assert(inStream()); - if (inElement()) { + assert(level_ > TopLevel); + --level_; + if (level_ == TopLevel) { + assert(element == "stream"); + client_->handleStreamEnd(); + } + else { assert(currentElementParser_); currentElementParser_->handleEndElement(element, ns); - --currentDepth_; - if (!inElement()) { + if (level_ == StreamLevel) { client_->handleElement(currentElementParser_->getElement()); delete currentElementParser_; - currentElementParser_ = 0; + currentElementParser_ = NULL; } } - else { - assert(element == "stream"); - --currentDepth_; - client_->handleStreamEnd(); - } } void XMPPParser::handleCharacterData(const String& data) { diff --git a/Swiften/Parser/XMPPParser.h b/Swiften/Parser/XMPPParser.h index 45d005c..c1a9323 100644 --- a/Swiften/Parser/XMPPParser.h +++ b/Swiften/Parser/XMPPParser.h @@ -37,21 +37,18 @@ namespace Swift { virtual void handleEndElement(const String& element, const String& ns); virtual void handleCharacterData(const String& data); - bool inStream() const { - return currentDepth_ > 0; - } - - bool inElement() const { - return currentDepth_ > 1; - } - ElementParser* createElementParser(const String& element, const String& xmlns); private: XMLParser* xmlParser_; XMPPParserClient* client_; PayloadParserFactoryCollection* payloadParserFactories_; - int currentDepth_; + enum Level { + TopLevel = 0, + StreamLevel = 1, + ElementLevel = 2 + }; + int level_; ElementParser* currentElementParser_; bool parseErrorOccurred_; }; |