[gnote] Make XmlReader accept preparesed xmlDoc



commit 44f1c21d2360fe0a113ae7f542d5c8bcd86f5ccb
Author: Aurimas Černius <aurisc4 gmail com>
Date:   Tue Jul 31 15:10:25 2018 +0300

    Make XmlReader accept preparesed xmlDoc

 src/sharp/xmlreader.cpp           | 27 ++++++++++++++++++---
 src/sharp/xmlreader.hpp           |  6 +++--
 src/test/unit/xmlreaderutests.cpp | 51 ++++++++++++++++++++++++++++++++++++++-
 3 files changed, 78 insertions(+), 6 deletions(-)
---
diff --git a/src/sharp/xmlreader.cpp b/src/sharp/xmlreader.cpp
index 2a7c3a6c..c7514191 100644
--- a/src/sharp/xmlreader.cpp
+++ b/src/sharp/xmlreader.cpp
@@ -1,7 +1,7 @@
 /*
  * gnote
  *
- * Copyright (C) 2013,2016-2017 Aurimas Cernius
+ * Copyright (C) 2013,2016-2018 Aurimas Cernius
  * Copyright (C) 2009 Hubert Figuiere
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
@@ -51,14 +51,31 @@ namespace sharp {
   }
 
   XmlReader::XmlReader()
-    : m_reader(NULL)
+    : m_doc(NULL)
+    , m_reader(NULL)
     , m_error(false)
   {
     m_error = true;
   }
 
+  XmlReader::XmlReader(xmlDocPtr xml_doc)
+    : m_doc(xml_doc)
+    , m_reader(NULL)
+    , m_error(false)
+  {
+    if(xml_doc == NULL) {
+      m_error = true;
+    }
+    else {
+      m_reader = xmlReaderWalker(xml_doc);
+      m_error = (m_reader == NULL);
+      // do not setup error hanling, crashes if tried
+    }
+  }
+
   XmlReader::XmlReader(const Glib::ustring & filename)
-    : m_reader(NULL)
+    : m_doc(NULL)
+    , m_reader(NULL)
     , m_error(false)
   {
     m_reader = xmlNewTextReaderFilename(filename.c_str());
@@ -171,6 +188,10 @@ namespace sharp {
       xmlFreeTextReader(m_reader);
       m_reader = NULL;
     }
+    if(m_doc) {
+      xmlFreeDoc(m_doc);
+      m_doc = NULL;
+    }
     m_error = true;
   }
 
diff --git a/src/sharp/xmlreader.hpp b/src/sharp/xmlreader.hpp
index d4e6bd60..6e369861 100644
--- a/src/sharp/xmlreader.hpp
+++ b/src/sharp/xmlreader.hpp
@@ -1,7 +1,7 @@
 /*
  * gnote
  *
- * Copyright (C) 2016-2017 Aurimas Cernius
+ * Copyright (C) 2016-2018 Aurimas Cernius
  * Copyright (C) 2009 Hubert Figuiere
  * 
  * Permission is hereby granted, free of charge, to any person obtaining a
@@ -42,8 +42,9 @@ class XmlReader
 public:
   /** Create a XmlReader to read from a buffer */
   XmlReader();
+  explicit XmlReader(xmlDocPtr xml_doc);
   /** Create a XmlReader to read from a file */
-  XmlReader(const Glib::ustring & filename);
+  explicit XmlReader(const Glib::ustring & filename);
   ~XmlReader();
 
   /** load the buffer from the s 
@@ -75,6 +76,7 @@ private:
   void setup_error_handling();
   static void error_handler(void* arg, const char* msg, int severity, void* locator);
 
+  xmlDocPtr        m_doc;
   Glib::ustring    m_buffer;
   xmlTextReaderPtr m_reader;
   // error signaling
diff --git a/src/test/unit/xmlreaderutests.cpp b/src/test/unit/xmlreaderutests.cpp
index 183418d2..4068cf34 100644
--- a/src/test/unit/xmlreaderutests.cpp
+++ b/src/test/unit/xmlreaderutests.cpp
@@ -1,7 +1,7 @@
 /*
  * gnote
  *
- * Copyright (C) 2017 Aurimas Cernius
+ * Copyright (C) 2017-2018 Aurimas Cernius
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -74,6 +74,55 @@ SUITE(XmlReader)
     CHECK_EQUAL(XML_READER_TYPE_TEXT, xml.get_node_type());
   }
 
+  TEST(xmlDoc_test)
+  {
+    Glib::ustring content = "<note-content xmlns:link=\"http://beatniksoftware.com/tomboy/link\";>"
+                              "Start Here\n\n"
+                              "<bold>Welcome to Gnote!</bold>\n\n"
+                              "Use this \"Start Here\" note to begin organizing "
+                              "your ideas and thoughts.\n\n"
+                              "You can create new notes to hold your ideas by "
+                              "selecting the \"Create New Note\" item from the "
+                              "Gnote menu in your GNOME Panel. "
+                              "Your note will be saved automatically.\n\n"
+                              "Then organize the notes you create by linking "
+                              "related notes and ideas together!\n\n"
+                              "We've created a note called "
+                              "<link:internal>Using Links in Gnote</link:internal>. "
+                              "Notice how each time we type <link:internal>Using "
+                              "Links in Gnote</link:internal> it automatically "
+                              "gets underlined?  Click on the link to open the note."
+                            "</note-content>";
+
+    xmlDocPtr xml_doc = xmlReadMemory(content.c_str(), content.bytes(), "", "UTF-8", 0);
+    CHECK(NULL != xml_doc);
+    sharp::XmlReader xml(xml_doc);
+
+    CHECK(xml.read());
+    CHECK_EQUAL(XML_READER_TYPE_ELEMENT, xml.get_node_type());
+    CHECK_EQUAL("note-content", xml.get_name());
+    CHECK_EQUAL("http://beatniksoftware.com/tomboy/link";, xml.get_attribute("xmlns:link"));
+
+    CHECK_EQUAL("Start Here\n\n"
+                  "<bold>Welcome to Gnote!</bold>\n\n"
+                  "Use this \"Start Here\" note to begin organizing "
+                  "your ideas and thoughts.\n\n"
+                  "You can create new notes to hold your ideas by "
+                  "selecting the \"Create New Note\" item from the "
+                  "Gnote menu in your GNOME Panel. "
+                  "Your note will be saved automatically.\n\n"
+                  "Then organize the notes you create by linking "
+                  "related notes and ideas together!\n\n"
+                  "We've created a note called <link:internal "
+                  "xmlns:link=\"http://beatniksoftware.com/tomboy/link\";>Using Links in 
Gnote</link:internal>. "
+                  "Notice how each time we type <link:internal "
+                  "xmlns:link=\"http://beatniksoftware.com/tomboy/link\";>Using "
+                  "Links in Gnote</link:internal> it automatically "
+              "gets underlined?  Click on the link to open the note.", xml.read_inner_xml());
+    CHECK(xml.read());
+    CHECK_EQUAL(XML_READER_TYPE_TEXT, xml.get_node_type());
+  }
+
   TEST(unicode_test)
   {
     Glib::ustring content = "<note><title>ąčęėįšų</title><note-content>"


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