Skip to content

Commit 61b5ae8

Browse files
committed
TextReader: Use xmlTextReaderSetStructuredErrorHandler()
instead of xmlTextReaderSetErrorHandler(). Use callback function with C linkage. The callback in textreader.cc was overlooked in commit f7bc8f2.
1 parent 1030ace commit 61b5ae8

File tree

2 files changed

+45
-10
lines changed

2 files changed

+45
-10
lines changed

libxml++/parsers/textreader.cc

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,43 @@
66

77
#include <libxml/xmlreader.h>
88

9+
namespace
10+
{
11+
//TODO: When we can break ABI, change on_libxml_error(), and change ErrorFuncType to
12+
// using ErrorFuncType = void (*)(void* userData, const xmlError* error);
13+
// C++ linkage
14+
using ErrorFuncType = void (*)(void* arg, const char* msg, int severity, void* locator);
15+
ErrorFuncType p_callback_error;
16+
17+
extern "C"
18+
{
19+
static void c_callback_error(void* userData, const xmlError* error)
20+
{
21+
const Glib::ustring msg = xmlpp::format_xml_error(error);
22+
23+
// Compute severity as in libxml2's xmlreader.c file,
24+
// static (i.e. private) function xmlTextReaderStructuredRelay().
25+
xmlParserSeverities severity{};
26+
switch (error->domain)
27+
{
28+
case XML_FROM_VALID:
29+
case XML_FROM_DTD:
30+
severity = (error->level == XML_ERR_WARNING) ?
31+
XML_PARSER_SEVERITY_VALIDITY_WARNING :
32+
XML_PARSER_SEVERITY_VALIDITY_ERROR;
33+
break;
34+
default:
35+
severity = (error->level == XML_ERR_WARNING) ?
36+
XML_PARSER_SEVERITY_WARNING :
37+
XML_PARSER_SEVERITY_ERROR;
38+
break;
39+
}
40+
p_callback_error(userData, msg.c_str(), severity, nullptr);
41+
}
42+
43+
} // extern "C"
44+
} // anonymous namespace
45+
946
namespace xmlpp
1047
{
1148

@@ -346,20 +383,16 @@ bool TextReader::is_valid() const
346383

347384
void TextReader::setup_exceptions()
348385
{
349-
xmlTextReaderErrorFunc func = nullptr;
350-
void* arg = nullptr;
351-
352-
// We respect any other error handlers already setup:
353-
xmlTextReaderGetErrorHandler(impl_, &func, &arg);
354-
if(!func)
355-
{
356-
func = (xmlTextReaderErrorFunc)&TextReader::on_libxml_error;
357-
xmlTextReaderSetErrorHandler(impl_, func, this);
358-
}
386+
p_callback_error = &on_libxml_error;
387+
xmlTextReaderSetStructuredErrorHandler(impl_, &c_callback_error, this);
359388
}
360389

361390
void TextReader::on_libxml_error(void* arg, const char* msg, int severity, void* /* locator */)
362391
{
392+
//TODO: Change this function when we can break ABI.
393+
// It was created when setup_exceptions() called xmlTextReaderSetErrorHandler()
394+
// instead of xmlTextReaderSetStructuredErrorHandler().
395+
363396
auto ths = static_cast<TextReader*>(arg);
364397
ths->severity_ = severity;
365398
ths->error_ = msg ? msg : "unknown parse error";

libxml++/parsers/textreader.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,8 @@ class TextReader: public NonCopyable
304304

305305
LIBXMLPP_API
306306
void setup_exceptions();
307+
//TODO: When we can break ABI, change on_libxml_error() to
308+
// static void on_libxml_error(void* userData, const xmlError* error);
307309
LIBXMLPP_API
308310
static void on_libxml_error(void * arg, const char *msg, int severity,
309311
void * locator);

0 commit comments

Comments
 (0)