[gnote] Remove more libxml++: DomParser for the note tags.



commit eff5297981f51177c3994e7680aa61bfe6ee1b4f
Author: Hubert Figuiere <hub figuiere net>
Date:   Tue May 26 12:18:29 2009 -0400

    Remove more libxml++: DomParser for the note tags.
---
 src/Makefile.am       |    1 +
 src/actionmanager.cpp |    1 -
 src/note.cpp          |   67 ++++++++++++++++++++++++-------------------------
 src/note.hpp          |    8 ++---
 src/notetag.hpp       |    4 ---
 src/sharp/xml.cpp     |   67 +++++++++++++++++++++++++++++++++++++++++++++++++
 src/sharp/xml.hpp     |   46 +++++++++++++++++++++++++++++++++
 src/test/notetest.cpp |   38 ++++++++++++++++++++-------
 8 files changed, 178 insertions(+), 54 deletions(-)

diff --git a/src/Makefile.am b/src/Makefile.am
index 7de83b3..4c1d931 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -86,6 +86,7 @@ libgnote_a_SOURCES = \
 	sharp/string.hpp sharp/string.cpp \
 	sharp/uri.hpp sharp/uri.cpp \
 	sharp/uuid.hpp \
+	sharp/xml.hpp sharp/xml.cpp \
 	sharp/xmlconvert.hpp sharp/xmlconvert.cpp \
 	sharp/xmlreader.hpp sharp/xmlreader.cpp \
 	sharp/xmlresolver.hpp \
diff --git a/src/actionmanager.cpp b/src/actionmanager.cpp
index 1c43604..3160ed6 100644
--- a/src/actionmanager.cpp
+++ b/src/actionmanager.cpp
@@ -54,7 +54,6 @@
 #include <gtkmm/imagemenuitem.h>
 #include <gtkmm/image.h>
 #include <gtkmm/stock.h>
-#include <libxml++/libxml++.h>
 #include <libxml++/parsers/domparser.h>
 
 
diff --git a/src/note.cpp b/src/note.cpp
index 75af4a1..4595bf5 100644
--- a/src/note.cpp
+++ b/src/note.cpp
@@ -32,8 +32,7 @@
 #include <boost/filesystem/path.hpp>
 #include <boost/algorithm/string/find.hpp>
 
-#include <libxml++/parsers/domparser.h>
-#include <libxml++/nodes/textnode.h>
+#include <libxml/parser.h>
 
 #include <glibmm/i18n.h>
 #include <gtkmm/button.h>
@@ -50,6 +49,7 @@
 #include "sharp/files.hpp"
 #include "sharp/map.hpp"
 #include "sharp/string.hpp"
+#include "sharp/xml.hpp"
 #include "sharp/xmlconvert.hpp"
 #include "sharp/xmlreader.hpp"
 #include "sharp/xmlwriter.hpp"
