catch2/include/internal/catch_xmlwriter.h

117 lines
3.5 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
#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 {
enum class XmlFormatting {
None = 0x00,
Indent = 0x01,
Newline = 0x02,
};
XmlFormatting operator | (XmlFormatting lhs, XmlFormatting rhs);
XmlFormatting operator & (XmlFormatting lhs, XmlFormatting rhs);
2012-05-15 09:02:36 +02:00
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:
ScopedElement( XmlWriter* writer, XmlFormatting fmt );
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
ScopedElement& writeText( std::string const& text, XmlFormatting fmt = XmlFormatting::Newline | XmlFormatting::Indent );
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;
XmlFormatting m_fmt;
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;
XmlWriter& startElement( std::string const& name, XmlFormatting fmt = XmlFormatting::Newline | XmlFormatting::Indent);
ScopedElement scopedElement( std::string const& name, XmlFormatting fmt = XmlFormatting::Newline | XmlFormatting::Indent);
2010-12-10 09:01:42 +01:00
XmlWriter& endElement(XmlFormatting fmt = XmlFormatting::Newline | XmlFormatting::Indent);
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() );
}
XmlWriter& writeText( std::string const& text, XmlFormatting fmt = XmlFormatting::Newline | XmlFormatting::Indent);
XmlWriter& writeComment(std::string const& text, XmlFormatting fmt = XmlFormatting::Newline | XmlFormatting::Indent);
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:
void applyFormatting(XmlFormatting fmt);
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