2007-09-25 20:47:12 +02:00
|
|
|
/*****************************************************************************
|
2012-02-02 02:53:35 +01:00
|
|
|
*
|
2007-09-25 20:47:12 +02:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2011-10-23 15:04:25 +02:00
|
|
|
* Copyright (C) 2011 Artem Pavlenko
|
2007-09-25 20:47:12 +02: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
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2010-07-15 02:20:50 +02:00
|
|
|
#ifdef HAVE_LIBXML2
|
|
|
|
|
2012-03-05 16:49:54 +01:00
|
|
|
// mapnik
|
2012-03-13 09:02:53 +01:00
|
|
|
#include <mapnik/xml_loader.hpp>
|
2012-03-11 23:24:28 +01:00
|
|
|
#include <mapnik/xml_node.hpp>
|
2007-10-08 19:42:41 +02:00
|
|
|
#include <mapnik/config_error.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>
|
2013-06-03 04:28:24 +02:00
|
|
|
#include <mapnik/util/fs.hpp>
|
2007-09-25 20:47:12 +02:00
|
|
|
|
2012-03-05 16:49:54 +01:00
|
|
|
// libxml
|
2007-10-08 19:42:41 +02:00
|
|
|
#include <libxml/parser.h>
|
|
|
|
#include <libxml/tree.h>
|
|
|
|
#include <libxml/parserInternals.h>
|
2010-08-10 08:01:16 +02:00
|
|
|
#include <libxml/xinclude.h>
|
2007-09-25 20:47:12 +02:00
|
|
|
|
2013-06-03 04:28:24 +02:00
|
|
|
// stl
|
|
|
|
#include <stdexcept>
|
|
|
|
|
2009-11-12 00:37:57 +01:00
|
|
|
#define DEFAULT_OPTIONS (XML_PARSE_NOERROR | XML_PARSE_NOENT | XML_PARSE_NOBLANKS | XML_PARSE_DTDLOAD | XML_PARSE_NOCDATA)
|
2009-01-16 00:51:07 +01:00
|
|
|
|
2012-02-02 02:53:35 +01:00
|
|
|
namespace mapnik
|
2007-09-25 20:47:12 +02:00
|
|
|
{
|
2012-12-17 03:19:52 +01:00
|
|
|
class libxml2_loader : mapnik::noncopyable
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
libxml2_loader(const char *encoding = NULL, int options = DEFAULT_OPTIONS, const char *url = NULL) :
|
2012-03-05 16:49:54 +01:00
|
|
|
ctx_(0),
|
|
|
|
encoding_(encoding),
|
|
|
|
options_(options),
|
|
|
|
url_(url)
|
2007-09-25 20:47:12 +02:00
|
|
|
{
|
2010-06-02 13:03:30 +02:00
|
|
|
LIBXML_TEST_VERSION;
|
|
|
|
ctx_ = xmlNewParserCtxt();
|
2012-03-05 16:49:54 +01:00
|
|
|
if (!ctx_)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
throw std::runtime_error("Failed to create parser context.");
|
|
|
|
}
|
|
|
|
}
|
2007-09-25 20:47:12 +02:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
~libxml2_loader()
|
|
|
|
{
|
|
|
|
if (ctx_)
|
|
|
|
{
|
2012-02-02 02:53:35 +01:00
|
|
|
xmlFreeParserCtxt(ctx_);
|
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2007-09-25 20:47:12 +02:00
|
|
|
|
2012-03-13 11:11:28 +01:00
|
|
|
void load(std::string const& filename, xml_node &node)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2013-06-03 04:28:24 +02:00
|
|
|
if (!mapnik::util::exists(filename))
|
2012-03-05 16:49:54 +01:00
|
|
|
{
|
2012-12-07 23:06:13 +01:00
|
|
|
throw config_error(std::string("Could not load map file: File does not exist"), 0, filename);
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
xmlDocPtr doc = xmlCtxtReadFile(ctx_, filename.c_str(), encoding_, options_);
|
|
|
|
|
2012-03-05 16:49:54 +01:00
|
|
|
if (!doc)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2012-03-05 16:49:54 +01:00
|
|
|
xmlError * error = xmlCtxtGetLastError(ctx_);
|
2010-06-02 13:03:30 +02:00
|
|
|
if (error)
|
2007-09-25 20:47:12 +02:00
|
|
|
{
|
2012-12-07 05:15:27 +01:00
|
|
|
std::string msg("XML document not well formed:\n");
|
|
|
|
msg += error->message;
|
2012-02-02 02:53:35 +01:00
|
|
|
// remove CR
|
2012-12-07 05:15:27 +01:00
|
|
|
msg = msg.substr(0, msg.size() - 1);
|
2012-03-12 01:09:26 +01:00
|
|
|
throw config_error(msg, error->line, error->file);
|
2009-01-16 00:51:07 +01:00
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2012-03-05 16:49:54 +01:00
|
|
|
load(doc, node);
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
|
|
|
|
2012-03-05 16:49:54 +01:00
|
|
|
void load(const int fd, xml_node &node)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
xmlDocPtr doc = xmlCtxtReadFd(ctx_, fd, url_, encoding_, options_);
|
2012-03-05 16:49:54 +01:00
|
|
|
load(doc, node);
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
|
|
|
|
2012-03-13 11:11:28 +01:00
|
|
|
void load_string(std::string const& buffer, xml_node &node, std::string const & base_path)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2011-05-26 01:51:40 +02:00
|
|
|
if (!base_path.empty())
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2013-06-03 04:28:24 +02:00
|
|
|
if (!mapnik::util::exists(base_path)) {
|
2012-12-07 23:06:13 +01:00
|
|
|
throw config_error(std::string("Could not locate base_path '") +
|
2011-05-26 01:51:40 +02:00
|
|
|
base_path + "': file or directory does not exist");
|
2012-02-02 02:53:35 +01:00
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
|
|
|
|
2011-05-26 01:51:40 +02:00
|
|
|
xmlDocPtr doc = xmlCtxtReadMemory(ctx_, buffer.data(), buffer.length(), base_path.c_str(), encoding_, options_);
|
2010-06-02 13:03:30 +02:00
|
|
|
|
2012-03-05 16:49:54 +01:00
|
|
|
load(doc, node);
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2009-01-16 00:51:07 +01:00
|
|
|
|
2012-03-05 16:49:54 +01:00
|
|
|
void load(const xmlDocPtr doc, xml_node &node)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2012-03-05 16:49:54 +01:00
|
|
|
if (!doc)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2012-12-07 05:15:27 +01:00
|
|
|
std::string msg("XML document not well formed");
|
2010-06-02 13:03:30 +02:00
|
|
|
xmlError * error = xmlCtxtGetLastError( ctx_ );
|
|
|
|
if (error)
|
2009-01-16 00:51:07 +01:00
|
|
|
{
|
2012-12-07 05:15:27 +01:00
|
|
|
msg += ":\n";
|
|
|
|
msg += error->message;
|
|
|
|
throw config_error(msg, error->line, error->file);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw config_error(msg);
|
2009-01-16 00:51:07 +01:00
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
|
|
|
|
2012-03-05 16:49:54 +01:00
|
|
|
int iXIncludeReturn = xmlXIncludeProcessFlags(doc, options_);
|
2010-08-10 08:01:16 +02:00
|
|
|
|
|
|
|
if (iXIncludeReturn < 0)
|
|
|
|
{
|
|
|
|
xmlFreeDoc(doc);
|
|
|
|
throw config_error("XML XInclude error. One or more files failed to load.");
|
|
|
|
}
|
|
|
|
|
2012-03-05 16:49:54 +01:00
|
|
|
xmlNode * root = xmlDocGetRootElement(doc);
|
|
|
|
if (!root) {
|
2010-08-10 08:01:16 +02:00
|
|
|
xmlFreeDoc(doc);
|
2010-06-02 13:03:30 +02:00
|
|
|
throw config_error("XML document is empty.");
|
|
|
|
}
|
2009-01-16 00:51:07 +01:00
|
|
|
|
2012-03-05 16:49:54 +01:00
|
|
|
populate_tree(root, node);
|
2010-06-02 13:03:30 +02:00
|
|
|
xmlFreeDoc(doc);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2012-03-05 16:49:54 +01:00
|
|
|
void append_attributes(xmlAttr *attributes, xml_node &node)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2012-03-05 16:49:54 +01:00
|
|
|
for (; attributes; attributes = attributes->next )
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2012-04-06 22:05:59 +02:00
|
|
|
node.add_attribute((const char *)attributes->name, (const char *)attributes->children->content);
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-05 16:49:54 +01:00
|
|
|
void populate_tree(xmlNode *cur_node, xml_node &node)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
for (; cur_node; cur_node = cur_node->next )
|
|
|
|
{
|
2012-02-02 02:53:35 +01:00
|
|
|
switch (cur_node->type)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
case XML_ELEMENT_NODE:
|
2009-01-16 00:51:07 +01:00
|
|
|
{
|
2012-03-05 16:49:54 +01:00
|
|
|
|
2012-04-06 22:05:59 +02:00
|
|
|
xml_node &new_node = node.add_child((const char *)cur_node->name, cur_node->line, false);
|
2012-03-05 16:49:54 +01:00
|
|
|
append_attributes(cur_node->properties, new_node);
|
|
|
|
populate_tree(cur_node->children, new_node);
|
2007-09-25 20:47:12 +02:00
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
break;
|
|
|
|
case XML_TEXT_NODE:
|
2012-01-21 00:02:44 +01:00
|
|
|
{
|
2012-04-06 22:05:59 +02:00
|
|
|
std::string trimmed((const char*)cur_node->content);
|
2012-12-07 23:06:13 +01:00
|
|
|
mapnik::util::trim(trimmed);
|
2012-03-05 16:49:54 +01:00
|
|
|
if (trimmed.empty()) break; //Don't add empty text nodes
|
|
|
|
node.add_child(trimmed, cur_node->line, true);
|
2012-01-21 00:02:44 +01:00
|
|
|
}
|
|
|
|
break;
|
2010-06-02 13:03:30 +02:00
|
|
|
case XML_COMMENT_NODE:
|
2012-03-13 15:56:11 +01:00
|
|
|
break;
|
2010-06-02 13:03:30 +02:00
|
|
|
default:
|
|
|
|
break;
|
2007-09-25 20:47:12 +02:00
|
|
|
|
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
|
|
|
}
|
2007-09-25 20:47:12 +02:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
xmlParserCtxtPtr ctx_;
|
|
|
|
const char *encoding_;
|
|
|
|
int options_;
|
|
|
|
const char *url_;
|
|
|
|
};
|
2007-09-25 20:47:12 +02:00
|
|
|
|
2012-03-13 09:02:53 +01:00
|
|
|
void read_xml(std::string const & filename, xml_node &node)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
libxml2_loader loader;
|
2012-03-05 16:49:54 +01:00
|
|
|
loader.load(filename, node);
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2012-03-13 09:02:53 +01:00
|
|
|
void read_xml_string(std::string const & str, xml_node &node, std::string const & base_path)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
libxml2_loader loader;
|
2012-03-05 16:49:54 +01:00
|
|
|
loader.load_string(str, node, base_path);
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2007-09-25 20:47:12 +02:00
|
|
|
|
|
|
|
} // end of namespace mapnik
|
2010-07-15 02:20:50 +02:00
|
|
|
|
|
|
|
#endif
|