Refactored XMLWriter to provide finer-grained control over formatting

This commit is contained in:
Martin Hořeňovský
2019-06-22 14:39:34 +02:00
parent 74e0e737a6
commit 3136c4fb6a
5 changed files with 104 additions and 43 deletions

View File

@@ -14,6 +14,14 @@
#include <vector>
namespace Catch {
enum class XmlFormatting {
None = 0x00,
Indent = 0x01,
Newline = 0x02,
};
XmlFormatting operator | (XmlFormatting lhs, XmlFormatting rhs);
XmlFormatting operator & (XmlFormatting lhs, XmlFormatting rhs);
class XmlEncode {
public:
@@ -35,14 +43,14 @@ namespace Catch {
class ScopedElement {
public:
ScopedElement( XmlWriter* writer );
ScopedElement( XmlWriter* writer, XmlFormatting fmt );
ScopedElement( ScopedElement&& other ) noexcept;
ScopedElement& operator=( ScopedElement&& other ) noexcept;
~ScopedElement();
ScopedElement& writeText( std::string const& text, bool indent = true );
ScopedElement& writeText( std::string const& text, XmlFormatting fmt = XmlFormatting::Newline | XmlFormatting::Indent );
template<typename T>
ScopedElement& writeAttribute( std::string const& name, T const& attribute ) {
@@ -52,6 +60,7 @@ namespace Catch {
private:
mutable XmlWriter* m_writer = nullptr;
XmlFormatting m_fmt;
};
XmlWriter( std::ostream& os = Catch::cout() );
@@ -60,11 +69,11 @@ namespace Catch {
XmlWriter( XmlWriter const& ) = delete;
XmlWriter& operator=( XmlWriter const& ) = delete;
XmlWriter& startElement( std::string const& name );
XmlWriter& startElement( std::string const& name, XmlFormatting fmt = XmlFormatting::Newline | XmlFormatting::Indent);
ScopedElement scopedElement( std::string const& name );
ScopedElement scopedElement( std::string const& name, XmlFormatting fmt = XmlFormatting::Newline | XmlFormatting::Indent);
XmlWriter& endElement();
XmlWriter& endElement(XmlFormatting fmt = XmlFormatting::Newline | XmlFormatting::Indent);
XmlWriter& writeAttribute( std::string const& name, std::string const& attribute );
@@ -77,9 +86,9 @@ namespace Catch {
return writeAttribute( name, rss.str() );
}
XmlWriter& writeText( std::string const& text, bool indent = true );
XmlWriter& writeText( std::string const& text, XmlFormatting fmt = XmlFormatting::Newline | XmlFormatting::Indent);
XmlWriter& writeComment( std::string const& text );
XmlWriter& writeComment(std::string const& text, XmlFormatting fmt = XmlFormatting::Newline | XmlFormatting::Indent);
void writeStylesheetRef( std::string const& url );
@@ -89,6 +98,8 @@ namespace Catch {
private:
void applyFormatting(XmlFormatting fmt);
void writeDeclaration();
void newlineIfNecessary();