From 928e016d596ff433b8b4a158dbd1c298293e3263 Mon Sep 17 00:00:00 2001 From: e-driver Date: Tue, 25 Oct 2016 13:03:57 +0200 Subject: [PATCH 1/2] Enhanced xml pretty printing with nodes having attributes only. Each attribute is printed in a separate, indented line now. This makes the xml better readable, especially if there are many attributes in the node. Signed-off-by: e-driver --- .../property_tree/detail/xml_parser_write.hpp | 29 ++++++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/include/boost/property_tree/detail/xml_parser_write.hpp b/include/boost/property_tree/detail/xml_parser_write.hpp index 0af2265fa..3af508cde 100644 --- a/include/boost/property_tree/detail/xml_parser_write.hpp +++ b/include/boost/property_tree/detail/xml_parser_write.hpp @@ -113,12 +113,31 @@ namespace boost { namespace property_tree { namespace xml_parser // Write attributes if (optional attribs = pt.get_child_optional(xmlattr())) + { for (It it = attribs.get().begin(); it != attribs.get().end(); ++it) - stream << Ch(' ') << it->first << Ch('=') - << Ch('"') - << encode_char_entities( - it->second.template get_value()) - << Ch('"'); + { + if (want_pretty && has_attrs_only) + { + stream << Ch('\n'); + write_xml_indent(stream,indent+1,settings); + } + else + { + stream << Ch(' '); + } + + stream << it->first << Ch('=') + << Ch('"') + << encode_char_entities( + it->second.template get_value()) + << Ch('"'); + } + if (want_pretty && has_attrs_only) + { + stream << Ch('\n'); + write_xml_indent(stream,indent,settings); + } + } if ( has_attrs_only ) { From d01ed393c824fb1b270917cf6b91f0f339a0677e Mon Sep 17 00:00:00 2001 From: e-driver Date: Mon, 19 Dec 2016 18:25:53 +0100 Subject: [PATCH 2/2] xml writer settings extended for optional writing attributes in separate lines. --- include/boost/property_tree/detail/xml_parser_write.hpp | 5 +++-- .../property_tree/detail/xml_parser_writer_settings.hpp | 16 ++++++++++------ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/include/boost/property_tree/detail/xml_parser_write.hpp b/include/boost/property_tree/detail/xml_parser_write.hpp index 3af508cde..c8ddac384 100644 --- a/include/boost/property_tree/detail/xml_parser_write.hpp +++ b/include/boost/property_tree/detail/xml_parser_write.hpp @@ -114,9 +114,10 @@ namespace boost { namespace property_tree { namespace xml_parser // Write attributes if (optional attribs = pt.get_child_optional(xmlattr())) { + bool attrInSeparateLine = want_pretty && settings.attr_separate_line; for (It it = attribs.get().begin(); it != attribs.get().end(); ++it) { - if (want_pretty && has_attrs_only) + if (attrInSeparateLine) { stream << Ch('\n'); write_xml_indent(stream,indent+1,settings); @@ -132,7 +133,7 @@ namespace boost { namespace property_tree { namespace xml_parser it->second.template get_value()) << Ch('"'); } - if (want_pretty && has_attrs_only) + if (attrInSeparateLine) { stream << Ch('\n'); write_xml_indent(stream,indent,settings); diff --git a/include/boost/property_tree/detail/xml_parser_writer_settings.hpp b/include/boost/property_tree/detail/xml_parser_writer_settings.hpp index 5fb79f3e6..01a76b56f 100644 --- a/include/boost/property_tree/detail/xml_parser_writer_settings.hpp +++ b/include/boost/property_tree/detail/xml_parser_writer_settings.hpp @@ -16,12 +16,12 @@ namespace boost { namespace property_tree { namespace xml_parser { - + // Naively convert narrow string to another character type template Str widen(const char *text) { - typedef typename Str::value_type Ch; + typedef typename Str::value_type Ch; Str result; while (*text) { @@ -35,28 +35,32 @@ namespace boost { namespace property_tree { namespace xml_parser template class xml_writer_settings { - typedef typename Str::value_type Ch; + typedef typename Str::value_type Ch; public: xml_writer_settings(Ch inchar = Ch(' '), typename Str::size_type incount = 0, - const Str &enc = widen("utf-8")) + const Str &enc = widen("utf-8"), + bool attr_separate_line = false) : indent_char(inchar) , indent_count(incount) , encoding(enc) + , attr_separate_line(attr_separate_line) { } Ch indent_char; typename Str::size_type indent_count; Str encoding; + bool attr_separate_line; }; template xml_writer_settings xml_writer_make_settings(typename Str::value_type indent_char = (typename Str::value_type)(' '), typename Str::size_type indent_count = 0, - const Str &encoding = widen("utf-8")) + const Str &encoding = widen("utf-8"), + bool attr_separate_line = false) { - return xml_writer_settings(indent_char, indent_count, encoding); + return xml_writer_settings(indent_char, indent_count, encoding, attr_separate_line); } } } }