2003-04-25 Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
[mono.git] / mcs / class / System.XML / System.Xml / XmlNamedNodeMap.cs
index 97606e73f7b4b885edc17f925ac2a21994e9ec07..b588fb19edb987c390f096b974a0f3ac78ee5704 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,91 @@ 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) {
+                                       if (node.IsReadOnly)
+                                               throw new InvalidOperationException ("Cannot remove. This node is read only: " + 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; } }
        }
 }