Imported Upstream version 2.91.2
[platform/upstream/libxml++.git] / libxml++ / parsers / parser.cc
index e45a905..503987a 100644 (file)
@@ -4,6 +4,7 @@
  * included with libxml++ as the file COPYING.
  */
 
+#include "libxml++/exceptions/wrapped_exception.h"
 #include "libxml++/parsers/parser.h"
 
 #include <libxml/parser.h>
@@ -34,7 +35,7 @@ struct Parser::Impl
 };
 
 Parser::Parser()
-: context_(nullptr), exception_ptr_(nullptr), pimpl_(new Impl)
+: context_(nullptr), exception_(nullptr), pimpl_(new Impl)
 {
 }
 
@@ -193,20 +194,7 @@ void Parser::on_validity_warning(const Glib::ustring& message)
 
 void Parser::check_for_error_and_warning_messages()
 {
-  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";
-  }
+  Glib::ustring msg(exception_ ? exception_->what() : "");
   bool parser_msg = false;
   bool validity_msg = false;
 
@@ -238,17 +226,10 @@ void Parser::check_for_error_and_warning_messages()
     pimpl_->validate_warning_.erase();
   }
 
-  try
-  {
-    if (validity_msg)
-      throw validity_error(msg);
-    else if (parser_msg)
-      throw parse_error(msg);
-  }
-  catch (...)
-  {
-    exception_ptr_ = std::current_exception();
-  }
+  if (validity_msg)
+    exception_.reset(new validity_error(msg));
+  else if (parser_msg)
+    exception_.reset(new parse_error(msg));
 }
   
 //static
@@ -308,10 +289,7 @@ void Parser::callback_error_or_warning(MsgType msg_type, void* ctx,
         // returns an error message (as it usually does).
 
         //Convert the ... to a string:
-        char buff[1024];
-
-        vsnprintf(buff, sizeof(buff)/sizeof(buff[0]), msg, var_args);
-        ubuff = buff;
+        ubuff = format_printf_message(msg, var_args);
       }
 
       try
@@ -342,7 +320,30 @@ void Parser::callback_error_or_warning(MsgType msg_type, void* ctx,
 
 void Parser::handle_exception()
 {
-  exception_ptr_ = std::current_exception();
+  try
+  {
+    throw; // Re-throw current exception
+  }
+  catch (const exception& e)
+  {
+    exception_.reset(e.clone());
+  }
+#ifdef LIBXMLXX_HAVE_EXCEPTION_PTR
+  catch (...)
+  {
+    exception_.reset(new wrapped_exception(std::current_exception()));
+  }
+#else
+  catch (const std::exception& e)
+  {
+    exception_.reset(new exception(e.what()));
+  }
+  catch (...)
+  {
+    exception_.reset(new exception("An exception was thrown that is not derived from std::exception or xmlpp::exception.\n"
+      "It could not be caught and rethrown because this platform does not support std::exception_ptr."));
+  }
+#endif
 
   if (context_)
     xmlStopParser(context_);
@@ -354,11 +355,10 @@ void Parser::check_for_exception()
 {
   check_for_error_and_warning_messages();
   
-  if (exception_ptr_)
+  if (exception_)
   {
-    std::exception_ptr tmp(exception_ptr_);
-    exception_ptr_ = nullptr;
-    std::rethrow_exception(tmp);
+    std::unique_ptr<exception> tmp(std::move(exception_));
+    tmp->raise();
   }
 }