catch2/include/internal/catch_xmlwriter.hpp

333 lines
9.0 KiB
C++
Raw Normal View History

2010-12-10 09:01:42 +01:00
/*
* catch_xmlwriter.hpp
* Catch
*
* 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
#include <sstream>
#include <string>
#include <vector>
2010-12-10 09:01:42 +01:00
namespace Catch
{
class XmlWriter
{
public:
class ScopedElement
{
public:
///////////////////////////////////////////////////////////////////
ScopedElement
(
XmlWriter* writer
)
2010-12-10 09:01:42 +01:00
: m_writer( writer )
{
}
///////////////////////////////////////////////////////////////////
ScopedElement
(
const ScopedElement& other
)
2010-12-10 09:01:42 +01:00
: m_writer( other.m_writer )
{
other.m_writer = NULL;
}
///////////////////////////////////////////////////////////////////
~ScopedElement
()
2010-12-10 09:01:42 +01:00
{
if( m_writer )
m_writer->endElement();
}
///////////////////////////////////////////////////////////////////
ScopedElement& writeText
(
const std::string& text
)
2010-12-10 09:01:42 +01:00
{
m_writer->writeText( text );
return *this;
}
///////////////////////////////////////////////////////////////////
template<typename T>
ScopedElement& writeAttribute
(
const std::string& name,
const T& attribute
)
{
m_writer->writeAttribute( name, attribute );
return *this;
}
2010-12-10 09:01:42 +01:00
private:
mutable XmlWriter* m_writer;
};
///////////////////////////////////////////////////////////////////////
XmlWriter
()
: m_tagIsOpen( false ),
m_needsNewline( false ),
m_os( &std::cout )
{
}
///////////////////////////////////////////////////////////////////////
XmlWriter
(
std::ostream& os
)
2010-12-10 09:01:42 +01:00
: m_tagIsOpen( false ),
m_needsNewline( false ),
m_os( &os )
2010-12-10 09:01:42 +01:00
{
}
///////////////////////////////////////////////////////////////////////
~XmlWriter
()
2010-12-10 09:01:42 +01:00
{
while( !m_tags.empty() )
{
endElement();
}
}
///////////////////////////////////////////////////////////////////////
XmlWriter& operator =
(
const XmlWriter& other
)
{
XmlWriter temp( other );
swap( temp );
return *this;
}
///////////////////////////////////////////////////////////////////////
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 );
std::swap( m_indent, other.m_indent );
std::swap( m_os, other.m_os );
}
2010-12-10 09:01:42 +01:00
///////////////////////////////////////////////////////////////////////
XmlWriter& startElement
(
const std::string& 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
(
const std::string& name
)
2010-12-10 09:01:42 +01:00
{
ScopedElement scoped( this );
startElement( name );
return scoped;
}
///////////////////////////////////////////////////////////////////////
XmlWriter& endElement
()
2010-12-10 09:01:42 +01:00
{
newlineIfNecessary();
m_indent = m_indent.substr( 0, m_indent.size()-2 );
if( m_tagIsOpen )
{
stream() << "/>\n";
2010-12-10 09:01:42 +01:00
m_tagIsOpen = false;
}
else
{
stream() << m_indent << "</" << m_tags.back() << ">\n";
2010-12-10 09:01:42 +01:00
}
m_tags.pop_back();
return *this;
}
///////////////////////////////////////////////////////////////////////
XmlWriter& writeAttribute
(
const std::string& name,
const std::string& attribute
)
2010-12-10 09:01:42 +01: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
(
const std::string& name,
bool attribute
)
{
stream() << " " << name << "=\"" << ( attribute ? "true" : "false" ) << "\"";
return *this;
}
2010-12-10 09:01:42 +01:00
///////////////////////////////////////////////////////////////////////
template<typename T>
XmlWriter& writeAttribute
(
const std::string& name,
const T& attribute
)
{
if( !name.empty() )
{
stream() << " " << name << "=\"" << attribute << "\"";
}
return *this;
}
///////////////////////////////////////////////////////////////////////
XmlWriter& writeText
(
const std::string& text
)
2010-12-10 09:01:42 +01:00
{
if( !text.empty() )
{
bool tagWasOpen = m_tagIsOpen;
ensureTagClosed();
if( tagWasOpen )
stream() << m_indent;
2010-12-10 09:01:42 +01:00
writeEncodedText( text );
m_needsNewline = true;
}
return *this;
}
///////////////////////////////////////////////////////////////////////
XmlWriter& writeComment
(
const std::string& 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;
}
///////////////////////////////////////////////////////////////////////
XmlWriter& writeBlankLine
()
2010-12-10 09:01:42 +01:00
{
ensureTagClosed();
stream() << "\n";
2010-12-10 09:01:42 +01:00
return *this;
}
private:
///////////////////////////////////////////////////////////////////////
std::ostream& stream
()
{
return *m_os;
}
///////////////////////////////////////////////////////////////////////
void ensureTagClosed
()
2010-12-10 09:01:42 +01:00
{
if( m_tagIsOpen )
{
stream() << ">\n";
2010-12-10 09:01:42 +01:00
m_tagIsOpen = false;
}
}
///////////////////////////////////////////////////////////////////////
void newlineIfNecessary
()
2010-12-10 09:01:42 +01:00
{
if( m_needsNewline )
{
stream() << "\n";
2010-12-10 09:01:42 +01:00
m_needsNewline = false;
}
}
///////////////////////////////////////////////////////////////////////
void writeEncodedText
(
const std::string& text
)
2010-12-10 09:01:42 +01:00
{
// !TBD finish this
if( !findReplaceableString( text, "<", "&lt;" ) &&
!findReplaceableString( text, "&", "&amp;" ) &&
!findReplaceableString( text, "\"", "&quote;" ) )
{
stream() << text;
2010-12-10 09:01:42 +01:00
}
}
///////////////////////////////////////////////////////////////////////
bool findReplaceableString
(
const std::string& text,
const std::string& replaceWhat,
const std::string& replaceWith
)
2010-12-10 09:01:42 +01:00
{
std::string::size_type pos = text.find_first_of( replaceWhat );
if( pos != std::string::npos )
{
stream() << text.substr( 0, pos ) << replaceWith;
2010-12-10 09:01:42 +01:00
writeEncodedText( text.substr( pos+1 ) );
return true;
}
return false;
}
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
};
}
#endif