2010-12-10 03:01:42 -05: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
|
|
|
|
|
2010-12-10 15:01:40 -05:00
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2012-05-15 03:02:36 -04:00
|
|
|
namespace Catch {
|
|
|
|
|
|
|
|
class XmlWriter {
|
2010-12-10 03:01:42 -05:00
|
|
|
public:
|
|
|
|
|
2012-05-15 03:02:36 -04:00
|
|
|
class ScopedElement {
|
2010-12-10 03:01:42 -05:00
|
|
|
public:
|
2012-05-15 03:02:36 -04:00
|
|
|
ScopedElement( XmlWriter* writer )
|
2010-12-10 03:01:42 -05:00
|
|
|
: m_writer( writer )
|
2012-05-15 03:02:36 -04:00
|
|
|
{}
|
2010-12-10 03:01:42 -05:00
|
|
|
|
2012-05-15 03:02:36 -04:00
|
|
|
ScopedElement( const ScopedElement& other )
|
|
|
|
: m_writer( other.m_writer ){
|
2010-12-10 03:01:42 -05:00
|
|
|
other.m_writer = NULL;
|
|
|
|
}
|
|
|
|
|
2012-05-15 03:02:36 -04:00
|
|
|
~ScopedElement() {
|
2010-12-10 03:01:42 -05:00
|
|
|
if( m_writer )
|
|
|
|
m_writer->endElement();
|
|
|
|
}
|
|
|
|
|
2012-05-15 03:02:36 -04:00
|
|
|
ScopedElement& writeText( const std::string& text ) {
|
2010-12-10 03:01:42 -05:00
|
|
|
m_writer->writeText( text );
|
|
|
|
return *this;
|
|
|
|
}
|
2011-02-03 15:00:46 -05:00
|
|
|
|
2010-12-10 15:01:40 -05:00
|
|
|
template<typename T>
|
2012-05-15 03:02:36 -04:00
|
|
|
ScopedElement& writeAttribute( const std::string& name, const T& attribute ) {
|
2010-12-10 15:01:40 -05:00
|
|
|
m_writer->writeAttribute( name, attribute );
|
|
|
|
return *this;
|
|
|
|
}
|
2010-12-10 03:01:42 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
mutable XmlWriter* m_writer;
|
|
|
|
};
|
|
|
|
|
2012-05-15 03:02:36 -04:00
|
|
|
XmlWriter()
|
2010-12-10 15:01:40 -05:00
|
|
|
: m_tagIsOpen( false ),
|
|
|
|
m_needsNewline( false ),
|
|
|
|
m_os( &std::cout )
|
2012-05-15 03:02:36 -04:00
|
|
|
{}
|
2010-12-10 15:01:40 -05:00
|
|
|
|
2012-05-15 03:02:36 -04:00
|
|
|
XmlWriter( std::ostream& os )
|
2010-12-10 03:01:42 -05:00
|
|
|
: m_tagIsOpen( false ),
|
|
|
|
m_needsNewline( false ),
|
2010-12-10 15:01:40 -05:00
|
|
|
m_os( &os )
|
2012-05-15 03:02:36 -04:00
|
|
|
{}
|
2010-12-10 03:01:42 -05:00
|
|
|
|
2012-05-15 03:02:36 -04:00
|
|
|
~XmlWriter() {
|
2010-12-10 03:01:42 -05:00
|
|
|
while( !m_tags.empty() )
|
|
|
|
endElement();
|
|
|
|
}
|
2010-12-10 15:01:40 -05:00
|
|
|
|
2012-05-15 03:02:36 -04:00
|
|
|
XmlWriter& operator = ( const XmlWriter& other ) {
|
2010-12-10 15:01:40 -05:00
|
|
|
XmlWriter temp( other );
|
|
|
|
swap( temp );
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2012-05-15 03:02:36 -04:00
|
|
|
void swap( XmlWriter& other ) {
|
2010-12-10 15:01:40 -05:00
|
|
|
std::swap( m_tagIsOpen, other.m_tagIsOpen );
|
|
|
|
std::swap( m_needsNewline, other.m_needsNewline );
|
|
|
|
std::swap( m_tags, other.m_tags );
|
|
|
|
std::swap( m_indent, other.m_indent );
|
|
|
|
std::swap( m_os, other.m_os );
|
|
|
|
}
|
2010-12-10 03:01:42 -05:00
|
|
|
|
2012-05-15 03:02:36 -04:00
|
|
|
XmlWriter& startElement( const std::string& name ) {
|
2010-12-10 03:01:42 -05:00
|
|
|
ensureTagClosed();
|
|
|
|
newlineIfNecessary();
|
2010-12-10 15:01:40 -05:00
|
|
|
stream() << m_indent << "<" << name;
|
2010-12-10 03:01:42 -05:00
|
|
|
m_tags.push_back( name );
|
|
|
|
m_indent += " ";
|
|
|
|
m_tagIsOpen = true;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2012-05-15 03:02:36 -04:00
|
|
|
ScopedElement scopedElement( const std::string& name ) {
|
2010-12-10 03:01:42 -05:00
|
|
|
ScopedElement scoped( this );
|
|
|
|
startElement( name );
|
|
|
|
return scoped;
|
|
|
|
}
|
|
|
|
|
2012-05-15 03:02:36 -04:00
|
|
|
XmlWriter& endElement() {
|
2010-12-10 03:01:42 -05:00
|
|
|
newlineIfNecessary();
|
|
|
|
m_indent = m_indent.substr( 0, m_indent.size()-2 );
|
2012-05-15 03:02:36 -04:00
|
|
|
if( m_tagIsOpen ) {
|
2010-12-10 15:01:40 -05:00
|
|
|
stream() << "/>\n";
|
2010-12-10 03:01:42 -05:00
|
|
|
m_tagIsOpen = false;
|
|
|
|
}
|
2012-05-15 03:02:36 -04:00
|
|
|
else {
|
2010-12-10 15:01:40 -05:00
|
|
|
stream() << m_indent << "</" << m_tags.back() << ">\n";
|
2010-12-10 03:01:42 -05:00
|
|
|
}
|
|
|
|
m_tags.pop_back();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2012-05-15 03:02:36 -04:00
|
|
|
XmlWriter& writeAttribute( const std::string& name, const std::string& attribute ) {
|
|
|
|
if( !name.empty() && !attribute.empty() ) {
|
2010-12-10 15:01:40 -05:00
|
|
|
stream() << " " << name << "=\"";
|
2010-12-10 03:01:42 -05:00
|
|
|
writeEncodedText( attribute );
|
2010-12-10 15:01:40 -05:00
|
|
|
stream() << "\"";
|
2010-12-10 03:01:42 -05:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
2011-02-03 15:00:46 -05:00
|
|
|
|
2012-05-15 03:02:36 -04:00
|
|
|
XmlWriter& writeAttribute( const std::string& name, bool attribute ) {
|
2010-12-10 15:01:40 -05:00
|
|
|
stream() << " " << name << "=\"" << ( attribute ? "true" : "false" ) << "\"";
|
|
|
|
return *this;
|
|
|
|
}
|
2010-12-10 03:01:42 -05:00
|
|
|
|
2010-12-10 03:18:06 -05:00
|
|
|
template<typename T>
|
2012-05-15 03:02:36 -04:00
|
|
|
XmlWriter& writeAttribute( const std::string& name, const T& attribute ) {
|
2010-12-10 03:18:06 -05:00
|
|
|
if( !name.empty() )
|
2010-12-10 15:01:40 -05:00
|
|
|
stream() << " " << name << "=\"" << attribute << "\"";
|
2010-12-10 03:18:06 -05:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2012-05-15 03:02:36 -04:00
|
|
|
XmlWriter& writeText( const std::string& text ) {
|
|
|
|
if( !text.empty() ){
|
2010-12-10 03:01:42 -05:00
|
|
|
bool tagWasOpen = m_tagIsOpen;
|
|
|
|
ensureTagClosed();
|
|
|
|
if( tagWasOpen )
|
2010-12-10 15:01:40 -05:00
|
|
|
stream() << m_indent;
|
2010-12-10 03:01:42 -05:00
|
|
|
writeEncodedText( text );
|
|
|
|
m_needsNewline = true;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2012-05-15 03:02:36 -04:00
|
|
|
XmlWriter& writeComment( const std::string& text ) {
|
2010-12-10 03:01:42 -05:00
|
|
|
ensureTagClosed();
|
2010-12-10 15:01:40 -05:00
|
|
|
stream() << m_indent << "<!--" << text << "-->";
|
2010-12-10 03:01:42 -05:00
|
|
|
m_needsNewline = true;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2012-05-15 03:02:36 -04:00
|
|
|
XmlWriter& writeBlankLine() {
|
2010-12-10 03:01:42 -05:00
|
|
|
ensureTagClosed();
|
2010-12-10 15:01:40 -05:00
|
|
|
stream() << "\n";
|
2010-12-10 03:01:42 -05:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2012-05-15 03:02:36 -04:00
|
|
|
std::ostream& stream() {
|
2010-12-10 15:01:40 -05:00
|
|
|
return *m_os;
|
|
|
|
}
|
|
|
|
|
2012-05-15 03:02:36 -04:00
|
|
|
void ensureTagClosed() {
|
|
|
|
if( m_tagIsOpen ) {
|
2010-12-10 15:01:40 -05:00
|
|
|
stream() << ">\n";
|
2010-12-10 03:01:42 -05:00
|
|
|
m_tagIsOpen = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-15 03:02:36 -04:00
|
|
|
void newlineIfNecessary() {
|
|
|
|
if( m_needsNewline ) {
|
2010-12-10 15:01:40 -05:00
|
|
|
stream() << "\n";
|
2010-12-10 03:01:42 -05:00
|
|
|
m_needsNewline = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-15 03:02:36 -04:00
|
|
|
void writeEncodedText( const std::string& text ) {
|
2012-02-10 13:58:06 -05:00
|
|
|
static const char* charsToEncode = "<&\"";
|
|
|
|
std::string mtext = text;
|
|
|
|
std::string::size_type pos = mtext.find_first_of( charsToEncode );
|
2012-05-15 03:02:36 -04:00
|
|
|
while( pos != std::string::npos ) {
|
2012-02-10 13:58:06 -05:00
|
|
|
stream() << mtext.substr( 0, pos );
|
|
|
|
|
2012-05-15 03:02:36 -04:00
|
|
|
switch( mtext[pos] ) {
|
2012-02-10 13:58:06 -05:00
|
|
|
case '<':
|
|
|
|
stream() << "<";
|
|
|
|
break;
|
|
|
|
case '&':
|
|
|
|
stream() << "&";
|
|
|
|
break;
|
|
|
|
case '\"':
|
|
|
|
stream() << """;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mtext = mtext.substr( pos+1 );
|
|
|
|
pos = mtext.find_first_of( charsToEncode );
|
2010-12-10 03:01:42 -05:00
|
|
|
}
|
2012-02-10 13:58:06 -05:00
|
|
|
stream() << mtext;
|
|
|
|
}
|
2010-12-10 03:01:42 -05:00
|
|
|
|
|
|
|
bool m_tagIsOpen;
|
|
|
|
bool m_needsNewline;
|
|
|
|
std::vector<std::string> m_tags;
|
|
|
|
std::string m_indent;
|
2010-12-10 15:01:40 -05:00
|
|
|
std::ostream* m_os;
|
2010-12-10 03:01:42 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
2011-05-24 03:23:02 -04:00
|
|
|
#endif // TWOBLUECUBES_CATCH_XMLWRITER_HPP_INCLUDED
|