1 //  (C) Copyright Gennadiy Rozental 2004-2014.
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/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 namespace unit_test {
37 
38 // ************************************************************************** //
39 // **************               xml print helpers              ************** //
40 // ************************************************************************** //
41 
42 inline void
print_escaped(std::ostream & where_to,const_string value)43 print_escaped( std::ostream& where_to, const_string value )
44 {
45     static fixed_mapping<char,char const*> char_type(
46         '<' , "lt",
47         '>' , "gt",
48         '&' , "amp",
49         '\'', "apos" ,
50         '"' , "quot",
51 
52         0
53     );
54 
55     BOOST_TEST_FOREACH( char, c, value ) {
56         char const* ref = char_type[c];
57 
58         if( ref )
59             where_to << '&' << ref << ';';
60         else
61             where_to << c;
62     }
63 }
64 
65 //____________________________________________________________________________//
66 
67 inline void
print_escaped(std::ostream & where_to,std::string const & value)68 print_escaped( std::ostream& where_to, std::string const& value )
69 {
70     print_escaped( where_to, const_string( value ) );
71 }
72 
73 //____________________________________________________________________________//
74 
75 template<typename T>
76 inline void
print_escaped(std::ostream & where_to,T const & value)77 print_escaped( std::ostream& where_to, T const& value )
78 {
79     where_to << value;
80 }
81 
82 //____________________________________________________________________________//
83 
84 inline void
print_escaped_cdata(std::ostream & where_to,const_string value)85 print_escaped_cdata( std::ostream& where_to, const_string value )
86 {
87     static const_string cdata_end( "]]>" );
88 
89     const_string::size_type pos = value.find( cdata_end );
90     if( pos == const_string::npos )
91         where_to << value;
92     else {
93         where_to << value.substr( 0, pos+2 ) << cdata_end
94                  << BOOST_TEST_L( "<![CDATA[" ) << value.substr( pos+2 );
95     }
96 }
97 
98 //____________________________________________________________________________//
99 
100 typedef custom_manip<struct attr_value_t> attr_value;
101 
102 template<typename T>
103 inline std::ostream&
operator <<(custom_printer<attr_value> const & p,T const & value)104 operator<<( custom_printer<attr_value> const& p, T const& value )
105 {
106     *p << "=\"";
107     print_escaped( *p, value );
108     *p << '"';
109 
110     return *p;
111 }
112 
113 //____________________________________________________________________________//
114 
115 typedef custom_manip<struct cdata_t> cdata;
116 
117 inline std::ostream&
operator <<(custom_printer<cdata> const & p,const_string value)118 operator<<( custom_printer<cdata> const& p, const_string value )
119 {
120     *p << BOOST_TEST_L( "<![CDATA[" );
121     print_escaped_cdata( *p, value );
122     return  *p << BOOST_TEST_L( "]]>" );
123 }
124 
125 //____________________________________________________________________________//
126 
127 } // namespace unit_test
128 } // namespace boost
129 
130 #include <boost/test/detail/enable_warnings.hpp>
131 
132 #endif // BOOST_TEST_UTILS_XML_PRINTER_HPP
133