diff options
Diffstat (limited to 'Swiften/Client/XMLBeautifier.cpp')
-rw-r--r-- | Swiften/Client/XMLBeautifier.cpp | 94 |
1 files changed, 56 insertions, 38 deletions
diff --git a/Swiften/Client/XMLBeautifier.cpp b/Swiften/Client/XMLBeautifier.cpp index e2cd58e..97a228f 100644 --- a/Swiften/Client/XMLBeautifier.cpp +++ b/Swiften/Client/XMLBeautifier.cpp @@ -14,35 +14,53 @@ #include <sstream> #include <stack> +#include <iostream> #include <Swiften/Base/Log.h> #include <Swiften/Parser/PlatformXMLParserFactory.h> namespace Swift { -XMLBeautifier::XMLBeautifier(bool indention, bool coloring) : doIndention(indention), doColoring(coloring), intLevel(0), parser(nullptr), lastWasStepDown(false) { - factory = new PlatformXMLParserFactory(); +XMLBeautifier::XMLBeautifier(bool indention, bool coloring) : doIndention_(indention), doColoring_(coloring), factory_(new PlatformXMLParserFactory()) { } -XMLBeautifier::~XMLBeautifier() { - delete factory; -} std::string XMLBeautifier::beautify(const std::string &text) { - parser = factory->createXMLParser(this); - intLevel = 0; - buffer.str(std::string()); - parser->parse(text); - delete parser; - return buffer.str(); + wasReset_ = false; + if (!parser_) { + reset(); + } + buffer_.str(std::string()); + if (!parser_->parse(text)) { + reset(); + parser_->parse(text); + } + return buffer_.str(); +} + +bool XMLBeautifier::wasReset() const { + return wasReset_; +} + +int XMLBeautifier::getLevel() const { + return intLevel_; } void XMLBeautifier::indent() { - for (int i = 0; i < intLevel; ++i) { - buffer << " "; + for (int i = 0; i < intLevel_; ++i) { + buffer_ << " "; } } +void XMLBeautifier::reset() { + parser_ = factory_->createXMLParser(this); + intLevel_ = 0; + lastWasStepDown_ = false; + std::stack<std::string>().swap(parentNSs_); + buffer_.str(std::string()); + wasReset_ = true; +} + // all bold but reset // static const char colorBlue[] = "\x1b[01;34m"; static const char colorCyan[] = "\x1b[01;36m"; @@ -86,47 +104,47 @@ std::string XMLBeautifier::styleValue(const std::string& text) const { } void XMLBeautifier::handleStartElement(const std::string& element, const std::string& ns, const AttributeMap& attributes) { - if (doIndention) { - if (intLevel) buffer << std::endl; + if (doIndention_) { + if (intLevel_) buffer_ << std::endl; } indent(); - buffer << "<" << (doColoring ? styleTag(element) : element); - if (!ns.empty() && (!parentNSs.empty() && parentNSs.top() != ns)) { - buffer << " "; - buffer << (doColoring ? styleAttribute("xmlns") : "xmlns"); - buffer << "="; - buffer << "\"" << (doColoring ? styleNamespace(ns) : ns) << "\""; + buffer_ << "<" << (doColoring_ ? styleTag(element) : element); + if (!ns.empty() && (!parentNSs_.empty() && parentNSs_.top() != ns)) { + buffer_ << " "; + buffer_ << (doColoring_ ? styleAttribute("xmlns") : "xmlns"); + buffer_ << "="; + buffer_ << "\"" << (doColoring_ ? styleNamespace(ns) : ns) << "\""; } if (!attributes.getEntries().empty()) { for (const auto& entry : attributes.getEntries()) { - buffer << " "; - buffer << (doColoring ? styleAttribute(entry.getAttribute().getName()) : entry.getAttribute().getName()); - buffer << "="; - buffer << "\"" << (doColoring ? styleValue(entry.getValue()) : entry.getValue()) << "\""; + buffer_ << " "; + buffer_ << (doColoring_ ? styleAttribute(entry.getAttribute().getName()) : entry.getAttribute().getName()); + buffer_ << "="; + buffer_ << "\"" << (doColoring_ ? styleValue(entry.getValue()) : entry.getValue()) << "\""; } } - buffer << ">"; - ++intLevel; - lastWasStepDown = false; - parentNSs.push(ns); + buffer_ << ">"; + ++intLevel_; + lastWasStepDown_ = false; + parentNSs_.push(ns); } void XMLBeautifier::handleEndElement(const std::string& element, const std::string& /* ns */) { - --intLevel; - parentNSs.pop(); - if (/*hadCDATA.top() ||*/ lastWasStepDown) { - if (doIndention) { - buffer << std::endl; + --intLevel_; + parentNSs_.pop(); + if (/*hadCDATA.top() ||*/ lastWasStepDown_) { + if (doIndention_) { + buffer_ << std::endl; } indent(); } - buffer << "</" << (doColoring ? styleTag(element) : element) << ">"; - lastWasStepDown = true; + buffer_ << "</" << (doColoring_ ? styleTag(element) : element) << ">"; + lastWasStepDown_ = true; } void XMLBeautifier::handleCharacterData(const std::string& data) { - buffer << data; - lastWasStepDown = false; + buffer_ << data; + lastWasStepDown_ = false; } } |