catch_xmlwriter.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Created by Phil on 09/12/2010.
  3. * Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
  4. *
  5. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. */
  8. #ifndef TWOBLUECUBES_CATCH_XMLWRITER_HPP_INCLUDED
  9. #define TWOBLUECUBES_CATCH_XMLWRITER_HPP_INCLUDED
  10. #include "catch_stream.h"
  11. #include "catch_compiler_capabilities.h"
  12. #include <vector>
  13. namespace Catch {
  14. enum class XmlFormatting {
  15. None = 0x00,
  16. Indent = 0x01,
  17. Newline = 0x02,
  18. };
  19. XmlFormatting operator | (XmlFormatting lhs, XmlFormatting rhs);
  20. XmlFormatting operator & (XmlFormatting lhs, XmlFormatting rhs);
  21. class XmlEncode {
  22. public:
  23. enum ForWhat { ForTextNodes, ForAttributes };
  24. XmlEncode( std::string const& str, ForWhat forWhat = ForTextNodes );
  25. void encodeTo( std::ostream& os ) const;
  26. friend std::ostream& operator << ( std::ostream& os, XmlEncode const& xmlEncode );
  27. private:
  28. std::string m_str;
  29. ForWhat m_forWhat;
  30. };
  31. class XmlWriter {
  32. public:
  33. class ScopedElement {
  34. public:
  35. ScopedElement( XmlWriter* writer, XmlFormatting fmt );
  36. ScopedElement( ScopedElement&& other ) noexcept;
  37. ScopedElement& operator=( ScopedElement&& other ) noexcept;
  38. ~ScopedElement();
  39. ScopedElement& writeText( std::string const& text, XmlFormatting fmt = XmlFormatting::Newline | XmlFormatting::Indent );
  40. template<typename T>
  41. ScopedElement& writeAttribute( std::string const& name, T const& attribute ) {
  42. m_writer->writeAttribute( name, attribute );
  43. return *this;
  44. }
  45. private:
  46. mutable XmlWriter* m_writer = nullptr;
  47. XmlFormatting m_fmt;
  48. };
  49. XmlWriter( std::ostream& os = Catch::cout() );
  50. ~XmlWriter();
  51. XmlWriter( XmlWriter const& ) = delete;
  52. XmlWriter& operator=( XmlWriter const& ) = delete;
  53. XmlWriter& startElement( std::string const& name, XmlFormatting fmt = XmlFormatting::Newline | XmlFormatting::Indent);
  54. ScopedElement scopedElement( std::string const& name, XmlFormatting fmt = XmlFormatting::Newline | XmlFormatting::Indent);
  55. XmlWriter& endElement(XmlFormatting fmt = XmlFormatting::Newline | XmlFormatting::Indent);
  56. XmlWriter& writeAttribute( std::string const& name, std::string const& attribute );
  57. XmlWriter& writeAttribute( std::string const& name, bool attribute );
  58. template<typename T>
  59. XmlWriter& writeAttribute( std::string const& name, T const& attribute ) {
  60. ReusableStringStream rss;
  61. rss << attribute;
  62. return writeAttribute( name, rss.str() );
  63. }
  64. XmlWriter& writeText( std::string const& text, XmlFormatting fmt = XmlFormatting::Newline | XmlFormatting::Indent);
  65. XmlWriter& writeComment(std::string const& text, XmlFormatting fmt = XmlFormatting::Newline | XmlFormatting::Indent);
  66. void writeStylesheetRef( std::string const& url );
  67. XmlWriter& writeBlankLine();
  68. void ensureTagClosed();
  69. private:
  70. void applyFormatting(XmlFormatting fmt);
  71. void writeDeclaration();
  72. void newlineIfNecessary();
  73. bool m_tagIsOpen = false;
  74. bool m_needsNewline = false;
  75. std::vector<std::string> m_tags;
  76. std::string m_indent;
  77. std::ostream& m_os;
  78. };
  79. }
  80. #endif // TWOBLUECUBES_CATCH_XMLWRITER_HPP_INCLUDED