Add XML name registry.

Allows users to add new types to XML styles.
This commit is contained in:
Hermann Kraus 2012-02-17 19:45:13 +01:00
parent f06bda1c1c
commit 7c98b8c9c0
4 changed files with 119 additions and 12 deletions

View file

@ -0,0 +1,57 @@
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2012 Artem Pavlenko
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*****************************************************************************/
#ifndef FORMATTING_REGISTRY_HPP
#define FORMATTING_REGISTRY_HPP
// mapnik
#include <mapnik/utils.hpp>
#include <mapnik/formatting/base.hpp>
// boost
#include <boost/utility.hpp>
// stl
#include <string>
#include <map>
namespace mapnik
{
namespace formatting
{
typedef node_ptr (*from_xml_function_ptr)(boost::property_tree::ptree const& xml);
class registry : public singleton<registry, CreateStatic>,
private boost::noncopyable
{
public:
registry();
~registry() {}
void register_name(std::string name, from_xml_function_ptr ptr, bool overwrite=false);
node_ptr from_xml(std::string name, boost::property_tree::ptree const& xml);
private:
std::map<std::string, from_xml_function_ptr> map_;
};
} //ns formatting
} //ns mapnik
#endif // FORMATTING_REGISTRY_HPP

View file

@ -170,6 +170,7 @@ source = Split(
formatting/list.cpp
formatting/text.cpp
formatting/format.cpp
formatting/registry.cpp
"""
)

View file

@ -22,9 +22,7 @@
// mapnik
#include <mapnik/formatting/base.hpp>
#include <mapnik/formatting/list.hpp>
#include <mapnik/formatting/text.hpp>
#include <mapnik/formatting/expression.hpp>
#include <mapnik/formatting/format.hpp>
#include <mapnik/formatting/registry.hpp>
// boost
#include <boost/property_tree/ptree.hpp>
@ -47,16 +45,11 @@ node_ptr node::from_xml(boost::property_tree::ptree const& xml)
boost::property_tree::ptree::const_iterator itr = xml.begin();
boost::property_tree::ptree::const_iterator end = xml.end();
for (; itr != end; ++itr) {
node_ptr n;
if (itr->first == "<xmltext>") {
n = text_node::from_xml(itr->second);
} else if (itr->first == "Format") {
n = format_node::from_xml(itr->second);
} else if (itr->first == "ExpressionFormat") {
n = expression_format::from_xml(itr->second);
} else if (itr->first != "<xmlcomment>" && itr->first != "<xmlattr>" && itr->first != "Placement") {
throw config_error("Unknown item " + itr->first);
if (itr->first == "<xmlcomment>" || itr->first == "<xmlattr>" || itr->first == "Placement")
{
continue;
}
node_ptr n = registry::instance()->from_xml(itr->first, itr->second);
if (n) list->push_back(n);
}
if (list->get_children().size() == 1) {

View file

@ -0,0 +1,56 @@
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2012 Artem Pavlenko
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*****************************************************************************/
// mapnik
#include <mapnik/formatting/registry.hpp>
#include <mapnik/formatting/text.hpp>
#include <mapnik/formatting/format.hpp>
#include <mapnik/formatting/expression.hpp>
namespace mapnik
{
namespace formatting
{
registry::registry()
{
register_name("<xmltext>", &text_node::from_xml);
register_name("Format", &format_node::from_xml);
register_name("ExpressionFormat", &expression_format::from_xml);
}
void registry::register_name(std::string name, from_xml_function_ptr ptr, bool overwrite)
{
if (overwrite) {
map_[name] = ptr;
} else {
map_.insert(make_pair(name, ptr));
}
}
node_ptr registry::from_xml(std::string name, const boost::property_tree::ptree &xml)
{
std::map<std::string, from_xml_function_ptr>::const_iterator itr = map_.find(name);
if (itr == map_.end()) throw config_error("Unknown element '" + name + "'");
return itr->second(xml);
}
} //ns formatting
} //ns mapnik