[gnote] Make XmlWrite throw exceptions on write failures



commit eaf20c1114cae62772d22ffeb830babed4e29566
Author: Aurimas Černius <aurisc4 gmail com>
Date:   Fri Mar 1 23:54:51 2013 +0200

    Make XmlWrite throw exceptions on write failures
    
    Fixes Bug 627317.

 po/POTFILES.in          |    1 +
 src/sharp/xmlwriter.cpp |  123 +++++++++++++++++++++++++++++++++++++++++++++++
 src/sharp/xmlwriter.hpp |   50 ++++---------------
 3 files changed, 134 insertions(+), 40 deletions(-)
---
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 4abda39..ae388fb 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -38,6 +38,7 @@ src/preferencesdialog.cpp
 src/recentchanges.cpp
 src/searchnoteswidget.cpp
 src/sharp/addinstreemodel.cpp
+src/sharp/xmlwriter.cpp
 src/synchronization/fusesyncserviceaddin.cpp
 src/synchronization/syncdialog.cpp
 src/synchronization/syncmanager.cpp
diff --git a/src/sharp/xmlwriter.cpp b/src/sharp/xmlwriter.cpp
index a2b564a..e587d85 100644
--- a/src/sharp/xmlwriter.cpp
+++ b/src/sharp/xmlwriter.cpp
@@ -1,6 +1,7 @@
 /*
  * gnote
  *
+ * Copyright (C) 2013 Aurimas Cernius
  * Copyright (C) 2009 Hubert Figuiere
  * 
  * Permission is hereby granted, free of charge, to any person obtaining a
@@ -23,11 +24,28 @@
  */
 
 
+#include <boost/format.hpp>
+#include <glibmm/i18n.h>
 #include <glibmm/ustring.h>
 
 #include "debug.hpp"
+#include "exception.hpp"
 #include "xmlwriter.hpp"
 
