More reformatting

This commit is contained in:
Phil Nash
2012-05-15 08:02:36 +01:00
parent 2efc1146bf
commit d0be9ed5d9
11 changed files with 124 additions and 396 deletions

View File

@@ -1,7 +1,4 @@
/*
* catch_xmlwriter.hpp
* Catch
*
* Created by Phil on 09/12/2010.
* Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
*
@@ -15,60 +12,34 @@
#include <string>
#include <vector>
namespace Catch
{
class XmlWriter
{
namespace Catch {
class XmlWriter {
public:
class ScopedElement
{
class ScopedElement {
public:
///////////////////////////////////////////////////////////////////
ScopedElement
(
XmlWriter* writer
)
ScopedElement( XmlWriter* writer )
: m_writer( writer )
{
}
{}
///////////////////////////////////////////////////////////////////
ScopedElement
(
const ScopedElement& other
)
: m_writer( other.m_writer )
{
ScopedElement( const ScopedElement& other )
: m_writer( other.m_writer ){
other.m_writer = NULL;
}
///////////////////////////////////////////////////////////////////
~ScopedElement
()
{
~ScopedElement() {
if( m_writer )
m_writer->endElement();
}
///////////////////////////////////////////////////////////////////
ScopedElement& writeText
(
const std::string& text
)
{
ScopedElement& writeText( const std::string& text ) {
m_writer->writeText( text );
return *this;
}
///////////////////////////////////////////////////////////////////
template<typename T>
ScopedElement& writeAttribute
(
const std::string& name,
const T& attribute
)
{
ScopedElement& writeAttribute( const std::string& name, const T& attribute ) {
m_writer->writeAttribute( name, attribute );
return *this;
}
@@ -77,53 +48,30 @@ namespace Catch
mutable XmlWriter* m_writer;
};
///////////////////////////////////////////////////////////////////////
XmlWriter
()
XmlWriter()
: m_tagIsOpen( false ),
m_needsNewline( false ),
m_os( &std::cout )
{
}
{}
///////////////////////////////////////////////////////////////////////
XmlWriter
(
std::ostream& os
)
XmlWriter( std::ostream& os )
: m_tagIsOpen( false ),
m_needsNewline( false ),
m_os( &os )
{
}
{}
///////////////////////////////////////////////////////////////////////
~XmlWriter
()
{
~XmlWriter() {
while( !m_tags.empty() )
{
endElement();
}
}
///////////////////////////////////////////////////////////////////////
XmlWriter& operator =
(
const XmlWriter& other
)
{
XmlWriter& operator = ( const XmlWriter& other ) {
XmlWriter temp( other );
swap( temp );
return *this;
}
///////////////////////////////////////////////////////////////////////
void swap
(
XmlWriter& other
)
{
void swap( XmlWriter& other ) {
std::swap( m_tagIsOpen, other.m_tagIsOpen );
std::swap( m_needsNewline, other.m_needsNewline );
std::swap( m_tags, other.m_tags );
@@ -131,12 +79,7 @@ namespace Catch
std::swap( m_os, other.m_os );
}
///////////////////////////////////////////////////////////////////////
XmlWriter& startElement
(
const std::string& name
)
{
XmlWriter& startElement( const std::string& name ) {
ensureTagClosed();
newlineIfNecessary();
stream() << m_indent << "<" << name;
@@ -146,45 +89,28 @@ namespace Catch
return *this;
}
///////////////////////////////////////////////////////////////////////
ScopedElement scopedElement
(
const std::string& name
)
{
ScopedElement scopedElement( const std::string& name ) {
ScopedElement scoped( this );
startElement( name );
return scoped;
}
///////////////////////////////////////////////////////////////////////
XmlWriter& endElement
()
{
XmlWriter& endElement() {
newlineIfNecessary();
m_indent = m_indent.substr( 0, m_indent.size()-2 );
if( m_tagIsOpen )
{
if( m_tagIsOpen ) {
stream() << "/>\n";
m_tagIsOpen = false;
}
else
{
else {
stream() << m_indent << "</" << m_tags.back() << ">\n";
}
m_tags.pop_back();
return *this;
}
///////////////////////////////////////////////////////////////////////
XmlWriter& writeAttribute
(
const std::string& name,
const std::string& attribute
)
{
if( !name.empty() && !attribute.empty() )
{
XmlWriter& writeAttribute( const std::string& name, const std::string& attribute ) {
if( !name.empty() && !attribute.empty() ) {
stream() << " " << name << "=\"";
writeEncodedText( attribute );
stream() << "\"";
@@ -192,40 +118,20 @@ namespace Catch
return *this;
}
///////////////////////////////////////////////////////////////////////
XmlWriter& writeAttribute
(
const std::string& name,
bool attribute
)
{
XmlWriter& writeAttribute( const std::string& name, bool attribute ) {
stream() << " " << name << "=\"" << ( attribute ? "true" : "false" ) << "\"";
return *this;
}
///////////////////////////////////////////////////////////////////////
template<typename T>
XmlWriter& writeAttribute
(
const std::string& name,
const T& attribute
)
{
XmlWriter& writeAttribute( const std::string& name, const T& attribute ) {
if( !name.empty() )
{
stream() << " " << name << "=\"" << attribute << "\"";
}
return *this;
}
///////////////////////////////////////////////////////////////////////
XmlWriter& writeText
(
const std::string& text
)
{
if( !text.empty() )
{
XmlWriter& writeText( const std::string& text ) {
if( !text.empty() ){
bool tagWasOpen = m_tagIsOpen;
ensureTagClosed();
if( tagWasOpen )
@@ -236,22 +142,14 @@ namespace Catch
return *this;
}
///////////////////////////////////////////////////////////////////////
XmlWriter& writeComment
(
const std::string& text
)
{
XmlWriter& writeComment( const std::string& text ) {
ensureTagClosed();
stream() << m_indent << "<!--" << text << "-->";
m_needsNewline = true;
return *this;
}
///////////////////////////////////////////////////////////////////////
XmlWriter& writeBlankLine
()
{
XmlWriter& writeBlankLine() {
ensureTagClosed();
stream() << "\n";
return *this;
@@ -259,50 +157,32 @@ namespace Catch
private:
///////////////////////////////////////////////////////////////////////
std::ostream& stream
()
{
std::ostream& stream() {
return *m_os;
}
///////////////////////////////////////////////////////////////////////
void ensureTagClosed
()
{
if( m_tagIsOpen )
{
void ensureTagClosed() {
if( m_tagIsOpen ) {
stream() << ">\n";
m_tagIsOpen = false;
}
}
///////////////////////////////////////////////////////////////////////
void newlineIfNecessary
()
{
if( m_needsNewline )
{
void newlineIfNecessary() {
if( m_needsNewline ) {
stream() << "\n";
m_needsNewline = false;
}
}
///////////////////////////////////////////////////////////////////////
void writeEncodedText
(
const std::string& text
)
{
void writeEncodedText( const std::string& text ) {
static const char* charsToEncode = "<&\"";
std::string mtext = text;
std::string::size_type pos = mtext.find_first_of( charsToEncode );
while( pos != std::string::npos )
{
while( pos != std::string::npos ) {
stream() << mtext.substr( 0, pos );
switch( mtext[pos] )
{
switch( mtext[pos] ) {
case '<':
stream() << "&lt;";
break;