2012-03-13 09:02:53 +01:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2014-11-20 15:25:50 +01:00
|
|
|
* Copyright (C) 2014 Artem Pavlenko
|
2012-03-13 09:02:53 +01:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBXML2
|
|
|
|
#error HAVE_LIBXML2 defined but compiling rapidxml_loader.cpp!
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// mapnik
|
2014-08-13 08:52:31 +02:00
|
|
|
#include <mapnik/config_error.hpp>
|
2014-08-25 23:50:58 +02:00
|
|
|
#include <mapnik/utils.hpp>
|
|
|
|
#include <mapnik/util/fs.hpp>
|
2012-03-13 09:02:53 +01:00
|
|
|
#include <mapnik/xml_loader.hpp>
|
|
|
|
#include <boost/property_tree/detail/xml_parser_read_rapidxml.hpp>
|
|
|
|
#include <mapnik/xml_node.hpp>
|
2012-12-07 23:06:13 +01:00
|
|
|
#include <mapnik/util/trim.hpp>
|
2012-12-17 03:19:52 +01:00
|
|
|
#include <mapnik/noncopyable.hpp>
|
2012-03-13 09:02:53 +01:00
|
|
|
|
|
|
|
// stl
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
namespace rapidxml = boost::property_tree::detail::rapidxml;
|
2014-10-22 05:23:17 +02:00
|
|
|
|
2012-03-13 09:02:53 +01:00
|
|
|
namespace mapnik
|
|
|
|
{
|
2012-12-17 03:19:52 +01:00
|
|
|
class rapidxml_loader : mapnik::noncopyable
|
2012-03-13 09:02:53 +01:00
|
|
|
{
|
|
|
|
public:
|
2014-10-22 05:23:17 +02:00
|
|
|
rapidxml_loader() :
|
|
|
|
filename_() {}
|
2012-03-13 09:02:53 +01:00
|
|
|
|
2014-10-22 05:23:17 +02:00
|
|
|
~rapidxml_loader() {}
|
2012-03-13 09:02:53 +01:00
|
|
|
|
2014-10-04 01:27:24 +02:00
|
|
|
void load(std::string const& filename, xml_node & node)
|
2012-03-13 09:02:53 +01:00
|
|
|
{
|
2014-08-25 23:50:58 +02:00
|
|
|
if (!mapnik::util::exists(filename))
|
|
|
|
{
|
|
|
|
throw config_error(std::string("Could not load map file: File does not exist"), 0, filename);
|
|
|
|
}
|
2012-03-13 09:02:53 +01:00
|
|
|
filename_ = filename;
|
2014-08-25 23:50:58 +02:00
|
|
|
#ifdef _WINDOWS
|
|
|
|
std::basic_ifstream<char> stream(mapnik::utf8_to_utf16(filename));
|
|
|
|
#else
|
2012-03-13 09:02:53 +01:00
|
|
|
std::basic_ifstream<char> stream(filename.c_str());
|
2014-08-25 23:50:58 +02:00
|
|
|
#endif
|
2012-03-13 09:02:53 +01:00
|
|
|
if (!stream)
|
|
|
|
{
|
|
|
|
throw config_error("Could not load map file", 0, filename);
|
|
|
|
}
|
|
|
|
stream.unsetf(std::ios::skipws);
|
|
|
|
std::vector<char> v(std::istreambuf_iterator<char>(stream.rdbuf()),
|
2012-03-13 15:56:11 +01:00
|
|
|
std::istreambuf_iterator<char>());
|
2012-03-13 09:02:53 +01:00
|
|
|
if (!stream.good())
|
|
|
|
{
|
|
|
|
throw config_error("Could not load map file", 0, filename_);
|
|
|
|
}
|
|
|
|
v.push_back(0); // zero-terminate
|
2014-08-20 22:46:00 +02:00
|
|
|
load_array(v, node);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void load_array(T && array, xml_node & node)
|
|
|
|
{
|
2012-03-13 09:02:53 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
// Parse using appropriate flags
|
2013-05-22 18:13:05 +02:00
|
|
|
// https://github.com/mapnik/mapnik/issues/1856
|
|
|
|
// const int f_tws = rapidxml::parse_normalize_whitespace;
|
2014-04-29 23:32:57 +02:00
|
|
|
const int f_tws = rapidxml::parse_trim_whitespace | rapidxml::parse_validate_closing_tags;
|
2012-03-13 09:02:53 +01:00
|
|
|
rapidxml::xml_document<> doc;
|
2014-08-20 22:46:00 +02:00
|
|
|
doc.parse<f_tws>(&array.front());
|
2012-03-13 09:02:53 +01:00
|
|
|
|
|
|
|
for (rapidxml::xml_node<char> *child = doc.first_node();
|
|
|
|
child; child = child->next_sibling())
|
|
|
|
{
|
|
|
|
populate_tree(child, node);
|
|
|
|
}
|
|
|
|
}
|
2013-03-23 01:58:33 +01:00
|
|
|
catch (rapidxml::parse_error const& e)
|
2012-03-13 09:02:53 +01:00
|
|
|
{
|
|
|
|
long line = static_cast<long>(
|
2014-08-20 22:46:00 +02:00
|
|
|
std::count(&array.front(), e.where<char>(), '\n') + 1);
|
2012-03-13 09:02:53 +01:00
|
|
|
throw config_error(e.what(), line, filename_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-04 01:27:24 +02:00
|
|
|
void load_string(std::string const& buffer, xml_node & node, std::string const & )
|
2012-03-13 09:02:53 +01:00
|
|
|
{
|
2014-08-20 22:53:25 +02:00
|
|
|
// Note: base_path ignored because its not relevant - only needed for xml2 to load entities (see libxml2_loader.cpp)
|
2014-08-20 22:46:00 +02:00
|
|
|
load_array(std::string(buffer), node);
|
2012-03-13 09:02:53 +01:00
|
|
|
}
|
|
|
|
private:
|
2014-10-04 01:27:24 +02:00
|
|
|
void populate_tree(rapidxml::xml_node<char> *cur_node, xml_node & node)
|
2012-03-13 09:02:53 +01:00
|
|
|
{
|
|
|
|
switch (cur_node->type())
|
|
|
|
{
|
|
|
|
case rapidxml::node_element:
|
|
|
|
{
|
2014-10-04 01:27:24 +02:00
|
|
|
xml_node & new_node = node.add_child(cur_node->name(), 0, false);
|
2012-03-13 09:02:53 +01:00
|
|
|
// Copy attributes
|
|
|
|
for (rapidxml::xml_attribute<char> *attr = cur_node->first_attribute();
|
|
|
|
attr; attr = attr->next_attribute())
|
|
|
|
{
|
|
|
|
new_node.add_attribute(attr->name(), attr->value());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy children
|
|
|
|
for (rapidxml::xml_node<char> *child = cur_node->first_node();
|
|
|
|
child; child = child->next_sibling())
|
|
|
|
{
|
|
|
|
populate_tree(child, new_node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Data nodes
|
|
|
|
case rapidxml::node_data:
|
|
|
|
case rapidxml::node_cdata:
|
|
|
|
{
|
2014-05-23 19:27:37 +02:00
|
|
|
if (cur_node->value_size() > 0) // Don't add empty text nodes
|
|
|
|
{
|
|
|
|
node.add_child(cur_node->value(), 0, true);
|
|
|
|
}
|
2012-03-13 09:02:53 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
std::string filename_;
|
|
|
|
};
|
|
|
|
|
2014-10-04 01:27:24 +02:00
|
|
|
void read_xml(std::string const& filename, xml_node & node)
|
2012-03-13 09:02:53 +01:00
|
|
|
{
|
|
|
|
rapidxml_loader loader;
|
|
|
|
loader.load(filename, node);
|
|
|
|
}
|
2014-10-04 01:27:24 +02:00
|
|
|
void read_xml_string(std::string const& str, xml_node & node, std::string const& base_path)
|
2012-03-13 09:02:53 +01:00
|
|
|
{
|
|
|
|
rapidxml_loader loader;
|
|
|
|
loader.load_string(str, node, base_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end of namespace mapnik
|