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
6 // See http://www.boost.org/libs/test for the library home page.
7 //
8 // File : $RCSfile$
9 //
10 // Version : $Revision: 57992 $
11 //
12 // Description : common code used by any agent serving as XML printer
13 // ***************************************************************************
14
15 #ifndef BOOST_TEST_XML_PRINTER_HPP_071894GER
16 #define BOOST_TEST_XML_PRINTER_HPP_071894GER
17
18 // Boost.Test
19 #include <boost/test/utils/basic_cstring/basic_cstring.hpp>
20 #include <boost/test/utils/fixed_mapping.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
31 #include <boost/test/detail/suppress_warnings.hpp>
32
33 //____________________________________________________________________________//
34
35 namespace boost {
36
37 namespace unit_test {
38
39 // ************************************************************************** //
40 // ************** xml print helpers ************** //
41 // ************************************************************************** //
42
43 inline void
print_escaped(std::ostream & where_to,const_string value)44 print_escaped( std::ostream& where_to, const_string value )
45 {
46 static fixed_mapping<char,char const*> char_type(
47 '<' , "lt",
48 '>' , "gt",
49 '&' , "amp",
50 '\'', "apos" ,
51 '"' , "quot",
52
53 0
54 );
55
56 BOOST_TEST_FOREACH( char, c, value ) {
57 char const* ref = char_type[c];
58
59 if( ref )
60 where_to << '&' << ref << ';';
61 else
62 where_to << c;
63 }
64 }
65
66 //____________________________________________________________________________//
67
68 inline void
print_escaped(std::ostream & where_to,std::string const & value)69 print_escaped( std::ostream& where_to, std::string const& value )
70 {
71 print_escaped( where_to, const_string( value ) );
72 }
73
74 //____________________________________________________________________________//
75
76 template<typename T>
77 inline void
print_escaped(std::ostream & where_to,T const & value)78 print_escaped( std::ostream& where_to, T const& value )
79 {
80 where_to << value;
81 }
82
83 //____________________________________________________________________________//
84
85 typedef custom_manip<struct attr_value_t> attr_value;
86
87 template<typename T>
88 inline std::ostream&
operator <<(custom_printer<attr_value> const & p,T const & value)89 operator<<( custom_printer<attr_value> const& p, T const& value )
90 {
91 *p << "=\"";
92 print_escaped( *p, value );
93 *p << '"';
94
95 return *p;
96 }
97
98 //____________________________________________________________________________//
99
100 typedef custom_manip<struct cdata_t> cdata;
101
102 inline std::ostream&
operator <<(custom_printer<cdata> const & p,const_string value)103 operator<<( custom_printer<cdata> const& p, const_string value )
104 {
105 return *p << BOOST_TEST_L( "<![CDATA[" ) << value << BOOST_TEST_L( "]]>" );
106 }
107
108 //____________________________________________________________________________//
109
110 } // namespace unit_test
111
112 } // namespace boost
113
114 //____________________________________________________________________________//
115
116 #include <boost/test/detail/enable_warnings.hpp>
117
118 #endif // BOOST_TEST_XML_PRINTER_HPP_071894GER
119