mapnik/src/libxml2_loader.cpp

213 lines
6.1 KiB
C++
Raw Normal View History

/*****************************************************************************
2012-02-02 02:53:35 +01:00
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2011 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
*
*****************************************************************************/
#ifdef HAVE_LIBXML2
// 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>
#include <mapnik/util/trim.hpp>
2007-10-08 19:42:41 +02:00
// boost
#include <boost/utility.hpp>
#include <boost/filesystem/operations.hpp>
// libxml
2007-10-08 19:42:41 +02:00
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/parserInternals.h>
#include <libxml/xinclude.h>
#define DEFAULT_OPTIONS (XML_PARSE_NOERROR | XML_PARSE_NOENT | XML_PARSE_NOBLANKS | XML_PARSE_DTDLOAD | XML_PARSE_NOCDATA)
2012-02-02 02:53:35 +01:00
namespace mapnik
{
2010-06-02 13:03:30 +02:00
class libxml2_loader : boost::noncopyable
{
public:
libxml2_loader(const char *encoding = NULL, int options = DEFAULT_OPTIONS, const char *url = NULL) :
ctx_(0),
encoding_(encoding),
options_(options),
url_(url)
{
2010-06-02 13:03:30 +02:00
LIBXML_TEST_VERSION;
ctx_ = xmlNewParserCtxt();
if (!ctx_)
2010-06-02 13:03:30 +02:00
{
throw std::runtime_error("Failed to create parser context.");
}
}
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
}
void load(std::string const& filename, xml_node &node)
2010-06-02 13:03:30 +02:00
{
boost::filesystem::path path(filename);
if (!boost::filesystem::exists(path))
{
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_);
if (!doc)
2010-06-02 13:03:30 +02:00
{
xmlError * error = xmlCtxtGetLastError(ctx_);
2010-06-02 13:03:30 +02:00
if (error)
{
std::string msg("XML document not well formed:\n");
msg += error->message;
2012-02-02 02:53:35 +01:00
// remove CR
msg = msg.substr(0, msg.size() - 1);
2012-03-12 01:09:26 +01:00
throw config_error(msg, error->line, error->file);
}
2010-06-02 13:03:30 +02:00
}
load(doc, node);
2010-06-02 13:03:30 +02: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_);
load(doc, node);
2010-06-02 13:03:30 +02:00
}
void load_string(std::string const& buffer, xml_node &node, std::string const & base_path)
2010-06-02 13:03:30 +02:00
{
if (!base_path.empty())
2010-06-02 13:03:30 +02:00
{
boost::filesystem::path path(base_path);
if (!boost::filesystem::exists(path)) {
throw config_error(std::string("Could not locate base_path '") +
base_path + "': file or directory does not exist");
2012-02-02 02:53:35 +01:00
}
2010-06-02 13:03:30 +02:00
}
xmlDocPtr doc = xmlCtxtReadMemory(ctx_, buffer.data(), buffer.length(), base_path.c_str(), encoding_, options_);
2010-06-02 13:03:30 +02:00
load(doc, node);
2010-06-02 13:03:30 +02:00
}
void load(const xmlDocPtr doc, xml_node &node)
2010-06-02 13:03:30 +02:00
{
if (!doc)
2010-06-02 13:03:30 +02:00
{
std::string msg("XML document not well formed");
2010-06-02 13:03:30 +02:00
xmlError * error = xmlCtxtGetLastError( ctx_ );
if (error)
{
msg += ":\n";
msg += error->message;
throw config_error(msg, error->line, error->file);
}
else
{
throw config_error(msg);
}
2010-06-02 13:03:30 +02:00
}
int iXIncludeReturn = xmlXIncludeProcessFlags(doc, options_);
if (iXIncludeReturn < 0)
{
xmlFreeDoc(doc);
throw config_error("XML XInclude error. One or more files failed to load.");
}
xmlNode * root = xmlDocGetRootElement(doc);
if (!root) {
xmlFreeDoc(doc);
2010-06-02 13:03:30 +02:00
throw config_error("XML document is empty.");
}
populate_tree(root, node);
2010-06-02 13:03:30 +02:00
xmlFreeDoc(doc);
}
private:
void append_attributes(xmlAttr *attributes, xml_node &node)
2010-06-02 13:03:30 +02: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
}
}
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:
{
2012-04-06 22:05:59 +02:00
xml_node &new_node = node.add_child((const char *)cur_node->name, cur_node->line, false);
append_attributes(cur_node->properties, new_node);
populate_tree(cur_node->children, new_node);
}
2010-06-02 13:03:30 +02:00
break;
case XML_TEXT_NODE:
{
2012-04-06 22:05:59 +02:00
std::string trimmed((const char*)cur_node->content);
mapnik::util::trim(trimmed);
if (trimmed.empty()) break; //Don't add empty text nodes
node.add_child(trimmed, cur_node->line, true);
}
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;
}
2010-06-02 13:03:30 +02:00
}
}
2010-06-02 13:03:30 +02:00
xmlParserCtxtPtr ctx_;
const char *encoding_;
int options_;
const char *url_;
};
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;
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;
loader.load_string(str, node, base_path);
2010-06-02 13:03:30 +02:00
}
} // end of namespace mapnik
#endif