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
|
|
|
|
|
2015-07-01 08:33:27 +02:00
|
|
|
#include "catch_stream.h"
|
|
|
|
#include "catch_compiler_capabilities.h"
|
2014-10-02 20:13:21 +02:00
|
|
|
|
2010-12-10 21:01:40 +01:00
|
|
|
#include <vector>
|
|
|
|
|
2012-05-15 09:02:36 +02:00
|
|
|
namespace Catch {
|
|
|
|
|
2015-07-23 19:45:31 +02:00
|
|
|
class XmlEncode {
|
|
|
|
public:
|
|
|
|
enum ForWhat { ForTextNodes, ForAttributes };
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2017-07-19 10:13:47 +02:00
|
|
|
XmlEncode( std::string const& str, ForWhat forWhat = ForTextNodes );
|
2015-07-23 19:45:31 +02:00
|
|
|
|
2017-07-19 10:13:47 +02:00
|
|
|
void encodeTo( std::ostream& os ) const;
|
|
|
|
|
|
|
|
friend std::ostream& operator << ( std::ostream& os, XmlEncode const& xmlEncode );
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2015-07-23 19:45:31 +02:00
|
|
|
private:
|
|
|
|
std::string m_str;
|
|
|
|
ForWhat m_forWhat;
|
|
|
|
};
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2012-05-15 09:02:36 +02:00
|
|
|
class XmlWriter {
|
2010-12-10 09:01:42 +01:00
|
|
|
public:
|
2013-07-03 20:14:59 +02:00
|
|
|
|
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 );
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2017-07-25 17:16:28 +02:00
|
|
|
ScopedElement( ScopedElement&& other ) noexcept;
|
|
|
|
ScopedElement& operator=( ScopedElement&& other ) noexcept;
|
2013-07-03 20:14:59 +02:00
|
|
|
|
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 );
|
2011-02-03 21:00:46 +01:00
|
|
|
|
2010-12-10 21:01:40 +01:00
|
|
|
template<typename T>
|
2013-04-23 19:58:56 +02:00
|
|
|
ScopedElement& writeAttribute( std::string const& name, T const& attribute ) {
|
2010-12-10 21:01:40 +01:00
|
|
|
m_writer->writeAttribute( name, attribute );
|
|
|
|
return *this;
|
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2010-12-10 09:01:42 +01:00
|
|
|
private:
|
2017-07-20 00:27:28 +02:00
|
|
|
mutable XmlWriter* m_writer = nullptr;
|
2010-12-10 09:01:42 +01:00
|
|
|
};
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2017-07-19 10:13:47 +02:00
|
|
|
XmlWriter( std::ostream& os = Catch::cout() );
|
|
|
|
~XmlWriter();
|
2018-03-25 20:44:30 +02:00
|
|
|
|
2017-07-19 10:13:47 +02:00
|
|
|
XmlWriter( XmlWriter const& ) = delete;
|
|
|
|
XmlWriter& operator=( XmlWriter const& ) = delete;
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2017-07-19 10:13:47 +02:00
|
|
|
XmlWriter& startElement( std::string const& name );
|
2010-12-10 21:01:40 +01:00
|
|
|
|
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();
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2017-07-19 10:13:47 +02:00
|
|
|
XmlWriter& writeAttribute( std::string const& name, std::string const& attribute );
|
2011-02-03 21:00:46 +01:00
|
|
|
|
2017-07-19 10:13:47 +02:00
|
|
|
XmlWriter& writeAttribute( std::string const& name, bool attribute );
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2010-12-10 09:18:06 +01:00
|
|
|
template<typename T>
|
2013-04-23 19:58:56 +02:00
|
|
|
XmlWriter& writeAttribute( std::string const& name, T const& attribute ) {
|
2017-11-07 19:01:10 +01:00
|
|
|
ReusableStringStream rss;
|
|
|
|
rss << attribute;
|
|
|
|
return writeAttribute( name, rss.str() );
|
2010-12-10 09:18:06 +01:00
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2017-07-19 10:13:47 +02:00
|
|
|
XmlWriter& writeText( std::string const& text, bool indent = true );
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2017-07-19 10:13:47 +02:00
|
|
|
XmlWriter& writeComment( std::string const& text );
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2017-07-19 10:13:47 +02:00
|
|
|
void writeStylesheetRef( std::string const& url );
|
2017-02-08 00:09:43 +01:00
|
|
|
|
2017-07-19 10:13:47 +02:00
|
|
|
XmlWriter& writeBlankLine();
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2017-07-19 10:13:47 +02:00
|
|
|
void ensureTagClosed();
|
2017-02-13 16:56:25 +01:00
|
|
|
|
2010-12-10 09:01:42 +01:00
|
|
|
private:
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2017-07-19 10:13:47 +02:00
|
|
|
void writeDeclaration();
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2017-07-19 10:13:47 +02:00
|
|
|
void newlineIfNecessary();
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2017-04-25 19:56:53 +02:00
|
|
|
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;
|
2017-02-06 17:00:05 +01:00
|
|
|
std::ostream& m_os;
|
2010-12-10 09:01:42 +01:00
|
|
|
};
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2010-12-10 09:01:42 +01:00
|
|
|
}
|
2015-07-23 20:03:33 +02:00
|
|
|
|
2011-05-24 09:23:02 +02:00
|
|
|
#endif // TWOBLUECUBES_CATCH_XMLWRITER_HPP_INCLUDED
|