Imported Upstream version 2.91.1
[platform/upstream/libxml++.git] / libxml++ / validators / validator.cc
index 220d459..cd5d42d 100644 (file)
@@ -5,7 +5,6 @@
  * included with libxml++ as the file COPYING.
  */
 
-#include "libxml++/exceptions/wrapped_exception.h"
 #include "libxml++/validators/validator.h"
 
 #include <libxml/parser.h>
@@ -16,7 +15,7 @@
 namespace xmlpp {
 
 Validator::Validator()
-: valid_(nullptr), exception_(nullptr)
+: exception_ptr_(nullptr)
 {
 }
 
@@ -25,34 +24,15 @@ Validator::~Validator()
   release_underlying();
 }
 
-void Validator::initialize_valid()
+void Validator::initialize_context()
 {
-  // valid_ is used only by DtdValidator.
-  //TODO: When we can break ABI, move valid_ to DtdValidator.
-  if (valid_)
-  {
-    //Tell the validation context about the callbacks:
-    valid_->error = &callback_validity_error;
-    valid_->warning = &callback_validity_warning;
-
-    //Allow the callback_validity_*() methods to retrieve the C++ instance:
-    valid_->userData = this;
-  }
-
-  //Clear these temporary buffers too:
+  //Clear these temporary buffers:
   validate_error_.erase();
   validate_warning_.erase();
 }
 
 void Validator::release_underlying()
 {
-  if(valid_)
-  {
-    valid_->userData = nullptr; //Not really necessary.
-
-    xmlFreeValidCtxt(valid_);
-    valid_ = nullptr;
-  }
 }
 
 void Validator::on_validity_error(const Glib::ustring& message)
@@ -69,7 +49,20 @@ void Validator::on_validity_warning(const Glib::ustring& message)
 
 void Validator::check_for_validity_messages()
 {
-  Glib::ustring msg(exception_ ? exception_->what() : "");
+  Glib::ustring msg;
+  try
+  {
+    if (exception_ptr_)
+      std::rethrow_exception(exception_ptr_);
+  }
+  catch (const std::exception& e)
+  {
+    msg = e.what();
+  }
+  catch (...)
+  {
+    msg = "Unknown exception\n";
+  }
   bool validity_msg = false;
 
   if (!validate_error_.empty())
@@ -86,10 +79,14 @@ void Validator::check_for_validity_messages()
     validate_warning_.erase();
   }
 
-  if (validity_msg)
+  try
+  {
+    if (validity_msg)
+      throw validity_error(msg);
+  }
+  catch (...)
   {
-    delete exception_;
-    exception_ = new validity_error(msg);
+    exception_ptr_ = std::current_exception();
   }
 }
 
@@ -111,13 +108,9 @@ void Validator::callback_validity_error(void* valid_, const char* msg, ...)
     {
       validator->on_validity_error(Glib::ustring(buff));
     }
-    catch(const exception& e)
+    catch (...)
     {
-      validator->handleException(e);
-    }
-    catch(...)
-    {
-      validator->handleException(wrapped_exception(std::current_exception()));
+      validator->handle_exception();
     }
   }
 }
@@ -140,26 +133,21 @@ void Validator::callback_validity_warning(void* valid_, const char* msg, ...)
     {
       validator->on_validity_warning(Glib::ustring(buff));
     }
-    catch(const exception& e)
+    catch (...)
     {
-      validator->handleException(e);
-    }
-    catch(...)
-    {
-      validator->handleException(wrapped_exception(std::current_exception()));
+      validator->handle_exception();
     }
   }
 }
 
-void Validator::handleException(const exception& e)
+void Validator::handle_exception()
 {
-  delete exception_;
-  exception_ = e.Clone();
+  exception_ptr_ = std::current_exception();
 
   // Don't delete the DTD validation context or schema validation context
   // while validating. It would cause accesses to deallocated memory in libxml2
   // functions after the return from Validator::callback_validity_...().
-  // Parser::handleException() calls xmlStopParser(), but there is no
+  // Parser::handle_exception() calls xmlStopParser(), but there is no
   // xmlStopValidator() or similar function to call here.
   // We don't throw the exception here, since it would have to pass through
   // C functions. That's not guaranteed to work. It might work, but it depends
@@ -171,12 +159,12 @@ void Validator::handleException(const exception& e)
 void Validator::check_for_exception()
 {
   check_for_validity_messages();
-
-  if(exception_)
+  
+  if (exception_ptr_)
   {
-    std::unique_ptr<exception> tmp(exception_);
-    exception_ = nullptr;
-    tmp->Raise();
+    std::exception_ptr tmp(exception_ptr_);
+    exception_ptr_ = nullptr;
+    std::rethrow_exception(tmp);
   }
 }