|
6 | 6 |
|
7 | 7 | #include <libxml/xmlreader.h>
|
8 | 8 |
|
| 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 | + |
9 | 46 | namespace xmlpp
|
10 | 47 | {
|
11 | 48 |
|
@@ -346,20 +383,16 @@ bool TextReader::is_valid() const
|
346 | 383 |
|
347 | 384 | void TextReader::setup_exceptions()
|
348 | 385 | {
|
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); |
359 | 388 | }
|
360 | 389 |
|
361 | 390 | void TextReader::on_libxml_error(void* arg, const char* msg, int severity, void* /* locator */)
|
362 | 391 | {
|
| 392 | + //TODO: Change this function when we can break ABI. |
| 393 | + // It was created when setup_exceptions() called xmlTextReaderSetErrorHandler() |
| 394 | + // instead of xmlTextReaderSetStructuredErrorHandler(). |
| 395 | + |
363 | 396 | auto ths = static_cast<TextReader*>(arg);
|
364 | 397 | ths->severity_ = severity;
|
365 | 398 | ths->error_ = msg ? msg : "unknown parse error";
|
|
0 commit comments