1 // ----------------------------------------------------------------------------
2 // Copyright (C) 2002-2006 Marcin Kalicinski
3 // Copyright (C) 2013 Sebastian Redl
5 // Distributed under the Boost Software License, Version 1.0.
6 // (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 // For more information, see www.boost.org
10 // ----------------------------------------------------------------------------
11 #ifndef BOOST_PROPERTY_TREE_DETAIL_XML_PARSER_WRITE_HPP_INCLUDED
12 #define BOOST_PROPERTY_TREE_DETAIL_XML_PARSER_WRITE_HPP_INCLUDED
14 #include <boost/property_tree/ptree.hpp>
15 #include <boost/property_tree/detail/xml_parser_utils.hpp>
20 namespace boost { namespace property_tree { namespace xml_parser
23 void write_xml_indent(std::basic_ostream<typename Str::value_type> &stream,
25 const xml_writer_settings<Str> & settings
28 stream << std::basic_string<typename Str::value_type>(indent * settings.indent_count, settings.indent_char);
32 void write_xml_comment(std::basic_ostream<typename Str::value_type> &stream,
36 const xml_writer_settings<Str> & settings
39 typedef typename Str::value_type Ch;
41 write_xml_indent(stream,indent,settings);
42 stream << Ch('<') << Ch('!') << Ch('-') << Ch('-');
44 stream << Ch('-') << Ch('-') << Ch('>');
50 void write_xml_text(std::basic_ostream<typename Str::value_type> &stream,
54 const xml_writer_settings<Str> & settings
57 typedef typename Str::value_type Ch;
59 write_xml_indent(stream,indent,settings);
60 stream << encode_char_entities(s);
66 void write_xml_element(std::basic_ostream<typename Ptree::key_type::value_type> &stream,
67 const typename Ptree::key_type &key,
70 const xml_writer_settings<typename Ptree::key_type> & settings)
72 typedef typename Ptree::key_type::value_type Ch;
73 typedef typename Ptree::key_type Str;
74 typedef typename Ptree::const_iterator It;
76 bool want_pretty = settings.indent_count > 0;
77 // Find if elements present
78 bool has_elements = false;
79 bool has_attrs_only = pt.data().empty();
80 for (It it = pt.begin(), end = pt.end(); it != end; ++it)
82 if (it->first != xmlattr<Str>() )
84 has_attrs_only = false;
85 if (it->first != xmltext<Str>())
94 if (pt.data().empty() && pt.empty()) // Empty key
98 write_xml_indent(stream,indent,settings);
99 stream << Ch('<') << key <<
107 // Write opening tag, attributes and data
110 // Write opening brace and key
111 write_xml_indent(stream,indent,settings);
112 stream << Ch('<') << key;
115 if (optional<const Ptree &> attribs = pt.get_child_optional(xmlattr<Str>()))
116 for (It it = attribs.get().begin(); it != attribs.get().end(); ++it)
117 stream << Ch(' ') << it->first << Ch('=')
119 << encode_char_entities(
120 it->second.template get_value<Str>())
123 if ( has_attrs_only )
125 // Write closing brace
126 stream << Ch('/') << Ch('>');
132 // Write closing brace
135 // Break line if needed and if we want pretty-printing
136 if (has_elements && want_pretty)
141 // Write data text, if present
142 if (!pt.data().empty())
143 write_xml_text(stream,
144 pt.template get_value<Str>(),
145 indent + 1, has_elements && want_pretty, settings);
147 // Write elements, comments and texts
148 for (It it = pt.begin(); it != pt.end(); ++it)
150 if (it->first == xmlattr<Str>())
152 else if (it->first == xmlcomment<Str>())
153 write_xml_comment(stream,
154 it->second.template get_value<Str>(),
155 indent + 1, want_pretty, settings);
156 else if (it->first == xmltext<Str>())
157 write_xml_text(stream,
158 it->second.template get_value<Str>(),
159 indent + 1, has_elements && want_pretty, settings);
161 write_xml_element(stream, it->first, it->second,
162 indent + 1, settings);
166 if (indent >= 0 && !has_attrs_only)
169 write_xml_indent(stream,indent,settings);
170 stream << Ch('<') << Ch('/') << key << Ch('>');
178 template<class Ptree>
179 void write_xml_internal(std::basic_ostream<typename Ptree::key_type::value_type> &stream,
181 const std::string &filename,
182 const xml_writer_settings<typename Ptree::key_type> & settings)
184 typedef typename Ptree::key_type Str;
185 stream << detail::widen<Str>("<?xml version=\"1.0\" encoding=\"")
187 << detail::widen<Str>("\"?>\n");
188 write_xml_element(stream, Str(), pt, -1, settings);
190 BOOST_PROPERTY_TREE_THROW(xml_parser_error("write error", filename, 0));