Add XML name registry.
Allows users to add new types to XML styles.
This commit is contained in:
parent
f06bda1c1c
commit
7c98b8c9c0
4 changed files with 119 additions and 12 deletions
57
include/mapnik/formatting/registry.hpp
Normal file
57
include/mapnik/formatting/registry.hpp
Normal 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
|
|
@ -170,6 +170,7 @@ source = Split(
|
||||||
formatting/list.cpp
|
formatting/list.cpp
|
||||||
formatting/text.cpp
|
formatting/text.cpp
|
||||||
formatting/format.cpp
|
formatting/format.cpp
|
||||||
|
formatting/registry.cpp
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -22,9 +22,7 @@
|
||||||
// mapnik
|
// mapnik
|
||||||
#include <mapnik/formatting/base.hpp>
|
#include <mapnik/formatting/base.hpp>
|
||||||
#include <mapnik/formatting/list.hpp>
|
#include <mapnik/formatting/list.hpp>
|
||||||
#include <mapnik/formatting/text.hpp>
|
#include <mapnik/formatting/registry.hpp>
|
||||||
#include <mapnik/formatting/expression.hpp>
|
|
||||||
#include <mapnik/formatting/format.hpp>
|
|
||||||
|
|
||||||
// boost
|
// boost
|
||||||
#include <boost/property_tree/ptree.hpp>
|
#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 itr = xml.begin();
|
||||||
boost::property_tree::ptree::const_iterator end = xml.end();
|
boost::property_tree::ptree::const_iterator end = xml.end();
|
||||||
for (; itr != end; ++itr) {
|
for (; itr != end; ++itr) {
|
||||||
node_ptr n;
|
if (itr->first == "<xmlcomment>" || itr->first == "<xmlattr>" || itr->first == "Placement")
|
||||||
if (itr->first == "<xmltext>") {
|
{
|
||||||
n = text_node::from_xml(itr->second);
|
continue;
|
||||||
} 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);
|
|
||||||
}
|
}
|
||||||
|
node_ptr n = registry::instance()->from_xml(itr->first, itr->second);
|
||||||
if (n) list->push_back(n);
|
if (n) list->push_back(n);
|
||||||
}
|
}
|
||||||
if (list->get_children().size() == 1) {
|
if (list->get_children().size() == 1) {
|
||||||
|
|
56
src/formatting/registry.cpp
Normal file
56
src/formatting/registry.cpp
Normal 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
|
Loading…
Reference in a new issue