diff options
author | Peter Burgess <pete.burgess@isode.com> | 2018-03-26 11:17:24 (GMT) |
---|---|---|
committer | Peter Burgess <pete.burgess@isode.com> | 2018-03-27 12:58:17 (GMT) |
commit | 860ef54501e8c32d91160f798c8f9ecf811f2501 (patch) | |
tree | 60e3621f634b10a64fcf5cb1c5da8279c08e78a3 /Swiften/Parser/PayloadParsers/ReferencePayloadParser.cpp | |
parent | 92bba873587e0cfaf53aff6749d4537b13e275e8 (diff) | |
download | swift-860ef54501e8c32d91160f798c8f9ecf811f2501.zip swift-860ef54501e8c32d91160f798c8f9ecf811f2501.tar.bz2 |
Add new ReferencePayload element class, parser and serializer
Added a new element object ReferencePayload, and created the parser
and serializer to handle this element. Currently no functionality
to send references directly in swift, nor to render their contents.
Test-Information:
Unit tests written and passed for serializer and parser, testing
various types of valid and invalid references, and testing
references with embedded payloads.
Change-Id: I81fd5d9e020fac1729640f297705806af97f6388
Diffstat (limited to 'Swiften/Parser/PayloadParsers/ReferencePayloadParser.cpp')
-rw-r--r-- | Swiften/Parser/PayloadParsers/ReferencePayloadParser.cpp | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/Swiften/Parser/PayloadParsers/ReferencePayloadParser.cpp b/Swiften/Parser/PayloadParsers/ReferencePayloadParser.cpp new file mode 100644 index 0000000..a337a29 --- /dev/null +++ b/Swiften/Parser/PayloadParsers/ReferencePayloadParser.cpp @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#include <Swiften/Parser/PayloadParsers/ReferencePayloadParser.h> + +#include <cassert> +#include <iostream> + +#include <Swiften/Parser/PayloadParserFactory.h> +#include <Swiften/Parser/PayloadParserFactoryCollection.h> + +namespace Swift { + +ReferencePayloadParser::ReferencePayloadParser(PayloadParserFactoryCollection* factories) : factories_(factories) { +} + +ReferencePayload::Type ReferencePayloadParser::getTypeFromString(const std::string& typeString) const { + if (typeString == "data") { + return ReferencePayload::Type::Data; + } + else if (typeString == "mention") { + return ReferencePayload::Type::Mention; + } + else if (typeString == "pubsub") { + return ReferencePayload::Type::PubSub; + } + else { + return ReferencePayload::Type::Unknown; + } +} + +void ReferencePayloadParser::handleStartElement(const std::string& element, const std::string& ns, const AttributeMap& attributes) { + if (level_ == topLevel_) { + if (element == "reference") { + getPayloadInternal()->setType(getTypeFromString(attributes.getAttribute("type"))); + getPayloadInternal()->setUri(attributes.getAttributeValue("uri")); + getPayloadInternal()->setBegin(attributes.getAttributeValue("begin")); + getPayloadInternal()->setEnd(attributes.getAttributeValue("end")); + getPayloadInternal()->setAnchor(attributes.getAttributeValue("anchor")); + } + } + else if (level_ == payloadLevel_) { + PayloadParserFactory* payloadParserFactory = factories_->getPayloadParserFactory(element, ns, attributes); + if (payloadParserFactory) { + currentPayloadParser_.reset(payloadParserFactory->createPayloadParser()); + } + } + + if (level_ >= payloadLevel_ && currentPayloadParser_) { + currentPayloadParser_->handleStartElement(element, ns, attributes); + } + + ++level_; +} + +void ReferencePayloadParser::handleEndElement(const std::string& element, const std::string& ns) { + --level_; + if (currentPayloadParser_) { + if (level_ >= payloadLevel_) { + currentPayloadParser_->handleEndElement(element, ns); + } + + if (level_ == payloadLevel_) { + getPayloadInternal()->addPayload(currentPayloadParser_->getPayload()); + currentPayloadParser_.reset(); + } + } +} + +void ReferencePayloadParser::handleCharacterData(const std::string& data) { + if (level_ > payloadLevel_ && currentPayloadParser_) { + currentPayloadParser_->handleCharacterData(data); + } +} + +} |