1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2025 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
#include <mapnik/xml_loader.hpp>
#include <mapnik/xml_node.hpp>
#include <mapnik/config_error.hpp>
#include <mapnik/util/trim.hpp>
#include <mapnik/util/noncopyable.hpp>
#include <mapnik/util/fs.hpp>
// libxml
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/parserInternals.h>
#include <libxml/xinclude.h>
// stl
#include <stdexcept>
#if LIBXML_VERSION >= 20900
#define DEFAULT_OPTIONS \
(XML_PARSE_NOERROR | XML_PARSE_NOENT | XML_PARSE_NOBLANKS | XML_PARSE_DTDLOAD | XML_PARSE_NOCDATA | \
XML_PARSE_HUGE | XML_PARSE_BIG_LINES)
#elif LIBXML_VERSION >= 20703
#define DEFAULT_OPTIONS \
(XML_PARSE_NOERROR | XML_PARSE_NOENT | XML_PARSE_NOBLANKS | XML_PARSE_DTDLOAD | XML_PARSE_NOCDATA | XML_PARSE_HUGE)
#else
#define DEFAULT_OPTIONS \
(XML_PARSE_NOERROR | XML_PARSE_NOENT | XML_PARSE_NOBLANKS | XML_PARSE_DTDLOAD | XML_PARSE_NOCDATA)
#endif
namespace mapnik {
class libxml2_loader : util::noncopyable
{
public:
libxml2_loader(char const* encoding = nullptr, int options = DEFAULT_OPTIONS, char const* url = nullptr)
: ctx_(0),
encoding_(encoding),
options_(options),
url_(url)
{
LIBXML_TEST_VERSION;
ctx_ = xmlNewParserCtxt();
if (!ctx_)
{
throw std::runtime_error("Failed to create parser context.");
}
}
~libxml2_loader()
{
if (ctx_)
{
xmlFreeParserCtxt(ctx_);
}
}
void load(std::string const& filename, xml_node& node)
{
if (!mapnik::util::exists(filename))
{
throw config_error(std::string("Could not load map file: File does not exist"), 0, filename);
}
xmlDocPtr doc = xmlCtxtReadFile(ctx_, filename.c_str(), encoding_, options_);
if (!doc)
{
xmlError const* error = xmlCtxtGetLastError(ctx_);
if (error)
{
std::string msg("XML document not well formed:\n");
msg += error->message;
// remove CR
msg = msg.substr(0, msg.size() - 1);
throw config_error(msg, error->line, error->file);
}
}
load(doc, node);
}
void load(int const fd, xml_node& node)
{
xmlDocPtr doc = xmlCtxtReadFd(ctx_, fd, url_, encoding_, options_);
load(doc, node);
}
void load_string(std::string const& buffer, xml_node& node, std::string const& base_path)
{
if (!base_path.empty())
{
if (!mapnik::util::exists(base_path))
{
throw config_error(std::string("Could not locate base_path '") + base_path +
"': file or directory does not exist");
}
}
// NOTE: base_path here helps libxml2 resolve entities correctly: https://github.com/mapnik/mapnik/issues/440
xmlDocPtr doc = xmlCtxtReadMemory(ctx_, buffer.data(), buffer.length(), base_path.c_str(), encoding_, options_);
load(doc, node);
}
void load(xmlDocPtr const doc, xml_node& node)
{
if (!doc)
{
std::string msg("XML document not well formed");
xmlError const* error = xmlCtxtGetLastError(ctx_);
if (error)
{
msg += ":\n";
msg += error->message;
throw config_error(msg, error->line, error->file);
}
else
{
throw config_error(msg);
}
}
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);
throw config_error("XML document is empty.");
}
populate_tree(root, node);
xmlFreeDoc(doc);
}
private:
void inline append_attributes(xmlAttr* attributes, xml_node& node)
{
for (; attributes; attributes = attributes->next)
{
node.add_attribute(reinterpret_cast<char const*>(attributes->name),
reinterpret_cast<char const*>(attributes->children->content));
}
}
void inline populate_tree(xmlNode* cur_node, xml_node& node)
{
for (; cur_node; cur_node = cur_node->next)
{
switch (cur_node->type)
{
case XML_ELEMENT_NODE: {
xml_node& new_node =
node.add_child(reinterpret_cast<char const*>(cur_node->name), cur_node->line, false);
append_attributes(cur_node->properties, new_node);
populate_tree(cur_node->children, new_node);
}
break;
case XML_TEXT_NODE: {
std::string trimmed(reinterpret_cast<char const*>(cur_node->content));
mapnik::util::trim(trimmed);
if (trimmed.empty())
break; // Don't add empty text nodes
node.add_child(trimmed.c_str(), cur_node->line, true);
}
break;
case XML_COMMENT_NODE:
break;
default:
break;
}
}
}
xmlParserCtxtPtr ctx_;
char const* encoding_;
int options_;
char const* url_;
};
void read_xml(std::string const& filename, xml_node& node)
{
libxml2_loader loader;
loader.load(filename, node);
}
void read_xml_string(std::string const& str, xml_node& node, std::string const& base_path)
{
libxml2_loader loader;
loader.load_string(str, node, base_path);
}
} // end of namespace mapnik
#endif
|