xml_printer.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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/utils/basic_cstring/basic_cstring.hpp>
  17. #include <boost/test/utils/custom_manip.hpp>
  18. #include <boost/test/utils/foreach.hpp>
  19. #include <boost/test/utils/basic_cstring/io.hpp>
  20. // Boost
  21. #include <boost/config.hpp>
  22. // STL
  23. #include <iostream>
  24. #include <boost/test/detail/suppress_warnings.hpp>
  25. //____________________________________________________________________________//
  26. namespace boost {
  27. namespace unit_test {
  28. namespace utils {
  29. // ************************************************************************** //
  30. // ************** xml print helpers ************** //
  31. // ************************************************************************** //
  32. inline void
  33. print_escaped( std::ostream& where_to, const_string value )
  34. {
  35. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) && !defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX)
  36. static std::map<char,char const*> const char_type{{
  37. {'<' , "lt"},
  38. {'>' , "gt"},
  39. {'&' , "amp"},
  40. {'\'', "apos"},
  41. {'"' , "quot"}
  42. }};
  43. #else
  44. static std::map<char,char const*> char_type;
  45. if( char_type.empty() ) {
  46. char_type['<'] = "lt";
  47. char_type['>'] = "gt";
  48. char_type['&'] = "amp";
  49. char_type['\'']= "apos";
  50. char_type['"'] = "quot";
  51. }
  52. #endif
  53. BOOST_TEST_FOREACH( char, c, value ) {
  54. std::map<char,char const*>::const_iterator found_ref = char_type.find( c );
  55. if( found_ref != char_type.end() )
  56. where_to << '&' << found_ref->second << ';';
  57. else
  58. where_to << c;
  59. }
  60. }
  61. //____________________________________________________________________________//
  62. inline void
  63. print_escaped( std::ostream& where_to, std::string const& value )
  64. {
  65. print_escaped( where_to, const_string( value ) );
  66. }
  67. //____________________________________________________________________________//
  68. template<typename T>
  69. inline void
  70. print_escaped( std::ostream& where_to, T const& value )
  71. {
  72. where_to << value;
  73. }
  74. //____________________________________________________________________________//
  75. inline void
  76. print_escaped_cdata( std::ostream& where_to, const_string value )
  77. {
  78. static const_string cdata_end( "]]>" );
  79. const_string::size_type pos = value.find( cdata_end );
  80. if( pos == const_string::npos )
  81. where_to << value;
  82. else {
  83. where_to << value.substr( 0, pos+2 ) << cdata_end
  84. << BOOST_TEST_L( "<![CDATA[" ) << value.substr( pos+2 );
  85. }
  86. }
  87. //____________________________________________________________________________//
  88. typedef custom_manip<struct attr_value_t> attr_value;
  89. template<typename T>
  90. inline std::ostream&
  91. operator<<( custom_printer<attr_value> const& p, T const& value )
  92. {
  93. *p << "=\"";
  94. print_escaped( *p, value );
  95. *p << '"';
  96. return *p;
  97. }
  98. //____________________________________________________________________________//
  99. typedef custom_manip<struct cdata_t> cdata;
  100. inline std::ostream&
  101. operator<<( custom_printer<cdata> const& p, const_string value )
  102. {
  103. *p << BOOST_TEST_L( "<![CDATA[" );
  104. print_escaped_cdata( *p, value );
  105. return *p << BOOST_TEST_L( "]]>" );
  106. }
  107. //____________________________________________________________________________//
  108. } // namespace utils
  109. } // namespace unit_test
  110. } // namespace boost
  111. #include <boost/test/detail/enable_warnings.hpp>
  112. #endif // BOOST_TEST_UTILS_XML_PRINTER_HPP