* Implemented ReadInnerXml and ReadOuterXml. We still need to validate for
[mono.git] / mcs / class / System.XML / System.Xml / XmlNode.cs
index 0773cdaa350aec2f9df606fe811668276e2e4b42..87fbc8aa2141ed642b2509944ac385a8a9fa5a73 100644 (file)
@@ -9,6 +9,7 @@
 
 using System;
 using System.Collections;
+using System.IO;
 using System.Xml.XPath;
 
 namespace System.Xml
@@ -24,7 +25,7 @@ namespace System.Xml
 
                #region Constructors
 
-               protected internal XmlNode(XmlDocument ownerDocument)
+               internal XmlNode (XmlDocument ownerDocument)
                {
                        this.ownerDocument = ownerDocument;
                }
@@ -46,7 +47,7 @@ namespace System.Xml
 
                public virtual XmlNodeList ChildNodes {
                        get {
-                               return new XmlNodeListChildren(LastLinkedChild);
+                               return new XmlNodeListChildren(this);
                        }
                }
 
@@ -71,27 +72,51 @@ namespace System.Xml
                        set { throw new NotImplementedException (); }
                }
 
-               [MonoTODO]
+               [MonoTODO("Setter.")]
                public virtual string InnerXml {
-                       get { throw new NotImplementedException (); }
+                       get {
+                               StringWriter sw = new StringWriter ();
+                               XmlTextWriter xtw = new XmlTextWriter (sw);
+
+                               WriteContentTo(xtw);
+
+                               return sw.GetStringBuilder().ToString();
+                       }
+
                        set { throw new NotImplementedException (); }
                }
 
-               [MonoTODO]
                public virtual bool IsReadOnly {
-                       get { throw new NotImplementedException (); }
+                       get { return false; }
                }
 
-               [MonoTODO]
                [System.Runtime.CompilerServices.IndexerName("Item")]
                public virtual XmlElement this [string name] {
-                       get { throw new NotImplementedException (); }
+                       get { 
+                               foreach (XmlNode node in ChildNodes) {
+                                       if ((node.NodeType == XmlNodeType.Element) &&
+                                           (node.Name == name)) {
+                                               return (XmlElement) node;
+                                       }
+                               }
+
+                               return null;
+                       }
                }
 
-               [MonoTODO]
                [System.Runtime.CompilerServices.IndexerName("Item")]
                public virtual XmlElement this [string localname, string ns] {
-                       get { throw new NotImplementedException (); }
+                       get { 
+                               foreach (XmlNode node in ChildNodes) {
+                                       if ((node.NodeType == XmlNodeType.Element) &&
+                                           (node.LocalName == localname) && 
+                                           (node.NamespaceURI == ns)) {
+                                               return (XmlElement) node;
+                                       }
+                               }
+
+                               return null;
+                       }
                }
 
                public virtual XmlNode LastChild {
@@ -103,10 +128,8 @@ namespace System.Xml
                        set { }
                }
 
-               [MonoTODO]
                public abstract string LocalName { get; }
 
-               [MonoTODO]
                public abstract string Name     { get; }
 
                [MonoTODO]
@@ -118,12 +141,17 @@ namespace System.Xml
                        get { return null; }
                }
 
-               [MonoTODO]
                public abstract XmlNodeType NodeType { get;     }
 
-               [MonoTODO]
                public virtual string OuterXml {
-                       get { throw new NotImplementedException (); }
+                       get {
+                               StringWriter sw = new StringWriter ();
+                               XmlTextWriter xtw = new XmlTextWriter (sw);
+
+                               WriteTo(xtw);
+
+                               return sw.GetStringBuilder().ToString();
+                       }
                }
 
                public virtual XmlDocument OwnerDocument {
@@ -144,10 +172,9 @@ namespace System.Xml
                        get { return null; }
                }
 
-               [MonoTODO]
                public virtual string Value {
-                       get { throw new NotImplementedException (); }
-                       set { throw new NotImplementedException (); }
+                       get { return null; }
+                       set { throw new InvalidOperationException ("This node does not have a value"); }
                }
 
                #endregion
