diff options
Diffstat (limited to 'Swiften/Parser/Tree')
-rw-r--r-- | Swiften/Parser/Tree/NullParserElement.cpp | 13 | ||||
-rw-r--r-- | Swiften/Parser/Tree/NullParserElement.h | 21 | ||||
-rw-r--r-- | Swiften/Parser/Tree/ParserElement.cpp | 53 | ||||
-rw-r--r-- | Swiften/Parser/Tree/ParserElement.h | 48 | ||||
-rw-r--r-- | Swiften/Parser/Tree/TreeReparser.cpp | 48 | ||||
-rw-r--r-- | Swiften/Parser/Tree/TreeReparser.h | 19 |
6 files changed, 202 insertions, 0 deletions
diff --git a/Swiften/Parser/Tree/NullParserElement.cpp b/Swiften/Parser/Tree/NullParserElement.cpp new file mode 100644 index 0000000..7dda9c3 --- /dev/null +++ b/Swiften/Parser/Tree/NullParserElement.cpp @@ -0,0 +1,13 @@ +/* + * Copyright (c) 2011 Kevin Smith + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#include <Swiften/Parser/Tree/NullParserElement.h> + +namespace Swift { + +boost::shared_ptr<NullParserElement> NullParserElement::element = boost::make_shared<NullParserElement>(); + +} diff --git a/Swiften/Parser/Tree/NullParserElement.h b/Swiften/Parser/Tree/NullParserElement.h new file mode 100644 index 0000000..8dd9bc1 --- /dev/null +++ b/Swiften/Parser/Tree/NullParserElement.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2011 Kevin Smith + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#pragma once + +#include <string> +#include <Swiften/Parser/Tree/ParserElement.h> + +namespace Swift { + class NullParserElement : public ParserElement { + public: + NullParserElement() : ParserElement("", "", AttributeMap()) {} + + virtual operator bool() { return false; } + + static boost::shared_ptr<NullParserElement> element; + }; +} diff --git a/Swiften/Parser/Tree/ParserElement.cpp b/Swiften/Parser/Tree/ParserElement.cpp new file mode 100644 index 0000000..9d9b9b6 --- /dev/null +++ b/Swiften/Parser/Tree/ParserElement.cpp @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2011 Kevin Smith + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + + +#include <Swiften/Parser/Tree/ParserElement.h> +#include <Swiften/Parser/Tree/NullParserElement.h> + +#include <iostream> + +namespace Swift{ + +ParserElement::ParserElement(const std::string& name, const std::string& xmlns, const AttributeMap& attributes) : name_(name), xmlns_(xmlns), attributes_(attributes) { +} + +ParserElement::~ParserElement() { +} + +ParserElement::ref ParserElement::addChild(const std::string& name, const std::string& xmlns, const AttributeMap& attributes) { + ParserElement::ref child = boost::make_shared<ParserElement>(name, xmlns, attributes); + children_.push_back(child); + return child; +} + +void ParserElement::appendCharacterData(const std::string& data) { + text_ += data; +} + +struct DoesntMatch { + public: + DoesntMatch(const std::string& tagName, const std::string& ns) : tagName(tagName), ns(ns) {} + bool operator()(ParserElement::ref element) { return element->getName() != tagName || element->getNamespace() != ns; } + private: + std::string tagName; + std::string ns; +}; + + +std::vector<ParserElement::ref> ParserElement::getChildren(const std::string& name, const std::string& xmlns) const { + std::vector<ParserElement::ref> result; + std::remove_copy_if(children_.begin(), children_.end(), std::back_inserter(result), DoesntMatch(name, xmlns)); + return result; +} + +ParserElement::ref ParserElement::getChild(const std::string& name, const std::string& xmlns) const { + std::vector<ParserElement::ref> results = getChildren(name, xmlns); + ParserElement::ref result = results.empty() ? NullParserElement::element : results[0]; + return result; +} + +} diff --git a/Swiften/Parser/Tree/ParserElement.h b/Swiften/Parser/Tree/ParserElement.h new file mode 100644 index 0000000..b268c76 --- /dev/null +++ b/Swiften/Parser/Tree/ParserElement.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2011 Kevin Smith + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + + +#pragma once + +#include <string> +#include <vector> +#include <Swiften/Base/boost_bsignals.h> +#include <Swiften/Parser/AttributeMap.h> +#include <boost/shared_ptr.hpp> +#include <boost/smart_ptr/make_shared.hpp> + +namespace Swift { + class ParserElement { + public: + typedef boost::shared_ptr<ParserElement> ref; + + ParserElement(const std::string& name, const std::string& xmlns, const AttributeMap& attributes); + virtual ~ParserElement(); + + const std::string& getText() const { return text_; } + const std::string& getName() const { return name_; } + const std::string& getNamespace() const { return xmlns_; } + const AttributeMap& getAttributes() const { return attributes_; } + + ParserElement::ref addChild(const std::string& name, const std::string& xmlns, const AttributeMap& attributes); + void appendCharacterData(const std::string& data); + + std::vector<ParserElement::ref> getChildren(const std::string& name, const std::string& xmlns) const; + const std::vector<ParserElement::ref>& getAllChildren() const {return children_;} + ParserElement::ref getChild(const std::string& name, const std::string& xmlns) const; + + virtual operator bool() { + return true; + } + + private: + std::vector<ParserElement::ref> children_; + std::string name_; + std::string xmlns_; + AttributeMap attributes_; + std::string text_; + }; +} diff --git a/Swiften/Parser/Tree/TreeReparser.cpp b/Swiften/Parser/Tree/TreeReparser.cpp new file mode 100644 index 0000000..9d09831 --- /dev/null +++ b/Swiften/Parser/Tree/TreeReparser.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2011 Kevin Smith + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#include <Swiften/Parser/Tree/TreeReparser.h> + +#include <boost/lexical_cast.hpp> +#include <utility> +#include <deque> + +#include <Swiften/Parser/PayloadParserFactoryCollection.h> +#include <Swiften/Parser/PayloadParserFactory.h> +#include <Swiften/Parser/PayloadParser.h> +#include <Swiften/Base/foreach.h> +#include <Swiften/Elements/MUCOccupant.h> + +namespace Swift { + +typedef std::pair<ParserElement::ref, bool> ElementState; + +boost::shared_ptr<Payload> TreeReparser::parseTree(ParserElement::ref root, PayloadParserFactoryCollection* collection) { + PayloadParser* parser = collection->getPayloadParserFactory(root->getName(), root->getNamespace(), root->getAttributes())->createPayloadParser(); + std::deque<ElementState > stack; + stack.push_back(ElementState(root, true)); + while (!stack.empty()) { + ElementState current = stack.back(); + stack.pop_back(); + if (current.second) { + stack.push_back(ElementState(current.first, false)); + parser->handleStartElement(current.first->getName(), current.first->getNamespace(), current.first->getAttributes()); + foreach(ParserElement::ref child, current.first->getAllChildren()) { + stack.push_back(ElementState(child, true)); + } + } else { + parser->handleCharacterData(current.first->getText()); + parser->handleEndElement(current.first->getName(), current.first->getNamespace()); + } + + } + + boost::shared_ptr<Payload> payload = parser->getPayload(); + delete parser; + return payload; +} + +} diff --git a/Swiften/Parser/Tree/TreeReparser.h b/Swiften/Parser/Tree/TreeReparser.h new file mode 100644 index 0000000..212c518 --- /dev/null +++ b/Swiften/Parser/Tree/TreeReparser.h @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2011 Kevin Smith + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#pragma once + +#include <Swiften/Parser/GenericPayloadTreeParser.h> +#include <Swiften/Parser/PayloadParsers/MUCItemParser.h> + +namespace Swift { + class PayloadParserFactoryCollection; + class TreeReparser { + public: + static boost::shared_ptr<Payload> parseTree(ParserElement::ref root, PayloadParserFactoryCollection* collection); + + }; +} |