@@ -809,13 +809,12 @@ namespace gnote {
     // were to throw an XmlException in the middle of processing,
     // a note could be damaged.  Therefore, we check for parseability
     // ahead of time, and throw early.
-    {
-      xmlpp::DomParser parser;
-      parser.parse_memory(foreignNoteXml);
-      if(!parser) {
-        throw sharp::Exception("invalid XML in foreignNoteXml");
-      }
+    xmlDocPtr doc = xmlParseDoc((const xmlChar *)foreignNoteXml.c_str());
+
+    if(!doc) {
+      throw sharp::Exception("invalid XML in foreignNoteXml");
     }
+    xmlFreeDoc(doc);
 
     sharp::XmlReader xml;
     xml.load_buffer(foreignNoteXml);
@@ -827,7 +826,7 @@ namespace gnote {
         iter != tag_list.end(); ++iter) {
       remove_tag(*iter);
     }
-    Glib::ustring name;
+    std::string name;
 
     while (xml.read()) {
       switch (xml.get_node_type()) {
@@ -852,17 +851,16 @@ namespace gnote {
             sharp::XmlConvert::to_date_time(xml.read_string ());
         }
         else if(name == "tags") {
-          xmlpp::DomParser parser;
-          parser.parse_memory(xml.read_outer_xml());
-          if(parser) {
-            const xmlpp::Document * doc2 = parser.get_document();
+          xmlDocPtr doc2 = xmlParseDoc((const xmlChar*)xml.read_outer_xml().c_str());
+          if(doc2) {
             std::list<std::string> tag_strings;
-            parse_tags (doc2->get_root_node(), tag_strings);
+            parse_tags (doc2->children, tag_strings);
             for(std::list<std::string>::const_iterator iter = tag_strings.begin();
                 iter != tag_strings.end(); ++iter) {
               Tag::Ptr tag = TagManager::obj().get_or_create_tag(*iter);
               add_tag(tag);
             }
+            xmlFreeDoc(doc2);
           }
           else {
             DBG_OUT("loading tag subtree failed");
@@ -886,22 +884,23 @@ namespace gnote {
   }
 
 
-  void Note::parse_tags(const xmlpp::Node *tagnodes, std::list<std::string> & tags)
+  void Note::parse_tags(const xmlNodePtr tagnodes, std::list<std::string> & tags)
   {
-    xmlpp::NodeSet nodes = tagnodes->find("//*");
-    for(xmlpp::NodeSet::const_iterator iter = nodes.begin();
+    sharp::XmlNodeSet nodes = sharp::xml_node_xpath_find(tagnodes, "//*");
+    
+    if(nodes.empty()) {
+      return;
+    }
+    for(sharp::XmlNodeSet::const_iterator iter = nodes.begin();
         iter != nodes.end(); ++iter) {
-      const xmlpp::Node * node = *iter;
-      if(node->get_name() != "tag") {
-        continue;
-      }
-      const xmlpp::Element * content = dynamic_cast<const xmlpp::Element*>(node);
-      if(content) {
-        const xmlpp::TextNode * textnode = content->get_child_text();
-        if(textnode) {
-          std::string tag = textnode->get_content();
-          DBG_OUT("found tag %s", tag.c_str());
-          tags.push_back(tag);
+
+      const xmlNodePtr node = *iter;
+      if(xmlStrEqual(node->name, (const xmlChar*)"tag") && (node->type == XML_ELEMENT_NODE)) {
+        xmlChar * content = xmlNodeGetContent(node);
+        if(content) {
+          DBG_OUT("found tag %s", content);
+          tags.push_back((const char*)content);
+          xmlFree(content);
         }
       }
     }
@@ -1090,7 +1089,7 @@ namespace gnote {
 
     sharp::XmlReader xml(read_file);
 
-    Glib::ustring name;
+    std::string name;
 
     while (xml.read ()) {
       switch (xml.get_node_type()) {
@@ -1136,17 +1135,17 @@ namespace gnote {
           note->y() = boost::lexical_cast<int>(xml.read_string());
         }
         else if(name == "tags") {
-          xmlpp::DomParser parser;
-          parser.parse_memory(xml.read_outer_xml());
-          if(parser) {
-            const xmlpp::Document * doc2 = parser.get_document();
+          xmlDocPtr doc2 = xmlParseDoc((const xmlChar*)xml.read_outer_xml().c_str());
+
+          if(doc2) {
             std::list<std::string> tag_strings;
-            Note::parse_tags(doc2->get_root_node(), tag_strings);
+            Note::parse_tags(doc2->children, tag_strings);
             for(std::list<std::string>::const_iterator iter = tag_strings.begin();
                 iter != tag_strings.end(); ++iter) {
               Tag::Ptr tag = TagManager::obj().get_or_create_tag(*iter);
               note->tags()[tag->normalized_name()] = tag;
             }
+            xmlFreeDoc(doc2);
           }
           else {
             DBG_OUT("loading tag subtree failed");
diff --git a/src/note.hpp b/src/note.hpp
index 3357cd0..ef11cc7 100644
--- a/src/note.hpp
+++ b/src/note.hpp
@@ -28,6 +28,8 @@
 #include <queue>
 #include <memory>
 
+#include <libxml/tree.h>
+
 #include <sigc++/signal.h>
 #include <gtkmm/textbuffer.h>
 
@@ -37,10 +39,6 @@
 #include "utils.hpp"
 #include "sharp/datetime.hpp"
 
-namespace xmlpp {
-  class Node;
-}
-
 namespace sharp {
   class XmlWriter;
 }
@@ -167,7 +165,7 @@ public:
   void set_xml_content(const std::string & xml);
   std::string get_complete_note_xml();
   void load_foreign_note_xml(const std::string & foreignNoteXml, ChangeType changeType);
-  static void parse_tags(const xmlpp::Node *tagnodes, std::list<std::string> & tags);
+  static void parse_tags(const xmlNodePtr tagnodes, std::list<std::string> & tags);
   std::string text_content();
   void set_text_content(const std::string & text);
   const NoteData & data() const;
diff --git a/src/notetag.hpp b/src/notetag.hpp
index 1eb6a27..5321c5c 100644
--- a/src/notetag.hpp
+++ b/src/notetag.hpp
@@ -38,10 +38,6 @@ namespace sharp {
   class XmlReader;
 }
 
-namespace xmlpp {
-  class TextReader;
-}
-
 namespace gnote {
   class NoteEditor;
 
diff --git a/src/sharp/xml.cpp b/src/sharp/xml.cpp
new file mode 100644
index 0000000..ee2d750
--- /dev/null
+++ b/src/sharp/xml.cpp
@@ -0,0 +1,67 @@
+/*
+ * gnote
+ *
+ * Copyright (C) 2009 Hubert Figuiere
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+
+
+
+#include <libxml/xpath.h>
+
+#include "sharp/xml.hpp"
+
+
+namespace sharp {
+
+  XmlNodeSet xml_node_xpath_find(const xmlNodePtr node, 
+                                 const char * xpath)
+  {
+    xmlXPathContext* ctxt = xmlXPathNewContext(node->doc);
+    ctxt->node = node;
+
+    xmlXPathObject* result = xmlXPathEval((const xmlChar*)xpath, ctxt);
+
+    XmlNodeSet nodes;
+
+    if(result && (result->type == XPATH_NODESET)) {
+      xmlNodeSetPtr nodeset = result->nodesetval;
+
+      if(nodeset)
+      {
+        nodes.reserve(nodeset->nodeNr);
+        for (int i = 0; i != nodeset->nodeNr; ++i) {
+          nodes.push_back(nodeset->nodeTab[i]);
+        }
+      }
+    }
+
+    if(result) {
+      xmlXPathFreeObject(result);
+    }
+    if(ctxt) {
+      xmlXPathFreeContext(ctxt);
+    }
+
+    return nodes;
+  }
+
+}
diff --git a/src/sharp/xml.hpp b/src/sharp/xml.hpp
new file mode 100644
index 0000000..d021c8f
--- /dev/null
+++ b/src/sharp/xml.hpp
@@ -0,0 +1,46 @@
+/*
+ * gnote
+ *
+ * Copyright (C) 2009 Hubert Figuiere
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+
+
+
+#ifndef __SHARP_XML_HPP_
+#define __SHARP_XML_HPP_
+
+#include <vector>
+
+#include <libxml/tree.h>
+
+namespace sharp {
+
+
+  typedef std::vector<xmlNodePtr> XmlNodeSet;
+
+  XmlNodeSet xml_node_xpath_find(const xmlNodePtr node,
+                                 const char * xpath);
+
+}
+
+
+#endif
diff --git a/src/test/notetest.cpp b/src/test/notetest.cpp
index 853f3f4..b44d1e9 100644
--- a/src/test/notetest.cpp
+++ b/src/test/notetest.cpp
@@ -1,3 +1,22 @@
+/*
+ * gnote
+ *
+ * Copyright (C) 2009 Hubert Figuiere
+ *
+ * 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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
 
 
 #include <string>
@@ -5,25 +24,24 @@
 
 #include <boost/test/minimal.hpp>
 
-#include <libxml++/parsers/domparser.h>
+#include <libxml/tree.h>
 
 #include "note.hpp"
 
 int test_main(int /*argc*/, char ** /*argv*/)
 {
-  xmlpp::DomParser parser;
 //  std::string markup = "<tags><tag>system:notebook:ggoiiii</tag><tag>system:template</tag></tags>";
   std::string markup = "<tags xmlns=\"http://beatniksoftware.com/tomboy\";><tag>system:notebook:ggoiiii</tag><tag>system:template</tag></tags>";
-  parser.parse_memory(markup);
-  BOOST_CHECK(parser);
-  if(parser) {
-    xmlpp::Document * doc2 = parser.get_document();
-    BOOST_CHECK(doc2);
-    xmlpp::Element * element = doc2->get_root_node();
-    element->remove_attribute("xmlns");
+
+  xmlDocPtr doc = xmlParseDoc((const xmlChar*)markup.c_str());
+  BOOST_CHECK(doc);
+
+  if(doc) {
     std::list<std::string> tags;
-    gnote::Note::parse_tags(element, tags);
+    gnote::Note::parse_tags(doc->children, tags);
     BOOST_CHECK(!tags.empty());
+
+    xmlFreeDoc(doc);
   }
 
   return 0;



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