| 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 | |
| 6 | // See http://www.boost.org/libs/test for the library home page. |
| 7 | // |
| 8 | // File : $RCSfile$ |
| 9 | // |
| 10 | // Version : $Revision$ |
| 11 | // |
| 12 | // Description : common code used by any agent serving as OF_XML printer |
| 13 | // *************************************************************************** |
| 14 | |
| 15 | #ifndef BOOST_TEST_UTILS_XML_PRINTER_HPP |
| 16 | #define BOOST_TEST_UTILS_XML_PRINTER_HPP |
| 17 | |
| 18 | // Boost.Test |
| 19 | #include <boost/test/detail/global_typedef.hpp> |
| 20 | #include <boost/test/utils/basic_cstring/basic_cstring.hpp> |
| 21 | #include <boost/test/utils/custom_manip.hpp> |
| 22 | #include <boost/test/utils/foreach.hpp> |
| 23 | #include <boost/test/utils/basic_cstring/io.hpp> |
| 24 | |
| 25 | // Boost |
| 26 | #include <boost/config.hpp> |
| 27 | |
| 28 | // STL |
| 29 | #include <iostream> |
| 30 | #include <map> |
| 31 | |
| 32 | #include <boost/test/detail/suppress_warnings.hpp> |
| 33 | |
| 34 | //____________________________________________________________________________// |
| 35 | |
| 36 | namespace boost { |
| 37 | namespace unit_test { |
| 38 | namespace utils { |
| 39 | |
| 40 | // ************************************************************************** // |
| 41 | // ************** xml print helpers ************** // |
| 42 | // ************************************************************************** // |
| 43 | |
| 44 | inline void |
| 45 | print_escaped( std::ostream& where_to, const_string value ) |
| 46 | { |
| 47 | #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) && !defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX) |
| 48 | static std::map<char,char const*> const char_type{{ |
| 49 | {'<' , "lt" }, |
| 50 | {'>' , "gt" }, |
| 51 | {'&' , "amp" }, |
| 52 | {'\'', "apos" }, |
| 53 | {'"' , "quot" } |
| 54 | }}; |
| 55 | #else |
| 56 | static std::map<char,char const*> char_type; |
| 57 | |
| 58 | if( char_type.empty() ) { |
| 59 | char_type['<'] = "lt" ; |
| 60 | char_type['>'] = "gt" ; |
| 61 | char_type['&'] = "amp" ; |
| 62 | char_type['\'']= "apos" ; |
| 63 | char_type['"'] = "quot" ; |
| 64 | } |
| 65 | #endif |
| 66 | |
| 67 | BOOST_TEST_FOREACH( char, c, value ) { |
| 68 | std::map<char,char const*>::const_iterator found_ref = char_type.find( x: c ); |
| 69 | |
| 70 | if( found_ref != char_type.end() ) |
| 71 | where_to << '&' << found_ref->second << ';'; |
| 72 | else |
| 73 | where_to << c; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | //____________________________________________________________________________// |
| 78 | |
| 79 | inline void |
| 80 | print_escaped( std::ostream& where_to, std::string const& value ) |
| 81 | { |
| 82 | print_escaped( where_to, value: const_string( value ) ); |
| 83 | } |
| 84 | |
| 85 | //____________________________________________________________________________// |
| 86 | |
| 87 | template<typename T> |
| 88 | inline void |
| 89 | print_escaped( std::ostream& where_to, T const& value ) |
| 90 | { |
| 91 | where_to << value; |
| 92 | } |
| 93 | |
| 94 | //____________________________________________________________________________// |
| 95 | |
| 96 | inline void |
| 97 | print_escaped_cdata( std::ostream& where_to, const_string value ) |
| 98 | { |
| 99 | static const_string cdata_end( "]]>" ); |
| 100 | |
| 101 | const_string::size_type pos = value.find( str: cdata_end ); |
| 102 | if( pos == const_string::npos ) |
| 103 | where_to << value; |
| 104 | else { |
| 105 | where_to << value.substr( beg_index: 0, end_index: pos+2 ) << cdata_end |
| 106 | << BOOST_TEST_L( "<![CDATA[" ) << value.substr( beg_index: pos+2 ); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | //____________________________________________________________________________// |
| 111 | |
| 112 | typedef custom_manip<struct attr_value_t> attr_value; |
| 113 | |
| 114 | template<typename T> |
| 115 | inline std::ostream& |
| 116 | operator<<( custom_printer<attr_value> const& p, T const& value ) |
| 117 | { |
| 118 | *p << "=\"" ; |
| 119 | print_escaped( *p, value ); |
| 120 | *p << '"'; |
| 121 | |
| 122 | return *p; |
| 123 | } |
| 124 | |
| 125 | //____________________________________________________________________________// |
| 126 | |
| 127 | typedef custom_manip<struct cdata_t> cdata; |
| 128 | |
| 129 | inline std::ostream& |
| 130 | operator<<( custom_printer<cdata> const& p, const_string value ) |
| 131 | { |
| 132 | *p << BOOST_TEST_L( "<![CDATA[" ); |
| 133 | print_escaped_cdata( where_to&: *p, value ); |
| 134 | return *p << BOOST_TEST_L( "]]>" ); |
| 135 | } |
| 136 | |
| 137 | //____________________________________________________________________________// |
| 138 | |
| 139 | } // namespace utils |
| 140 | } // namespace unit_test |
| 141 | } // namespace boost |
| 142 | |
| 143 | #include <boost/test/detail/enable_warnings.hpp> |
| 144 | |
| 145 | #endif // BOOST_TEST_UTILS_XML_PRINTER_HPP |
| 146 | |