libxml++ r194 - in trunk: . libxml++/parsers



Author: murrayc
Date: Fri Jan  9 09:43:24 2009
New Revision: 194
URL: http://svn.gnome.org/viewvc/libxml++?rev=194&view=rev

Log:
2009-01-09  Stef Walter  <stef-list memberwebs com>

* libxml++/parsers/textreader.[h|cc]: Add setup_exceptions(), setting 
the on_libxml_error() callback, and call it from the constructors.
check_for_exceptions(): Actually check some member variables and throw an 
exception if necessary.
This should fix bug #348006.
It breaks ABI because it adds member variables, but we decided that is 
OK because nobody could actually be using this class seriously before 
now because it had no error checking.

Modified:
   trunk/ChangeLog
   trunk/libxml++/parsers/textreader.cc
   trunk/libxml++/parsers/textreader.h

Modified: trunk/libxml++/parsers/textreader.cc
==============================================================================
--- trunk/libxml++/parsers/textreader.cc	(original)
+++ trunk/libxml++/parsers/textreader.cc	Fri Jan  9 09:43:24 2009
@@ -1,5 +1,7 @@
 #include <libxml++/parsers/textreader.h>
 #include <libxml++/exceptions/internal_error.h>
+#include <libxml++/exceptions/parse_error.h>
+#include <libxml++/exceptions/validity_error.h>
 
 #include <libxml/xmlreader.h>
 
@@ -24,7 +26,7 @@
     struct _xmlTextReader* cobj)
     : propertyreader(new PropertyReader(*this)), impl_( cobj )
 {
-	
+  setup_exceptions();
 }
 
 TextReader::TextReader(
@@ -40,6 +42,8 @@
     throw internal_error("Cannot instantiate underlying libxml2 structure");
     #endif //LIBXMLCPP_EXCEPTIONS_ENABLED
   }
+
+  setup_exceptions();
 }
 
 TextReader::TextReader(
@@ -52,6 +56,8 @@
     throw internal_error("Cannot instantiate underlying libxml2 structure");
     #endif //LIBXMLCPP_EXCEPTIONS_ENABLED
   }
+
+  setup_exceptions();
 }
 
 TextReader::~TextReader()
@@ -335,14 +341,49 @@
       xmlTextReaderIsValid(impl_));
 }
 
+void TextReader::setup_exceptions()
+{
+  xmlTextReaderErrorFunc func = NULL;
+  void* arg = NULL; 
+
+  // We respect any other error handlers already setup:
+  xmlTextReaderGetErrorHandler(impl_, &func, &arg);
+  if(!func)
+  {
+     func = (xmlTextReaderErrorFunc)&TextReader::on_libxml_error;
+     xmlTextReaderSetErrorHandler(impl_, func, this);
+  }
+}
+
+void TextReader::on_libxml_error(void* arg, const char* msg, int severity, void* locator)
+{
+  TextReader* ths = (TextReader*)arg;
+  ths->severity_ = severity;
+  ths->error_ = msg ? msg : "unknown parse error";
+}
+
+void TextReader::check_for_exceptions() const
+{
+  if( severity_ == 0 )
+    return;
+    
+  TextReader* ths = const_cast<TextReader*>(this);
 
+  int severity = severity_;
+  ths->severity_ = 0;
 
+  if( severity == XML_PARSER_SEVERITY_ERROR )
+    throw parse_error(error_);
+  else if( severity == XML_PARSER_SEVERITY_VALIDITY_ERROR )
+    throw validity_error(error_);
+}
 
 int TextReader::PropertyReader::Int(
     int value)
 {
   if(value == -1)
     owner_.check_for_exceptions();
+
   return value;
 }
 
@@ -390,9 +431,4 @@
   return (const char *)value;
 }
 
-void TextReader::check_for_exceptions() const
-{
-  //TODO: Shouldn't we do something here? murrayc.
-}
-
 } // namespace xmlpp

Modified: trunk/libxml++/parsers/textreader.h
==============================================================================
--- trunk/libxml++/parsers/textreader.h	(original)
+++ trunk/libxml++/parsers/textreader.h	Fri Jan  9 09:43:24 2009
@@ -199,10 +199,15 @@
     struct PropertyReader;
     friend struct PropertyReader;
 
-    std::auto_ptr<PropertyReader> propertyreader;
+    void setup_exceptions();
+    static void on_libxml_error(void * arg, const char *msg, int severity,
+                              void * locator);
     void check_for_exceptions() const;
 
+    std::auto_ptr<PropertyReader> propertyreader;
     _xmlTextReader* impl_;
+    int severity_;
+    Glib::ustring error_;
 };
 
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]