xml_printer.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // (C) Copyright Gennadiy Rozental 2004-2008.
  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: 57992 $
  10. //
  11. // Description : common code used by any agent serving as XML printer
  12. // ***************************************************************************
  13. #ifndef BOOST_TEST_XML_PRINTER_HPP_071894GER
  14. #define BOOST_TEST_XML_PRINTER_HPP_071894GER
  15. // Boost.Test
  16. #include <boost/test/utils/basic_cstring/basic_cstring.hpp>
  17. #include <boost/test/utils/fixed_mapping.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 <boost/test/detail/suppress_warnings.hpp>
  26. //____________________________________________________________________________//
  27. namespace boost {
  28. namespace unit_test {
  29. // ************************************************************************** //
  30. // ************** xml print helpers ************** //
  31. // ************************************************************************** //
  32. inline void
  33. print_escaped( std::ostream& where_to, const_string value )
  34. {
  35. static fixed_mapping<char,char const*> char_type(
  36. '<' , "lt",
  37. '>' , "gt",
  38. '&' , "amp",
  39. '\'', "apos" ,
  40. '"' , "quot",
  41. 0
  42. );
  43. BOOST_TEST_FOREACH( char, c, value ) {
  44. char const* ref = char_type[c];
  45. if( ref )
  46. where_to << '&' << ref << ';';
  47. else
  48. where_to << c;
  49. }
  50. }
  51. //____________________________________________________________________________//
  52. inline void
  53. print_escaped( std::ostream& where_to, std::string const& value )
  54. {
  55. print_escaped( where_to, const_string( value ) );
  56. }
  57. //____________________________________________________________________________//
  58. template<typename T>
  59. inline void
  60. print_escaped( std::ostream& where_to, T const& value )
  61. {
  62. where_to << value;
  63. }
  64. //____________________________________________________________________________//
  65. typedef custom_manip<struct attr_value_t> attr_value;
  66. template<typename T>
  67. inline std::ostream&
  68. operator<<( custom_printer<attr_value> const& p, T const& value )
  69. {
  70. *p << "=\"";
  71. print_escaped( *p, value );
  72. *p << '"';
  73. return *p;
  74. }
  75. //____________________________________________________________________________//
  76. typedef custom_manip<struct cdata_t> cdata;
  77. inline std::ostream&
  78. operator<<( custom_printer<cdata> const& p, const_string value )
  79. {
  80. return *p << BOOST_TEST_L( "<![CDATA[" ) << value << BOOST_TEST_L( "]]>" );
  81. }
  82. //____________________________________________________________________________//
  83. } // namespace unit_test
  84. } // namespace boost
  85. //____________________________________________________________________________//
  86. #include <boost/test/detail/enable_warnings.hpp>
  87. #endif // BOOST_TEST_XML_PRINTER_HPP_071894GER