[rygel/wip/ruih] core,ruih: Add XML node iterator and use it



commit f1114cd2aefc6de9672963a92664be0d027ee4d4
Author: Jens Georg <mail jensge org>
Date:   Sun Oct 19 14:38:16 2014 +0200

    core,ruih: Add XML node iterator and use it
    
    Signed-off-by: Jens Georg <mail jensge org>

 src/librygel-core/rygel-xml-utils.vala           |   33 ++++++++++++++++++++++
 src/librygel-ruih/rygel-ruih-icon-elem.vala      |    4 +-
 src/librygel-ruih/rygel-ruih-protocol-elem.vala  |    5 +--
 src/librygel-ruih/rygel-ruih-servicemanager.vala |   13 +++-----
 src/librygel-ruih/rygel-ruih-ui-elem.vala        |   12 ++-----
 5 files changed, 46 insertions(+), 21 deletions(-)
---
diff --git a/src/librygel-core/rygel-xml-utils.vala b/src/librygel-core/rygel-xml-utils.vala
index 5c33057..d62c435 100644
--- a/src/librygel-core/rygel-xml-utils.vala
+++ b/src/librygel-core/rygel-xml-utils.vala
@@ -48,4 +48,37 @@ public class Rygel.XMLUtils {
 
         return ret;
     }
+
+    /**
+     * Convenience class to iterate over Xml.Node's siblings in vala's foreach
+     * loop.
+     */
+    public class Iterator {
+        private Xml.Node* iter;
+
+        public Iterator (Xml.Node* node) {
+            this.iter = node;
+        }
+
+        public Iterator iterator() {
+            return this;
+        }
+
+        public bool next () {
+            return this.iter != null;
+        }
+
+        public Xml.Node* @get () {
+            var current = this.iter;
+            this.iter = this.iter->next;
+
+            return current;
+        }
+    }
+
+    public class ChildIterator : Iterator {
+        public ChildIterator (Xml.Node* node) {
+            base (node->children);
+        }
+    }
 }
diff --git a/src/librygel-ruih/rygel-ruih-icon-elem.vala b/src/librygel-ruih/rygel-ruih-icon-elem.vala
index d30b946..556fd4c 100644
--- a/src/librygel-ruih/rygel-ruih-icon-elem.vala
+++ b/src/librygel-ruih/rygel-ruih-icon-elem.vala
@@ -48,7 +48,7 @@ protected class IconElem : UIListing {
             throw new Rygel.RuihServiceError.OPERATION_REJECTED ("Unable to parse Icon data - null");
         }
         // Invalid XML Handling?
-        for (Xml.Node* child_node = node->children; child_node != null; child_node = child_node->next) {
+        foreach (var child_node in new Rygel.XMLUtils.ChildIterator (node)) {
             if (child_node->type == Xml.ElementType.TEXT_NODE) {
                 // ignore text nodes
                 continue;
@@ -107,4 +107,4 @@ protected class IconElem : UIListing {
         }
         return "";
     }
-}
\ No newline at end of file
+}
diff --git a/src/librygel-ruih/rygel-ruih-protocol-elem.vala b/src/librygel-ruih/rygel-ruih-protocol-elem.vala
index c5eed14..8de200c 100644
--- a/src/librygel-ruih/rygel-ruih-protocol-elem.vala
+++ b/src/librygel-ruih/rygel-ruih-protocol-elem.vala
@@ -55,8 +55,7 @@ protected class ProtocolElem : UIListing {
             }
         }
 
-        for (Xml.Node* child_node = node->children; child_node != null;
-            child_node = child_node->next) {
+        foreach (var child_node in new Rygel.XMLUtils.ChildIterator (node)) {
             if (child_node->type == Xml.ElementType.TEXT_NODE) {
                 // ignore text nodes
                 continue;
@@ -145,4 +144,4 @@ protected class ProtocolElem : UIListing {
         }
         return sb.str;
     }
-}
\ No newline at end of file
+}
diff --git a/src/librygel-ruih/rygel-ruih-servicemanager.vala 
b/src/librygel-ruih/rygel-ruih-servicemanager.vala
index 03ef01a..8c57c3e 100644
--- a/src/librygel-ruih/rygel-ruih-servicemanager.vala
+++ b/src/librygel-ruih/rygel-ruih-servicemanager.vala
@@ -120,14 +120,13 @@ public class Rygel.RuihServiceManager : Object
         var ui_list_node = doc->get_root_element ();
         if (ui_list_node != null && ui_list_node->name == UILIST)
         {
-            for (var child_node = ui_list_node->children;
-                 child_node != null;
-                 child_node = child_node->next) {
-                if (child_node->name == UI) {
-                    this.ui_list.add (new UIElem (child_node));
+            foreach (var node in new XMLUtils.ChildIterator (ui_list_node)) {
+                if (node->name == UI) {
+                    this.ui_list.add (new UIElem (node));
                 }
             }
         }
+
         delete doc;
     }
 
@@ -190,9 +189,7 @@ public class Rygel.RuihServiceManager : Object
             return;
         }
 
-        for (var child_node = node->children;
-             child_node != null;
-             child_node = child_node->next) {
+        foreach (var child_node in new XMLUtils.ChildIterator (node)) {
             if (child_node->type == Xml.ElementType.TEXT_NODE) {
                 // ignore text nodes
                 continue;
diff --git a/src/librygel-ruih/rygel-ruih-ui-elem.vala b/src/librygel-ruih/rygel-ruih-ui-elem.vala
index 3fbefca..112924e 100644
--- a/src/librygel-ruih/rygel-ruih-ui-elem.vala
+++ b/src/librygel-ruih/rygel-ruih-ui-elem.vala
@@ -52,9 +52,7 @@ protected class UIElem : UIListing
         this.protocols = new ArrayList<ProtocolElem> ();
 
         // invalid XML exception?
-        for (Xml.Node* child_node = node->children; child_node != null;
-             child_node = child_node->next)
-        {
+        foreach (var child_node in new Rygel.XMLUtils.ChildIterator (node)) {
             if (child_node->type == Xml.ElementType.TEXT_NODE) {
                 // ignore text nodes
                 continue;
@@ -71,11 +69,9 @@ protected class UIElem : UIListing
                 this.description = child_node->get_content ();
                 break;
             case ICONLIST:
-                for (Xml.Node* icon_node = child_node->children;
-                    icon_node != null; icon_node = icon_node->next)
-                {
-                    if (icon_node->name == ICON)
-                    {
+                var it = new Rygel.XMLUtils.ChildIterator (child_node);
+                foreach (var icon_node in it) {
+                    if (icon_node->name == ICON) {
                         this.icons.add (new IconElem (icon_node));
                     }
                 }


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