catch2/include/internal/catch_xmlwriter.hpp

205 lines
5.8 KiB
C++
Raw Normal View History

2010-12-10 09:01:42 +01:00
/*
* Created by Phil on 09/12/2010.
* Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
*
* Distributed under the Boost Software License, Version 1.0. (See accompanying
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
#ifndef TWOBLUECUBES_CATCH_XMLWRITER_HPP_INCLUDED
#define TWOBLUECUBES_CATCH_XMLWRITER_HPP_INCLUDED
2014-10-02 20:13:21 +02:00
#include "../internal/catch_stream.h"
#include <sstream>
#include <string>
#include <vector>
2012-05-15 09:02:36 +02:00
namespace Catch {
class XmlWriter {
2010-12-10 09:01:42 +01:00
public:
2012-05-15 09:02:36 +02:00
class ScopedElement {
2010-12-10 09:01:42 +01:00
public:
2012-05-15 09:02:36 +02:00
ScopedElement( XmlWriter* writer )
2010-12-10 09:01:42 +01:00
: m_writer( writer )
2012-05-15 09:02:36 +02:00
{}
ScopedElement( ScopedElement const& other )
2012-05-15 09:02:36 +02:00
: m_writer( other.m_writer ){
2010-12-10 09:01:42 +01:00
other.m_writer = NULL;
}
2012-05-15 09:02:36 +02:00
~ScopedElement() {
2010-12-10 09:01:42 +01:00
if( m_writer )
m_writer->endElement();
}
ScopedElement& writeText( std::string const& text, bool indent = true ) {
m_writer->writeText( text, indent );
2010-12-10 09:01:42 +01:00
return *this;
}
template<typename T>
ScopedElement& writeAttribute( std::string const& name, T const& attribute ) {
m_writer->writeAttribute( name, attribute );
return *this;
}
2010-12-10 09:01:42 +01:00
private:
mutable XmlWriter* m_writer;
};
2012-05-15 09:02:36 +02:00
XmlWriter()
: m_tagIsOpen( false ),
m_needsNewline( false ),
m_os( &Catch::cout() )
2012-05-15 09:02:36 +02:00
{}
2012-05-15 09:02:36 +02:00
XmlWriter( std::ostream& os )
2010-12-10 09:01:42 +01:00
: m_tagIsOpen( false ),
m_needsNewline( false ),
m_os( &os )
2012-05-15 09:02:36 +02:00
{}
2012-05-15 09:02:36 +02:00
~XmlWriter() {
2010-12-10 09:01:42 +01:00
while( !m_tags.empty() )
endElement();
}
XmlWriter& startElement( std::string const& name ) {
2010-12-10 09:01:42 +01:00
ensureTagClosed();
newlineIfNecessary();
stream() << m_indent << "<" << name;
2010-12-10 09:01:42 +01:00
m_tags.push_back( name );
m_indent += " ";
m_tagIsOpen = true;
return *this;
}
ScopedElement scopedElement( std::string const& name ) {
2010-12-10 09:01:42 +01:00
ScopedElement scoped( this );
startElement( name );
return scoped;
}
2012-05-15 09:02:36 +02:00
XmlWriter& endElement() {
2010-12-10 09:01:42 +01:00
newlineIfNecessary();
m_indent = m_indent.substr( 0, m_indent.size()-2 );
2012-05-15 09:02:36 +02:00
if( m_tagIsOpen ) {
stream() << "/>\n";
2010-12-10 09:01:42 +01:00
m_tagIsOpen = false;
}
2012-05-15 09:02:36 +02:00
else {
stream() << m_indent << "</" << m_tags.back() << ">\n";
}
2010-12-10 09:01:42 +01:00
m_tags.pop_back();
return *this;
}
XmlWriter& writeAttribute( std::string const& name, std::string const& attribute ) {
2012-05-15 09:02:36 +02:00
if( !name.empty() && !attribute.empty() ) {
stream() << " " << name << "=\"";
2010-12-10 09:01:42 +01:00
writeEncodedText( attribute );
stream() << "\"";
2010-12-10 09:01:42 +01:00
}
return *this;
}
XmlWriter& writeAttribute( std::string const& name, bool attribute ) {
stream() << " " << name << "=\"" << ( attribute ? "true" : "false" ) << "\"";
return *this;
}
template<typename T>
XmlWriter& writeAttribute( std::string const& name, T const& attribute ) {
if( !name.empty() )
stream() << " " << name << "=\"" << attribute << "\"";
return *this;
}
XmlWriter& writeText( std::string const& text, bool indent = true ) {
2012-05-15 09:02:36 +02:00
if( !text.empty() ){
2010-12-10 09:01:42 +01:00
bool tagWasOpen = m_tagIsOpen;
ensureTagClosed();
if( tagWasOpen && indent )
stream() << m_indent;
2010-12-10 09:01:42 +01:00
writeEncodedText( text );
m_needsNewline = true;
}
return *this;
}
XmlWriter& writeComment( std::string const& text ) {
2010-12-10 09:01:42 +01:00
ensureTagClosed();
stream() << m_indent << "<!--" << text << "-->";
2010-12-10 09:01:42 +01:00
m_needsNewline = true;
return *this;
}
2012-05-15 09:02:36 +02:00
XmlWriter& writeBlankLine() {
2010-12-10 09:01:42 +01:00
ensureTagClosed();
stream() << "\n";
2010-12-10 09:01:42 +01:00
return *this;
}
2014-05-19 19:57:14 +02:00
void setStream( std::ostream& os ) {
m_os = &os;
}
2010-12-10 09:01:42 +01:00
private:
2014-05-19 19:57:14 +02:00
XmlWriter( XmlWriter const& );
void operator=( XmlWriter const& );
2012-05-15 09:02:36 +02:00
std::ostream& stream() {
return *m_os;
}
2012-05-15 09:02:36 +02:00
void ensureTagClosed() {
if( m_tagIsOpen ) {
stream() << ">\n";
2010-12-10 09:01:42 +01:00
m_tagIsOpen = false;
}
}
2012-05-15 09:02:36 +02:00
void newlineIfNecessary() {
if( m_needsNewline ) {
stream() << "\n";
2010-12-10 09:01:42 +01:00
m_needsNewline = false;
}
}
void writeEncodedText( std::string const& text ) {
static const char* charsToEncode = "<&\"";
std::string mtext = text;
std::string::size_type pos = mtext.find_first_of( charsToEncode );
2012-05-15 09:02:36 +02:00
while( pos != std::string::npos ) {
stream() << mtext.substr( 0, pos );
2012-05-15 09:02:36 +02:00
switch( mtext[pos] ) {
case '<':
stream() << "&lt;";
break;
case '&':
stream() << "&amp;";
break;
case '\"':
stream() << "&quot;";
break;
}
mtext = mtext.substr( pos+1 );
pos = mtext.find_first_of( charsToEncode );
2010-12-10 09:01:42 +01:00
}
stream() << mtext;
}
2010-12-10 09:01:42 +01:00
bool m_tagIsOpen;
bool m_needsNewline;
std::vector<std::string> m_tags;
std::string m_indent;
std::ostream* m_os;
2010-12-10 09:01:42 +01:00
};
2010-12-10 09:01:42 +01:00
}
#endif // TWOBLUECUBES_CATCH_XMLWRITER_HPP_INCLUDED