[gnome-games] utils: Add XmlDoc



commit 26f1fea3cd6285989ab546c8986d3eebc5c0de73
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Thu May 26 16:02:34 2016 +0200

    utils: Add XmlDoc
    
    This will be used in a subsequent commit to read the XML-based Gameinfo
    documents.

 src/Makefile.am        |    1 +
 src/utils/xml-doc.vala |   54 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+), 0 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 1b3379d..5faa163 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -130,6 +130,7 @@ gnome_games_SOURCES = \
        utils/grep.vala \
        utils/local-cover.vala \
        utils/string-input-stream.vala \
+       utils/xml-doc.vala \
        \
        config.vala \
        credits.vala \
diff --git a/src/utils/xml-doc.vala b/src/utils/xml-doc.vala
new file mode 100644
index 0000000..05d5bd4
--- /dev/null
+++ b/src/utils/xml-doc.vala
@@ -0,0 +1,54 @@
+// This file is part of GNOME Games. License: GPLv3
+
+private class Games.XmlDoc: Object {
+       private Xml.Doc* doc;
+
+       public XmlDoc.from_data (uint8[] data) throws Error {
+               doc = Xml.Parser.parse_memory ((string) data, data.length);
+       }
+
+       ~XmlDoc () {
+               if (doc != null)
+                       delete doc;
+       }
+
+       public string? get_content (string xpath, Xml.Node* current_node = null) {
+               Xml.Node* node = get_node (xpath, current_node);
+               if (node == null)
+                       return null;
+
+               return node->get_content ();
+       }
+
+       public int count_nodes (string xpath, Xml.Node* from_node = null) {
+               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 0;
+
+               var count = obj->nodesetval->length ();
+
+               delete obj;
+
+               return count;
+       }
+
+       private Xml.Node* get_node (string xpath, Xml.Node* from_node = null) {
+               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 null;
+
+               Xml.Node* first_node = obj->nodesetval->item (0);
+
+               delete obj;
+
+               return first_node;
+       }
+}


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