From 533b95f0e664a2ff56fce006d12760bedf17f626 Mon Sep 17 00:00:00 2001 From: Hermann Kraus Date: Sat, 21 Jan 2012 00:02:44 +0100 Subject: [PATCH] Enable XML parser to return multiple nodes. --- include/mapnik/ptree_helpers.hpp | 23 +++++++++++++++++------ src/libxml2_loader.cpp | 10 ++++++++-- src/load_map.cpp | 2 +- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/include/mapnik/ptree_helpers.hpp b/include/mapnik/ptree_helpers.hpp index c97ce4d55..c3790ebdf 100644 --- a/include/mapnik/ptree_helpers.hpp +++ b/include/mapnik/ptree_helpers.hpp @@ -258,7 +258,7 @@ T get(const boost::property_tree::ptree & node, const std::string & name, bool i } else { - str = node.get_optional(name ); + str = node.get_optional(name+"."); } if ( str ) { @@ -289,7 +289,7 @@ inline color get(boost::property_tree::ptree const& node, std::string const& nam } else { - str = node.get_optional(name ); + str = node.get_optional(name+"."); } if ( str ) @@ -322,7 +322,7 @@ T get(const boost::property_tree::ptree & node, const std::string & name, bool i } else { - str = node.get_optional(name); + str = node.get_optional(name+"."); } if ( ! str ) { @@ -348,7 +348,18 @@ T get_value(const boost::property_tree::ptree & node, const std::string & name) { try { - return node.get_value(); + /* NOTE: get_child works as long as there is only one child with that name. + If this function is used this used this condition must always be satisfied. + */ + return node.get_child("").get_value(); + } + catch (boost::property_tree::ptree_bad_path) + { + /* If the XML parser did not find any non-empty data element the is no + node. But we don't want to fail here but simply return a + default constructed value of the requested type. + */ + return T(); } catch (...) { @@ -369,7 +380,7 @@ boost::optional get_optional(const boost::property_tree::ptree & node, const } else { - str = node.get_optional(name); + str = node.get_optional(name+"."); } boost::optional result; @@ -403,7 +414,7 @@ inline boost::optional get_optional(const boost::property_tree::ptree & n } else { - str = node.get_optional(name); + str = node.get_optional(name+"."); } boost::optional result; diff --git a/src/libxml2_loader.cpp b/src/libxml2_loader.cpp index f935243ce..fedfbfd44 100644 --- a/src/libxml2_loader.cpp +++ b/src/libxml2_loader.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include #include @@ -200,8 +201,13 @@ private: } break; case XML_TEXT_NODE: - pt.put_value( (char*) cur_node->content ); - break; + { + std::string trimmed = boost::algorithm::trim_copy(std::string((char*)cur_node->content)); + if (trimmed.empty()) break; + ptree::iterator it = pt.push_back(ptree::value_type("", ptree())); + it->second.put_value(trimmed); + } + break; case XML_COMMENT_NODE: { ptree::iterator it = pt.push_back( diff --git a/src/load_map.cpp b/src/load_map.cpp index 423d2f80c..fffd85d19 100644 --- a/src/load_map.cpp +++ b/src/load_map.cpp @@ -652,7 +652,7 @@ void map_parser::parse_layer( Map & map, ptree const & lay ) if (child.first == "StyleName") { ensure_attrs(child.second, "StyleName", "none"); - std::string style_name = child.second.data(); + std::string style_name = get_value(child.second, "style name"); if (style_name.empty()) { std::ostringstream ss;