catch2/include/internal/catch_xmlwriter.hpp

108 lines
2.8 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 <sstream>
#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 );
2017-07-19 10:13:47 +02:00
ScopedElement( ScopedElement const& other );
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;
};
2017-07-19 10:13:47 +02:00
XmlWriter( std::ostream& os = Catch::cout() );
~XmlWriter();
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 ) {
2017-07-18 09:35:41 +02:00
m_oss.clear();
m_oss.str(std::string());
m_oss << attribute;
return writeAttribute( name, m_oss.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;
2017-07-18 09:35:41 +02:00
std::ostringstream m_oss;
2010-12-10 09:01:42 +01:00
};
2010-12-10 09:01:42 +01:00
}
#endif // TWOBLUECUBES_CATCH_XMLWRITER_HPP_INCLUDED