• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *          Copyright Andrey Semashev 2007 - 2015.
3  * Distributed under the Boost Software License, Version 1.0.
4  *    (See accompanying file LICENSE_1_0.txt or copy at
5  *          http://www.boost.org/LICENSE_1_0.txt)
6  */
7 /*!
8  * \file   formatters/xml_decorator.hpp
9  * \author Andrey Semashev
10  * \date   18.11.2012
11  *
12  * The header contains implementation of a XML-style character decorator.
13  */
14 
15 #ifndef BOOST_LOG_EXPRESSIONS_FORMATTERS_XML_DECORATOR_HPP_INCLUDED_
16 #define BOOST_LOG_EXPRESSIONS_FORMATTERS_XML_DECORATOR_HPP_INCLUDED_
17 
18 #include <boost/range/iterator_range_core.hpp>
19 #include <boost/log/detail/config.hpp>
20 #include <boost/log/expressions/formatters/char_decorator.hpp>
21 #include <boost/log/detail/header.hpp>
22 
23 #ifdef BOOST_HAS_PRAGMA_ONCE
24 #pragma once
25 #endif
26 
27 namespace boost {
28 
29 BOOST_LOG_OPEN_NAMESPACE
30 
31 namespace expressions {
32 
33 namespace aux {
34 
35 template< typename >
36 struct xml_decorator_traits;
37 
38 #ifdef BOOST_LOG_USE_CHAR
39 template< >
40 struct xml_decorator_traits< char >
41 {
get_patternsboost::expressions::aux::xml_decorator_traits42     static boost::iterator_range< const char* const* > get_patterns()
43     {
44         static const char* const patterns[] =
45         {
46             "&", "<", ">", "\"", "'"
47         };
48         return boost::make_iterator_range(patterns);
49     }
get_replacementsboost::expressions::aux::xml_decorator_traits50     static boost::iterator_range< const char* const* > get_replacements()
51     {
52         static const char* const replacements[] =
53         {
54             "&amp;", "&lt;", "&gt;", "&quot;", "&apos;"
55         };
56         return boost::make_iterator_range(replacements);
57     }
58 };
59 #endif // BOOST_LOG_USE_CHAR
60 
61 #ifdef BOOST_LOG_USE_WCHAR_T
62 template< >
63 struct xml_decorator_traits< wchar_t >
64 {
get_patternsboost::expressions::aux::xml_decorator_traits65     static boost::iterator_range< const wchar_t* const* > get_patterns()
66     {
67         static const wchar_t* const patterns[] =
68         {
69             L"&", L"<", L">", L"\"", L"'"
70         };
71         return boost::make_iterator_range(patterns);
72     }
get_replacementsboost::expressions::aux::xml_decorator_traits73     static boost::iterator_range< const wchar_t* const* > get_replacements()
74     {
75         static const wchar_t* const replacements[] =
76         {
77             L"&amp;", L"&lt;", L"&gt;", L"&quot;", L"&apos;"
78         };
79         return boost::make_iterator_range(replacements);
80     }
81 };
82 #endif // BOOST_LOG_USE_WCHAR_T
83 
84 template< typename CharT >
85 struct xml_decorator_gen
86 {
87     typedef CharT char_type;
88 
89     template< typename SubactorT >
operator []boost::expressions::aux::xml_decorator_gen90     BOOST_FORCEINLINE char_decorator_actor< SubactorT, pattern_replacer< char_type > > operator[] (SubactorT const& subactor) const
91     {
92         typedef xml_decorator_traits< char_type > traits_type;
93         typedef pattern_replacer< char_type > replacer_type;
94         typedef char_decorator_actor< SubactorT, replacer_type > result_type;
95         typedef typename result_type::terminal_type terminal_type;
96         typename result_type::base_type act = {{ terminal_type(subactor, replacer_type(traits_type::get_patterns(), traits_type::get_replacements())) }};
97         return result_type(act);
98     }
99 };
100 
101 } // namespace aux
102 
103 /*!
104  * XML-style decorator generator object. The decorator replaces characters that have special meaning
105  * in XML documents with the corresponding decorated counterparts. The generator provides
106  * <tt>operator[]</tt> that can be used to construct the actual decorator. For example:
107  *
108  * <code>
109  * xml_decor[ stream << attr< std::string >("MyAttr") ]
110  * </code>
111  *
112  * For wide-character formatting there is the similar \c wxml_decor decorator generator object.
113  */
114 #ifdef BOOST_LOG_USE_CHAR
115 BOOST_INLINE_VARIABLE const aux::xml_decorator_gen< char > xml_decor = {};
116 #endif
117 #ifdef BOOST_LOG_USE_WCHAR_T
118 BOOST_INLINE_VARIABLE const aux::xml_decorator_gen< wchar_t > wxml_decor = {};
119 #endif
120 
121 /*!
122  * The function creates an XML-style decorator generator for arbitrary character type.
123  */
124 template< typename CharT >
make_xml_decor()125 BOOST_FORCEINLINE aux::xml_decorator_gen< CharT > make_xml_decor()
126 {
127     return aux::xml_decorator_gen< CharT >();
128 }
129 
130 } // namespace expressions
131 
132 BOOST_LOG_CLOSE_NAMESPACE // namespace log
133 
134 } // namespace boost
135 
136 #include <boost/log/detail/footer.hpp>
137 
138 #endif // BOOST_LOG_EXPRESSIONS_FORMATTERS_XML_DECORATOR_HPP_INCLUDED_
139