xml_printer.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // (C) Copyright Gennadiy Rozental 2001.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // See http://www.boost.org/libs/test for the library home page.
  6. //
  7. // File : $RCSfile$
  8. //
  9. // Version : $Revision$
  10. //
  11. // Description : common code used by any agent serving as OF_XML printer
  12. // ***************************************************************************
  13. #ifndef BOOST_TEST_UTILS_XML_PRINTER_HPP
  14. #define BOOST_TEST_UTILS_XML_PRINTER_HPP
  15. // Boost.Test
  16. #include <boost/test/detail/global_typedef.hpp>
  17. #include <boost/test/utils/basic_cstring/basic_cstring.hpp>
  18. #include <boost/test/utils/custom_manip.hpp>
  19. #include <boost/test/utils/foreach.hpp>
  20. #include <boost/test/utils/basic_cstring/io.hpp>
  21. // Boost
  22. #include <boost/config.hpp>
  23. // STL
  24. #include <iostream>
  25. #include <map>
  26. #include <boost/test/detail/suppress_warnings.hpp>
  27. //____________________________________________________________________________//
  28. namespace boost {
  29. namespace unit_test {
  30. namespace utils {
  31. // ************************************************************************** //
  32. // ************** xml print helpers ************** //
  33. // ************************************************************************** //
  34. inline void
  35. print_escaped( std::ostream& where_to, const_string value )
  36. {
  37. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) && !defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX)
  38. static std::map<char,char const*> const char_type{{
  39. {'<' , "lt"},
  40. {'>' , "gt"},
  41. {'&' , "amp"},
  42. {'\'', "apos"},
  43. {'"' , "quot"}
  44. }};
  45. #else
  46. static std::map<char,char const*> char_type;
  47. if( char_type.empty() ) {
  48. char_type['<'] = "lt";
  49. char_type['>'] = "gt";
  50. char_type['&'] = "amp";
  51. char_type['\'']= "apos";
  52. char_type['"'] = "quot";
  53. }
  54. #endif
  55. BOOST_TEST_FOREACH( char, c, value ) {
  56. std::map<char,char const*>::const_iterator found_ref = char_type.find( c );
  57. if( found_ref != char_type.end() )
  58. where_to << '&' << found_ref->second << ';';
  59. else
  60. where_to << c;
  61. }
  62. }
  63. //____________________________________________________________________________//
  64. inline void
  65. print_escaped( std::ostream& where_to, std::string const& value )
  66. {
  67. print_escaped( where_to, const_string( value ) );
  68. }
  69. //____________________________________________________________________________//
  70. template<typename T>
  71. inline void
  72. print_escaped( std::ostream& where_to, T const& value )
  73. {
  74. where_to << value;
  75. }
  76. //____________________________________________________________________________//
  77. inline void
  78. print_escaped_cdata( std::ostream& where_to, const_string value )
  79. {
  80. static const_string cdata_end( "]]>" );
  81. const_string::size_type pos = value.find( cdata_end );
  82. if( pos == const_string::npos )
  83. where_to << value;
  84. else {
  85. where_to << value.substr( 0, pos+2 ) << cdata_end
  86. << BOOST_TEST_L( "<![CDATA[" ) << value.substr( pos+2 );
  87. }
  88. }
  89. //____________________________________________________________________________//
  90. typedef custom_manip<struct attr_value_t> attr_value;
  91. template<typename T>
  92. inline std::ostream&
  93. operator<<( custom_printer<attr_value> const& p, T const& value )
  94. {
  95. *p << "=\"";
  96. print_escaped( *p, value );
  97. *p << '"';
  98. return *p;
  99. }
  100. //____________________________________________________________________________//
  101. typedef custom_manip<struct cdata_t> cdata;
  102. inline std::ostream&
  103. operator<<( custom_printer<cdata> const& p, const_string value )
  104. {
  105. *p << BOOST_TEST_L( "<![CDATA[" );
  106. print_escaped_cdata( *p, value );
  107. return *p << BOOST_TEST_L( "]]>" );
  108. }
  109. //____________________________________________________________________________//
  110. } // namespace utils
  111. } // namespace unit_test
  112. } // namespace boost
  113. #include <boost/test/detail/enable_warnings.hpp>
  114. #endif // BOOST_TEST_UTILS_XML_PRINTER_HPP