[gnome-games] utils: Add XmlDoc.get_contents()



commit 7f33ddc3ffc28323ad2189b9a0e26f61f42a52bf
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Tue Aug 30 14:42:17 2016 +0200

    utils: Add XmlDoc.get_contents()
    
    This will be used in the next commit to get the content of multiple
    nodes in a Gameinfo document.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=770192

 src/utils/xml-doc.vala |   24 ++++++++++++++++++++++++
 1 files changed, 24 insertions(+), 0 deletions(-)
---
diff --git a/src/utils/xml-doc.vala b/src/utils/xml-doc.vala
index 05d5bd4..bf191f1 100644
--- a/src/utils/xml-doc.vala
+++ b/src/utils/xml-doc.vala
@@ -1,6 +1,8 @@
 // This file is part of GNOME Games. License: GPLv3
 
 private class Games.XmlDoc: Object {
+       private delegate void NodeCallback (Xml.Node* node);
+
        private Xml.Doc* doc;
 
        public XmlDoc.from_data (uint8[] data) throws Error {
@@ -20,6 +22,13 @@ private class Games.XmlDoc: Object {
                return node->get_content ();
        }
 
+       public string[] get_contents (string xpath, Xml.Node* current_node = null) {
+               string[] contents = {};
+               foreach_node (xpath, current_node, (node) => contents += node->get_content () );
+
+               return contents;
+       }
+
        public int count_nodes (string xpath, Xml.Node* from_node = null) {
                var ctx = new Xml.XPath.Context (doc);
                if (from_node != null)
@@ -51,4 +60,19 @@ private class Games.XmlDoc: Object {
 
                return first_node;
        }
+
+       private void foreach_node (string xpath, Xml.Node* from_node, NodeCallback callback) {
+               var ctx = new Xml.XPath.Context (doc);
+               if (from_node != null)
+                       ctx.node = from_node;
+
+               Xml.XPath.Object* obj = ctx.eval_expression (xpath);
+               if (obj->nodesetval == null)
+                       return;
+
+               for (int i = 0; i < obj->nodesetval->length (); i++)
+                       callback (obj->nodesetval->item (i));
+
+               delete obj;
+       }
 }


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