@@ -156,15 +183,22 @@ namespace System.Xml
 
                public virtual XmlNode AppendChild (XmlNode newChild)
                {
-                       if (NodeType == XmlNodeType.Document || NodeType == XmlNodeType.Element || NodeType == XmlNodeType.Attribute) {
-                               XmlLinkedNode newLinkedChild = (XmlLinkedNode)newChild;
+                       if (NodeType == XmlNodeType.Document
+                           || NodeType == XmlNodeType.Element
+                           || NodeType == XmlNodeType.Attribute) {
+                               XmlLinkedNode newLinkedChild = (XmlLinkedNode) newChild;
                                XmlLinkedNode lastLinkedChild = LastLinkedChild;
+
+                               newLinkedChild.parentNode = this;
+                               
                                if (lastLinkedChild != null) {
                                        newLinkedChild.NextLinkedSibling = lastLinkedChild.NextLinkedSibling;
                                        lastLinkedChild.NextLinkedSibling = newLinkedChild;
                                } else
                                        newLinkedChild.NextLinkedSibling = newLinkedChild;
+                               
                                LastLinkedChild = newLinkedChild;
+
                                return newChild;
                        } else
                                throw new InvalidOperationException();
@@ -184,10 +218,9 @@ namespace System.Xml
                        throw new NotImplementedException ();
                }
 
-               [MonoTODO]
                public IEnumerator GetEnumerator ()
                {
-                       throw new NotImplementedException ();
+                       return new XmlNodeListChildren(this).GetEnumerator();
                }
 
                [MonoTODO]
@@ -241,10 +274,33 @@ namespace System.Xml
                        LastLinkedChild = null;
                }
 
-               [MonoTODO]
                public virtual XmlNode RemoveChild (XmlNode oldChild)
                {
-                       throw new NotImplementedException ();
+                       if (NodeType == XmlNodeType.Document || NodeType == XmlNodeType.Element || NodeType == XmlNodeType.Attribute) 
+                       {
+                               if (IsReadOnly)
+                                       throw new ArgumentException();
+
+                               if (Object.ReferenceEquals(LastLinkedChild, LastLinkedChild.NextLinkedSibling) && Object.ReferenceEquals(LastLinkedChild, oldChild))
+                                       LastLinkedChild = null;
+                               else {
+                                       XmlLinkedNode oldLinkedChild = (XmlLinkedNode)oldChild;
+                                       XmlLinkedNode beforeLinkedChild = LastLinkedChild;
+                                       
+                                       while (!Object.ReferenceEquals(beforeLinkedChild.NextLinkedSibling, LastLinkedChild) && !Object.ReferenceEquals(beforeLinkedChild.NextLinkedSibling, oldLinkedChild))
+                                               beforeLinkedChild = beforeLinkedChild.NextLinkedSibling;
+
+                                       if (!Object.ReferenceEquals(beforeLinkedChild.NextLinkedSibling, oldLinkedChild))
+                                               throw new ArgumentException();
+
+                                       beforeLinkedChild.NextLinkedSibling = oldLinkedChild.NextLinkedSibling;
+                                       oldLinkedChild.NextLinkedSibling = null;
+                                }
+
+                               return oldChild;
+                       } 
+                       else
+                               throw new ArgumentException();
                }
 
                [MonoTODO]
@@ -254,25 +310,25 @@ namespace System.Xml
                }
 
                [MonoTODO]
-               public virtual XmlNodeList SelectNodes (string xpath)
+               public XmlNodeList SelectNodes (string xpath)
                {
                        throw new NotImplementedException ();
                }
 
                [MonoTODO]
-               public virtual XmlNodeList SelectNodes (string xpath, XmlNamespaceManager nsmgr)
+               public XmlNodeList SelectNodes (string xpath, XmlNamespaceManager nsmgr)
                {
                        throw new NotImplementedException ();
                }
 
                [MonoTODO]
-               public virtual XmlNode SelectSingleNode (string xpath)
+               public XmlNode SelectSingleNode (string xpath)
                {
                        throw new NotImplementedException ();
                }
 
                [MonoTODO]
-               public virtual XmlNode SelectSingleNode (string xpath, XmlNamespaceManager nsmgr)
+               public XmlNode SelectSingleNode (string xpath, XmlNamespaceManager nsmgr)
                {
                        throw new NotImplementedException ();
                }
@@ -288,10 +344,8 @@ namespace System.Xml
                        throw new NotImplementedException ();
                }
 
-               [MonoTODO]
                public abstract void WriteContentTo (XmlWriter w);
 
-               [MonoTODO]
                public abstract void WriteTo (XmlWriter w);
 
                #endregion