catch2/include/internal/catch_xmlwriter.h

106 lines
2.8 KiB
C
Raw Permalink 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
#include "catch_stream.h"
#include "catch_compiler_capabilities.h"
2014-10-02 20:13:21 +02:00
#include <vector>
2012-05-15 09:02:36 +02:00
namespace Catch {
class XmlEncode {
public:
enum ForWhat { ForTextNodes, ForAttributes };
2017-07-19 10:13:47 +02:00
XmlEncode( std::string const& str, ForWhat forWhat = ForTextNodes );
2017-07-19 10:13:47 +02:00
void encodeTo( std::ostream& os ) const;
friend std::ostream& operator << ( std::ostream& os, XmlEncode const& xmlEncode );
private:
std::string m_str;
ForWhat m_forWhat;
};
2012-05-15 09:02:36 +02:00
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:
2017-07-19 10:13:47 +02:00
ScopedElement( XmlWriter* writer );
ScopedElement( ScopedElement&& other ) noexcept;
ScopedElement& operator=( ScopedElement&& other ) noexcept;
2017-07-19 10:13:47 +02:00
~ScopedElement();
2010-12-10 09:01:42 +01:00
2017-07-19 10:13:47 +02:00
ScopedElement& writeText( std::string const& text, bool indent = true );
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 = nullptr;
2010-12-10 09:01:42 +01:00
};
2017-07-19 10:13:47 +02:00
XmlWriter( std::ostream& os = Catch::cout() );
~XmlWriter();
2017-07-19 10:13:47 +02:00
XmlWriter( XmlWriter const& ) = delete;
XmlWriter& operator=( XmlWriter const& ) = delete;
2017-07-19 10:13:47 +02:00
XmlWriter& startElement( std::string const& name );
2017-07-19 10:13:47 +02:00
ScopedElement scopedElement( std::string const& name );
2010-12-10 09:01:42 +01:00
2017-07-19 10:13:47 +02:00
XmlWriter& endElement();
2017-07-19 10:13:47 +02:00
XmlWriter& writeAttribute( std::string const& name, std::string const& attribute );
2017-07-19 10:13:47 +02:00
XmlWriter& writeAttribute( std::string const& name, bool attribute );
template<typename T>
XmlWriter& writeAttribute( std::string const& name, T const& attribute ) {
ReusableStringStream rss;
rss << attribute;
return writeAttribute( name, rss.str() );
}
2017-07-19 10:13:47 +02:00
XmlWriter& writeText( std::string const& text, bool indent = true );
2017-07-19 10:13:47 +02:00
XmlWriter& writeComment( std::string const& text );
2017-07-19 10:13:47 +02:00
void writeStylesheetRef( std::string const& url );
2017-07-19 10:13:47 +02:00
XmlWriter& writeBlankLine();
2017-07-19 10:13:47 +02:00
void ensureTagClosed();
2010-12-10 09:01:42 +01:00
private:
2017-07-19 10:13:47 +02:00
void writeDeclaration();
2017-07-19 10:13:47 +02:00
void newlineIfNecessary();
bool m_tagIsOpen = false;
bool m_needsNewline = false;
2010-12-10 09:01:42 +01:00
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