[gxml] libxml-NamedNodeMap implemented Gee.AbstractMap



commit b019e45064f19a82627567ead33caaa1b913d373
Author: Daniel Espinosa <esodan gmail com>
Date:   Wed Apr 15 16:43:57 2015 -0500

    libxml-NamedNodeMap implemented Gee.AbstractMap
    
    * Moved implementation of Node to xNode instead of BackedNode

 gxml/libxml-BackedNode.vala   |   22 -------
 gxml/libxml-Element.vala      |    2 +-
 gxml/libxml-NamedNodeMap.vala |  136 ++++++++++++++++++++++++++++++++++++++++-
 gxml/libxml-Node.vala         |   17 +++++
 4 files changed, 152 insertions(+), 25 deletions(-)
---
diff --git a/gxml/libxml-BackedNode.vala b/gxml/libxml-BackedNode.vala
index f123f64..d60e5a9 100644
--- a/gxml/libxml-BackedNode.vala
+++ b/gxml/libxml-BackedNode.vala
@@ -38,7 +38,6 @@ namespace GXml {
        public class BackedNode : xNode {
                /** Private properties */
                internal Xml.Node *node;
-               protected NodeList _child_nodes;
 
                internal void set_xmlnode (Xml.Node *node, xDocument owner) {
                        this.node = node;
@@ -366,26 +365,5 @@ namespace GXml {
                        str = buffer->content ();
                        return str;
                }
-               // GXml.Node interface implementations
-               public Gee.BidirList<GXml.Namespace> namespaces
-               {
-                       get {
-                               return _namespace_definitions;
-                       }
-               }
-               /*
-               public Gee.BidirList<GXml.Node> childs
-               {
-                       owned get {
-                               return (Gee.BidirList) child_nodes;
-                       }
-               }
-               public Gee.Map<string,GXml.Node> attrs { get; }
-               public string name { get; construct set; }
-               public string @value { get; set; }
-               public GXml.NodeType type_node { get; construct set; }
-               public GXml.Document document { get; construct set; }
-               public GXml.Node copy ();
-               public string to_string ();*/
        }
 }
diff --git a/gxml/libxml-Element.vala b/gxml/libxml-Element.vala
index e205dfa..9286636 100644
--- a/gxml/libxml-Element.vala
+++ b/gxml/libxml-Element.vala
@@ -82,7 +82,7 @@ namespace GXml {
                }
 
                // Note that NamedNodeMap is 'live' so changes to the Node should be seen in an already 
obtained NamedNodeMap
-               private NamedAttrMap _attributes = null;
+               private new NamedAttrMap _attributes = null;
 
                /**
                 * Contains a { link GXml.NamedAttrMap} of
diff --git a/gxml/libxml-NamedNodeMap.vala b/gxml/libxml-NamedNodeMap.vala
index 32c8f51..8f6e6b2 100644
--- a/gxml/libxml-NamedNodeMap.vala
+++ b/gxml/libxml-NamedNodeMap.vala
@@ -47,7 +47,7 @@ namespace GXml {
         * 
         * A collection of { link NamedNodeMap} of type { link Attr} objects in a { link xElement}.
         */
-       public class NamedAttrMap : GLib.Object, NamedNodeMap<Attr?> {
+       public class NamedAttrMap : AbstractMap<string,xNode>, NamedNodeMap<Attr?> {
                private xElement elem;
 
                internal NamedAttrMap (xElement e) {
@@ -152,7 +152,7 @@ namespace GXml {
                        private set {
                        }
                }
-               
+
                public Gee.Collection<Attr> get_values ()
                {
                  var c = new Gee.ArrayList<Attr> ();
@@ -161,5 +161,137 @@ namespace GXml {
                  }
                  return c;
                }
+               // Gee.AbstractMap
+               public override void clear () {
+                       foreach (string key in keys) {
+                               remove_named_item (key);
+                       }
+               }
+               public override new xNode @get (string key) { return get_named_item (key); }
+               public override bool has (string key, xNode value) {
+                       var e = get_named_item (key);
+                       if (e == null) return false;
+                       if (!(value is BackedNode)) return false;
+                       if (((BackedNode) value).node == ((BackedNode) e).node) return true;
+                       if (((Attr) value).node == ((Attr) e).node) return true;
+                       return false;
+               }
+               public override bool has_key (string key) {
+                       if (get_named_item (key) == null) return false;
+                       return true;
+               }
+               public override Gee.MapIterator<string,xNode> map_iterator () { return new Iterator (this); }
+               public override new void @set (string key, xNode value) {
+                       var n = get_named_item (key);
+                       if (n == null) return;
+                       remove_named_item (key);
+                       set_named_item ((Attr) value);
+               }
+               public override bool unset (string key, out xNode value = null) {
+                       var n = get_named_item (key);
+                       if (n == null) return false;
+                       remove_named_item (key);
+                       return true;
+               }
+               public override Gee.Set<Gee.Map.Entry<string,xNode>> entries {
+                       owned get {
+                               var s = new HashSet<Gee.Map.Entry<string,xNode>> ();
+                               var iter = map_iterator ();
+                               while (iter.next ()) {
+                                       var v = iter.get_value ();
+                                       var e = new Entry (iter.get_key (), v);
+                                       s.add (e);
+                               }
+                               return s;
+                       }
+               }
+               public override Gee.Set<string> keys {
+                       owned get {
+                               var s = new HashSet<string> ();
+                               var iter = map_iterator ();
+                               while (iter.next ()) {
+                                       s.add (iter.get_key ());
+                               }
+                               return s;
+                       }
+               }
+               public override bool read_only { get { return false; } }
+               public override int size {
+                       get {
+                               var iter = map_iterator ();
+                               int i = 0;
+                               while (iter.next ()) i++;
+                               return i;
+                       }
+               }
+               public override Gee.Collection<xNode> values {
+                       owned get {
+                               var s = new ArrayList <xNode> ();
+                               var iter = map_iterator ();
+                               while (iter.next ()) {
+                                       s.add (iter.get_value ());
+                               }
+                               return s;
+                       }
+               }
+               public class Entry : Gee.Map.Entry<string,xNode> {
+                       public Entry (string k, xNode v) {
+                               _key = k;
+                               value = v;
+                       }
+                       private string _key;
+                       public override string key { get { return _key; } }
+                       public override bool read_only { get { return true; } }
+                       public override xNode value { get; set; }
+               }
+               // Map Iterator
+               private class Iterator : Object, Gee.MapIterator<string,xNode>
+               {
+                       NamedAttrMap m;
+                       Xml.Attr *cur= null;
+                       Xml.Attr *head = null;
+                       public Iterator (NamedAttrMap m) {
+                               this.m = m;
+                               head = m.elem.node->properties;
+                       }
+                       public string get_key () { return cur->name; }
+                       public xNode get_value () { return m.get_named_item (get_key ()); }
+                       public bool has_next ()
+                       {
+                               if (cur == null) {
+                                       if (head == null) return false;
+                                       return true;
+                               }
+                               if (cur->next == null) return false;
+                               return true;
+                       }
+                       public bool next () {
+                               if (cur == null) {
+                                       cur = head;
+                                       return true;
+                               }
+                               if (cur->next == null) return false;
+                               cur = cur->next;
+                               return true;
+                       }
+                       public void set_value (xNode value) {
+                               if (!(value is Attr)) return;
+                               var n = m.get_named_item (get_key ());
+                               if (n == null) return;
+                               m.remove_named_item (get_key ());
+                               m.set_named_item ((Attr) value);
+                       }
+                       public void unset () {
+                               m.remove_named_item (get_key ());
+                       }
+                       public bool mutable { get { return true; } }
+                       public bool read_only { get { return false; } }
+                       public bool valid {
+                               get {
+                                       if (cur == null) return false;
+                                       return true;
+                               }
+                       }
+               }
        }
 }
diff --git a/gxml/libxml-Node.vala b/gxml/libxml-Node.vala
index 68a0fb6..2804301 100644
--- a/gxml/libxml-Node.vala
+++ b/gxml/libxml-Node.vala
@@ -22,6 +22,8 @@
  *      Daniel Espinosa <esodan gmail com>
  */
 
+using Gee;
+
 namespace GXml {
        /* TODO: consider adding public signals for new/deleted children */
 
@@ -36,6 +38,9 @@ namespace GXml {
         * URL: [[http://www.w3.org/TR/DOM-Level-1/level-one-core.html#ID-1950641247]]
         */
        public abstract class xNode : GLib.Object {
+               protected NodeList _child_nodes;
+               protected Gee.Map<string,xNode> _attributes = new Gee.HashMap<string,xNode> ();
+               private NamespaceAttrNodeList _namespace_definitions = null;
                /* Constructors */
                internal xNode (NodeType type, xDocument owner) {
                        this.node_type = type;
@@ -504,5 +509,17 @@ namespace GXml {
                public virtual string to_string (bool format = false, int level = 0) {
                        return "xNode(%d:%s)".printf (this.node_type, this.node_name);
                }
+               
+               // GXml.Node interface implementations
+               public virtual Gee.BidirList<GXml.Namespace> namespaces { get { return 
_namespace_definitions; } }
+               public virtual Gee.BidirList<GXml.Node> childs { get { return (BidirList<GXml.Node>) 
child_nodes; } }
+               public virtual Gee.Map<string,GXml.Node> attrs { get { return (Map<string,GXml.Node>) 
_attributes; } }
+               /*
+               public string name { get; construct set; }
+               public string @value { get; set; }
+               public GXml.NodeType type_node { get; construct set; }
+               public GXml.Document document { get; construct set; }
+               public GXml.Node copy ();
+               public string to_string ();*/
        }
 }


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