mirror of
https://github.com/catchorg/Catch2.git
synced 2025-11-29 08:29:33 +01:00
Refactored XMLWriter to provide finer-grained control over formatting
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
#include "catch_enforce.h"
|
||||
|
||||
#include <iomanip>
|
||||
#include <type_traits>
|
||||
|
||||
using uchar = unsigned char;
|
||||
|
||||
@@ -51,8 +52,31 @@ namespace {
|
||||
os.flags(f);
|
||||
}
|
||||
|
||||
bool shouldNewline(XmlFormatting fmt) {
|
||||
return !!(static_cast<std::underlying_type<XmlFormatting>::type>(fmt & XmlFormatting::Newline));
|
||||
}
|
||||
|
||||
bool shouldIndent(XmlFormatting fmt) {
|
||||
return !!(static_cast<std::underlying_type<XmlFormatting>::type>(fmt & XmlFormatting::Indent));
|
||||
}
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
XmlFormatting operator | (XmlFormatting lhs, XmlFormatting rhs) {
|
||||
return static_cast<XmlFormatting>(
|
||||
static_cast<std::underlying_type<XmlFormatting>::type>(lhs) |
|
||||
static_cast<std::underlying_type<XmlFormatting>::type>(rhs)
|
||||
);
|
||||
}
|
||||
|
||||
XmlFormatting operator & (XmlFormatting lhs, XmlFormatting rhs) {
|
||||
return static_cast<XmlFormatting>(
|
||||
static_cast<std::underlying_type<XmlFormatting>::type>(lhs) &
|
||||
static_cast<std::underlying_type<XmlFormatting>::type>(rhs)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
XmlEncode::XmlEncode( std::string const& str, ForWhat forWhat )
|
||||
: m_str( str ),
|
||||
m_forWhat( forWhat )
|
||||
@@ -157,13 +181,17 @@ namespace {
|
||||
return os;
|
||||
}
|
||||
|
||||
XmlWriter::ScopedElement::ScopedElement( XmlWriter* writer )
|
||||
: m_writer( writer )
|
||||
XmlWriter::ScopedElement::ScopedElement( XmlWriter* writer, XmlFormatting fmt )
|
||||
: m_writer( writer ),
|
||||
m_fmt(fmt)
|
||||
{}
|
||||
|
||||
XmlWriter::ScopedElement::ScopedElement( ScopedElement&& other ) noexcept
|
||||
: m_writer( other.m_writer ){
|
||||
: m_writer( other.m_writer ),
|
||||
m_fmt(other.m_fmt)
|
||||
{
|
||||
other.m_writer = nullptr;
|
||||
other.m_fmt = XmlFormatting::None;
|
||||
}
|
||||
XmlWriter::ScopedElement& XmlWriter::ScopedElement::operator=( ScopedElement&& other ) noexcept {
|
||||
if ( m_writer ) {
|
||||
@@ -171,17 +199,20 @@ namespace {
|
||||
}
|
||||
m_writer = other.m_writer;
|
||||
other.m_writer = nullptr;
|
||||
m_fmt = other.m_fmt;
|
||||
other.m_fmt = XmlFormatting::None;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
XmlWriter::ScopedElement::~ScopedElement() {
|
||||
if( m_writer )
|
||||
m_writer->endElement();
|
||||
if (m_writer) {
|
||||
m_writer->endElement(m_fmt);
|
||||
}
|
||||
}
|
||||
|
||||
XmlWriter::ScopedElement& XmlWriter::ScopedElement::writeText( std::string const& text, bool indent ) {
|
||||
m_writer->writeText( text, indent );
|
||||
XmlWriter::ScopedElement& XmlWriter::ScopedElement::writeText( std::string const& text, XmlFormatting fmt ) {
|
||||
m_writer->writeText( text, fmt );
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -191,37 +222,47 @@ namespace {
|
||||
}
|
||||
|
||||
XmlWriter::~XmlWriter() {
|
||||
while( !m_tags.empty() )
|
||||
while (!m_tags.empty()) {
|
||||
endElement();
|
||||
}
|
||||
newlineIfNecessary();
|
||||
}
|
||||
|
||||
XmlWriter& XmlWriter::startElement( std::string const& name ) {
|
||||
XmlWriter& XmlWriter::startElement( std::string const& name, XmlFormatting fmt ) {
|
||||
ensureTagClosed();
|
||||
newlineIfNecessary();
|
||||
m_os << m_indent << '<' << name;
|
||||
if (shouldIndent(fmt)) {
|
||||
m_os << m_indent;
|
||||
m_indent += " ";
|
||||
}
|
||||
m_os << '<' << name;
|
||||
m_tags.push_back( name );
|
||||
m_indent += " ";
|
||||
m_tagIsOpen = true;
|
||||
applyFormatting(fmt);
|
||||
return *this;
|
||||
}
|
||||
|
||||
XmlWriter::ScopedElement XmlWriter::scopedElement( std::string const& name ) {
|
||||
ScopedElement scoped( this );
|
||||
startElement( name );
|
||||
XmlWriter::ScopedElement XmlWriter::scopedElement( std::string const& name, XmlFormatting fmt ) {
|
||||
ScopedElement scoped( this, fmt );
|
||||
startElement( name, fmt );
|
||||
return scoped;
|
||||
}
|
||||
|
||||
XmlWriter& XmlWriter::endElement() {
|
||||
newlineIfNecessary();
|
||||
m_indent = m_indent.substr( 0, m_indent.size()-2 );
|
||||
XmlWriter& XmlWriter::endElement(XmlFormatting fmt) {
|
||||
m_indent = m_indent.substr(0, m_indent.size() - 2);
|
||||
|
||||
if( m_tagIsOpen ) {
|
||||
m_os << "/>";
|
||||
m_tagIsOpen = false;
|
||||
} else {
|
||||
newlineIfNecessary();
|
||||
if (shouldIndent(fmt)) {
|
||||
m_os << m_indent;
|
||||
}
|
||||
m_os << "</" << m_tags.back() << ">";
|
||||
}
|
||||
else {
|
||||
m_os << m_indent << "</" << m_tags.back() << ">";
|
||||
}
|
||||
m_os << std::endl;
|
||||
m_os << std::flush;
|
||||
applyFormatting(fmt);
|
||||
m_tags.pop_back();
|
||||
return *this;
|
||||
}
|
||||
@@ -237,22 +278,26 @@ namespace {
|
||||
return *this;
|
||||
}
|
||||
|
||||
XmlWriter& XmlWriter::writeText( std::string const& text, bool indent ) {
|
||||
XmlWriter& XmlWriter::writeText( std::string const& text, XmlFormatting fmt) {
|
||||
if( !text.empty() ){
|
||||
bool tagWasOpen = m_tagIsOpen;
|
||||
ensureTagClosed();
|
||||
if( tagWasOpen && indent )
|
||||
if (tagWasOpen && shouldIndent(fmt)) {
|
||||
m_os << m_indent;
|
||||
}
|
||||
m_os << XmlEncode( text );
|
||||
m_needsNewline = true;
|
||||
applyFormatting(fmt);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
XmlWriter& XmlWriter::writeComment( std::string const& text ) {
|
||||
XmlWriter& XmlWriter::writeComment( std::string const& text, XmlFormatting fmt) {
|
||||
ensureTagClosed();
|
||||
m_os << m_indent << "<!--" << text << "-->";
|
||||
m_needsNewline = true;
|
||||
if (shouldIndent(fmt)) {
|
||||
m_os << m_indent;
|
||||
}
|
||||
m_os << "<!--" << text << "-->";
|
||||
applyFormatting(fmt);
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -268,11 +313,16 @@ namespace {
|
||||
|
||||
void XmlWriter::ensureTagClosed() {
|
||||
if( m_tagIsOpen ) {
|
||||
m_os << ">" << std::endl;
|
||||
m_os << '>' << std::flush;
|
||||
newlineIfNecessary();
|
||||
m_tagIsOpen = false;
|
||||
}
|
||||
}
|
||||
|
||||
void XmlWriter::applyFormatting(XmlFormatting fmt) {
|
||||
m_needsNewline = shouldNewline(fmt);
|
||||
}
|
||||
|
||||
void XmlWriter::writeDeclaration() {
|
||||
m_os << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user