+
+namespace {
+
+  std::string make_write_failure_msg(const std::string & caller, const std::string & fail_func)
+  {
+    boost::format fmt(_("%1% failed"));
+    std::string msg = caller + ": ";
+    msg += str(fmt % fail_func);
+    return msg;
+  }
+
+}
+
+
 namespace sharp {
 
   XmlWriter::XmlWriter()
@@ -58,6 +76,111 @@ namespace sharp {
   }
 
 
+  int XmlWriter::write_start_document()
+  {
+    int res = xmlTextWriterStartDocument(m_writer, NULL, NULL, NULL);
+    if(res < 0) {
+      throw Exception(make_write_failure_msg(__FUNCTION__, "xmlTextWriterStartDocument"));
+    }
+
+    return res;
+  }
+
+
+  int XmlWriter::write_end_document()
+  {
+    int res = xmlTextWriterEndDocument(m_writer);
+    if(res < 0) {
+      throw Exception(make_write_failure_msg(__FUNCTION__, "xmlTextWriterEndDocument"));
+    }
+
+    return res;
+  }
+
+
+  int XmlWriter::write_start_element(const std::string & prefix,
+                                     const std::string & name, const std::string & nsuri)
+  {
+    int res = xmlTextWriterStartElementNS(m_writer, to_xmlchar(prefix), 
+                                          (const xmlChar*)name.c_str(), to_xmlchar(nsuri));
+    if(res < 0) {
+      throw Exception(make_write_failure_msg(__FUNCTION__, "xmlTextWriterStartElementNS"));
+    }
+
+    return res;
+  }
+
+
+  int XmlWriter::write_full_end_element()
+  {
+    // ???? what is the difference with write_end_element()
+    int res = xmlTextWriterEndElement(m_writer);
+    if(res < 0) {
+      throw Exception(make_write_failure_msg(__FUNCTION__, "xmlTextWriterEndElement"));
+    }
+
+    return res;
+  }
+
+
+  int XmlWriter::write_end_element()
+  {
+    int res = xmlTextWriterEndElement(m_writer);
+    if(res < 0) {
+      throw Exception(make_write_failure_msg(__FUNCTION__, "xmlTextWriterEndElement"));
+    }
+
+    return res;
+  }
+
+
+  int XmlWriter::write_start_attribute(const std::string & name)
+  {
+    int res = xmlTextWriterStartAttribute(m_writer, (const xmlChar*)name.c_str());
+    if(res < 0) {
+      throw Exception(make_write_failure_msg(__FUNCTION__, "xmlTextWriterStartAttribute"));
+    }
+
+    return res;
+  }
+
+
+  int XmlWriter::write_attribute_string(const std::string & prefix,const std::string & local_name,
+                                        const std::string & ns ,const std::string & value)
+  {
+    int res = xmlTextWriterWriteAttributeNS(m_writer, to_xmlchar(prefix), 
+                                            (const xmlChar*)local_name.c_str(),
+                                            to_xmlchar(ns), (const xmlChar*)value.c_str());
+    if(res < 0) {
+      throw Exception(make_write_failure_msg(__FUNCTION__, "xmlTextWriterWriteAttributeNS"));
+    }
+
+    return res;
+  }
+
+
+  int XmlWriter::write_end_attribute()
+  {
+    int res = xmlTextWriterEndAttribute(m_writer);
+    if(res < 0) {
+      throw Exception(make_write_failure_msg(__FUNCTION__, "xmlTextWriterEndAttribute"));
+    }
+
+    return res;
+  }
+
+
+  int XmlWriter::write_raw(const std::string & raw)
+  {
+    int res = xmlTextWriterWriteRaw(m_writer, (const xmlChar*)raw.c_str());
+    if(res < 0) {
+      throw Exception(make_write_failure_msg(__FUNCTION__, "xmlTextWriterWriteRaw"));
+    }
+
+    return res;
+  }
+
+
   int XmlWriter::write_char_entity(gunichar ch)
   {
     Glib::ustring unistring(1, (gunichar)ch);
diff --git a/src/sharp/xmlwriter.hpp b/src/sharp/xmlwriter.hpp
index 1e7952c..95f1ad2 100644
--- a/src/sharp/xmlwriter.hpp
+++ b/src/sharp/xmlwriter.hpp
@@ -1,6 +1,7 @@
 /*
  * gnote
  *
+ * Copyright (C) 2013 Aurimas Cernius
  * Copyright (C) 2009 Hubert Figuiere
  * 
  * Permission is hereby granted, free of charge, to any person obtaining a
@@ -53,47 +54,16 @@ namespace sharp {
     XmlWriter(const std::string & filename);
     XmlWriter(xmlDocPtr doc);
     ~XmlWriter();
-    int write_start_document()
-      {
-        return xmlTextWriterStartDocument(m_writer, NULL, NULL, NULL);
-      }
-    int write_end_document()
-      {
-        return xmlTextWriterEndDocument(m_writer);
-      }
-    int write_start_element(const std::string & prefix, const std::string & name, const std::string & nsuri)
-      {
-        return xmlTextWriterStartElementNS(m_writer, to_xmlchar(prefix), 
-                                           (const xmlChar*)name.c_str(), to_xmlchar(nsuri));
-      }
-    int write_full_end_element()
-      {
-        // ???? what is the difference with write_end_element()
-        return xmlTextWriterEndElement(m_writer);
-      }
-    int write_end_element()
-      {
-        return xmlTextWriterEndElement(m_writer);
-      }
-    int write_start_attribute(const std::string & name)
-      {
-        return xmlTextWriterStartAttribute(m_writer, (const xmlChar*)name.c_str());
-      }
+    int write_start_document();
+    int write_end_document();
+    int write_start_element(const std::string & prefix, const std::string & name, const std::string & nsuri);
+    int write_full_end_element();
+    int write_end_element();
+    int write_start_attribute(const std::string & name);
     int write_attribute_string(const std::string & prefix,const std::string & local_name,
-                               const std::string & ns ,const std::string & value)
-      {
-        return xmlTextWriterWriteAttributeNS(m_writer, to_xmlchar(prefix), 
-                                             (const xmlChar*)local_name.c_str(),
-                                             to_xmlchar(ns), (const xmlChar*)value.c_str());
-      }
-    int write_end_attribute()
-      {
-        return xmlTextWriterEndAttribute(m_writer);
-      }
-    int write_raw(const std::string & raw)
-      {
-        return xmlTextWriterWriteRaw(m_writer, (const xmlChar*)raw.c_str());
-      }
+                               const std::string & ns ,const std::string & value);
+    int write_end_attribute();
+    int write_raw(const std::string & raw);
     int write_char_entity(gunichar ch);
     int write_string(const std::string & );
 


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