2002-08-25 Tim Coleman <tim@timcoleman.com>
[mono.git] / mcs / class / System.XML / System.Xml / XmlNamedNodeMap.cs
index 97606e73f7b4b885edc17f925ac2a21994e9ec07..4a92046fd9353fe89c6f1ba7a2dc0dd7945fb982 100644 (file)
@@ -3,6 +3,7 @@
 //
 // Author:
 //   Jason Diamond (jason@injektilo.org)
+//   Duncan Mak  (duncan@ximian.com)
 //
 // (C) 2002 Jason Diamond  http://injektilo.org/
 //
@@ -14,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)
                {
@@ -23,59 +25,82 @@ namespace System.Xml
                        nodeList = new ArrayList ();
                }
 
-               [MonoTODO]
                public virtual int Count {
-                       get {
-                               throw new NotImplementedException ();
-                       }
+                       get { return nodeList.Count; }
                }
 
-               [MonoTODO]
                public virtual IEnumerator GetEnumerator () 
                {
-                       throw new NotImplementedException ();
+                       return nodeList.GetEnumerator ();
                }
 
-               [MonoTODO]
                public virtual XmlNode GetNamedItem (string name)
                {
                        foreach (XmlNode node in nodeList) {
                                if (node.Name == name)
                                        return node;
                        }
-
                        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;
+                       }
 
-               [MonoTODO]
+                       return null;
+               }
+               
                public virtual XmlNode Item (int index)
                {
-                       throw new NotImplementedException ();
+                       if (index < 0 || index > nodeList.Count)
+                               return null;
+                       else
+                               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)
                {
+                       if (readOnly || (node.OwnerDocument != parent.OwnerDocument))
+                               throw new ArgumentException ("Cannot add to NodeMap.");
+                                               
+                       foreach (XmlNode x in nodeList)
+                               if (x.Name == node.Name) {
+                                       nodeList.Remove (x);
+                                       nodeList.Add (node);
+                                       return x;
+                               }
+                       
                        nodeList.Add (node);
-                       return node;
+                       return null;
                }
+
+               internal ArrayList Nodes { get { return nodeList; } }
        }
 }