2003-02-03 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System.XML / System.Xml / XmlNamedNodeMap.cs
index 1a4b297f0d8a99192b423ab6d3364a456a4f8caf..756b30d18b6e0206f0675580e57f21a7de408658 100644 (file)
@@ -15,8 +15,9 @@ namespace System.Xml
 {
        public class XmlNamedNodeMap : IEnumerable
        {
-               private XmlNode parent;
-               private ArrayList nodeList;
+               XmlNode parent;
+               ArrayList nodeList;
+               bool readOnly = false;
 
                internal XmlNamedNodeMap (XmlNode parent)
                {
@@ -28,10 +29,9 @@ namespace System.Xml
                        get { return nodeList.Count; }
                }
 
-               [MonoTODO]
                public virtual IEnumerator GetEnumerator () 
                {
-                       throw new NotImplementedException ();
+                       return nodeList.GetEnumerator ();
                }
 
                public virtual XmlNode GetNamedItem (string name)
@@ -43,10 +43,15 @@ namespace System.Xml
                        return null;
                }
 
-               [MonoTODO]
                public virtual XmlNode GetNamedItem (string localName, string namespaceURI)
                {
-                       throw new NotImplementedException ();
+                       foreach (XmlNode node in nodeList) {
+                               if ((node.LocalName == localName)
+                                   && (node.NamespaceURI == namespaceURI))
+                                       return node;
+                       }
+
+                       return null;
                }
                
                public virtual XmlNode Item (int index)
@@ -57,23 +62,53 @@ namespace System.Xml
                                return (XmlNode) nodeList [index];
                }
 
-               [MonoTODO]
                public virtual XmlNode RemoveNamedItem (string name)
-               {
-                       throw new NotImplementedException ();
+               {                       
+                       foreach (XmlNode node in nodeList)
+                               if (node.Name == name) {
+                                       nodeList.Remove (node);
+                                       return node;
+                               }
+                       
+                       return null;
                }
 
-               [MonoTODO]
                public virtual XmlNode RemoveNamedItem (string localName, string namespaceURI)
                {
-                       throw new NotImplementedException ();
+                       foreach (XmlNode node in nodeList)
+                               if ((node.LocalName == localName)
+                                   && (node.NamespaceURI == namespaceURI)) {
+                                       nodeList.Remove (node);
+                                       return node;
+                               }
+
+                       return null;
                }
 
-               [MonoTODO]
                public virtual XmlNode SetNamedItem (XmlNode node)
                {
-                       nodeList.Add (node);
-                       return node;
+                       return SetNamedItem(node, -1);
                }
+
+               internal XmlNode SetNamedItem (XmlNode node, int pos)
+               {
+                       if (readOnly || (node.OwnerDocument != parent.OwnerDocument))
+                               throw new ArgumentException ("Cannot add to NodeMap.");
+
+                       foreach (XmlNode x in nodeList)
+                               if(x.LocalName == node.LocalName && x.NamespaceURI == node.NamespaceURI) {
+                                       nodeList.Remove (x);
+                                       nodeList.Add (node);
+                                       return x;
+                               }
+                       
+                       if(pos < 0)
+                               nodeList.Add (node);
+                       else
+                               nodeList.Insert(pos, node);
+                       return null;
+               }
+
+               internal ArrayList Nodes { get { return nodeList; } }
        }
 }