implementing XmlElement.SetAttributeNode(localName, namespaceURI) and
[mono.git] / mcs / class / System.XML / System.Xml / XmlNode.cs
index 84d2572fd409d35a5aed0028580e932ad14abcf6..d4d39a77aef518963f4a3e34284010eeb8e3f7b9 100644 (file)
@@ -10,6 +10,7 @@
 using System;
 using System.Collections;
 using System.IO;
+using System.Text;
 using System.Xml.XPath;
 
 namespace System.Xml
@@ -39,10 +40,9 @@ namespace System.Xml
                        get { return null; }
                }
 
-               [MonoTODO]
                public virtual string BaseURI
                {
-                       get { throw new NotImplementedException (); }
+                       get { return ParentNode.BaseURI; }
                }
 
                public virtual XmlNodeList ChildNodes {
@@ -68,10 +68,27 @@ namespace System.Xml
 
                [MonoTODO]
                public virtual string InnerText {
-                       get { throw new NotImplementedException (); }
+                       get {
+                               StringBuilder builder = new StringBuilder ();
+                               AppendChildValues (this, builder);
+                               return builder.ToString ();
+                       }
+
                        set { throw new NotImplementedException (); }
                }
 
+               private void AppendChildValues (XmlNode parent, StringBuilder builder)
+               {
+                       XmlNode node = parent.FirstChild;
+
+                       while (node != null) {
+                               if (node.NodeType == XmlNodeType.Text)
+                                       builder.Append (node.Value);
+                               AppendChildValues (node, builder);
+                               node = node.NextSibling;
+                       }
+               }
+
                [MonoTODO("Setter.")]
                public virtual string InnerXml {
                        get {
@@ -132,9 +149,8 @@ namespace System.Xml
 
                public abstract string Name     { get; }
 
-               [MonoTODO]
                public virtual string NamespaceURI {
-                       get { throw new NotImplementedException (); }
+                       get { return String.Empty; }
                }
 
                public virtual XmlNode NextSibling {
@@ -143,7 +159,6 @@ namespace System.Xml
 
                public abstract XmlNodeType NodeType { get;     }
 
-               [MonoTODO]
                public virtual string OuterXml {
                        get {
                                StringWriter sw = new StringWriter ();
@@ -163,10 +178,9 @@ namespace System.Xml
                        get { return parentNode; }
                }
 
-               [MonoTODO]
                public virtual string Prefix {
-                       get { throw new NotImplementedException (); }
-                       set { throw new NotImplementedException (); }
+                       get { return String.Empty; }
+                       set {}
                }
 
                public virtual XmlNode PreviousSibling {
@@ -184,9 +198,15 @@ namespace System.Xml
 
                public virtual XmlNode AppendChild (XmlNode newChild)
                {
-                       if (NodeType == XmlNodeType.Document
-                           || NodeType == XmlNodeType.Element
-                           || NodeType == XmlNodeType.Attribute) {
+                       XmlDocument ownerDoc = (NodeType == XmlNodeType.Document) ? (XmlDocument)this : OwnerDocument;
+
+                       ownerDoc.onNodeInserting (newChild, this);
+
+                       if (NodeType == XmlNodeType.Document || NodeType == XmlNodeType.Element || NodeType == XmlNodeType.Attribute) {
+                               
+                               if (newChild.OwnerDocument != ownerDoc)
+                                       throw new ArgumentException ("Can't append a node created by another document.");
+
                                XmlLinkedNode newLinkedChild = (XmlLinkedNode) newChild;
                                XmlLinkedNode lastLinkedChild = LastLinkedChild;
 
@@ -200,6 +220,8 @@ namespace System.Xml
                                
                                LastLinkedChild = newLinkedChild;
 
+                               ownerDoc.onNodeInserted (newChild, newChild.ParentNode);
+
                                return newChild;
                        } else
                                throw new InvalidOperationException();
@@ -216,7 +238,7 @@ namespace System.Xml
                [MonoTODO]
                public XPathNavigator CreateNavigator ()
                {
-                       throw new NotImplementedException ();
+                       return new XmlDocumentNavigator(this);
                }
 
                public IEnumerator GetEnumerator ()
@@ -272,11 +294,17 @@ namespace System.Xml
 
                public virtual void RemoveAll ()
                {
+                       XmlDocument ownerDoc = (NodeType == XmlNodeType.Document) ? (XmlDocument)this : OwnerDocument;
+
+                       ownerDoc.onNodeRemoving (this, this.ParentNode);
                        LastLinkedChild = null;
+                       ownerDoc.onNodeRemoved (this, this.ParentNode);
                }
 
                public virtual XmlNode RemoveChild (XmlNode oldChild)
                {
+                       OwnerDocument.onNodeRemoving (oldChild, oldChild.ParentNode);
+
                        if (NodeType == XmlNodeType.Document || NodeType == XmlNodeType.Element || NodeType == XmlNodeType.Attribute) 
                        {
                                if (IsReadOnly)
@@ -298,6 +326,8 @@ namespace System.Xml
                                        oldLinkedChild.NextLinkedSibling = null;
                                 }
 
+                               OwnerDocument.onNodeRemoved (oldChild, oldChild.ParentNode);
+
                                return oldChild;
                        } 
                        else
@@ -310,28 +340,43 @@ namespace System.Xml
                        throw new NotImplementedException ();
                }
 
-               [MonoTODO]
-               public virtual XmlNodeList SelectNodes (string xpath)
+               public XmlNodeList SelectNodes (string xpath)
                {
-                       throw new NotImplementedException ();
+                       return SelectNodes (xpath, null);
                }
 
                [MonoTODO]
-               public virtual XmlNodeList SelectNodes (string xpath, XmlNamespaceManager nsmgr)
+               public XmlNodeList SelectNodes (string xpath, XmlNamespaceManager nsmgr)
                {
-                       throw new NotImplementedException ();
+                       XPathNavigator nav = CreateNavigator ();
+                       XPathExpression expr = nav.Compile (xpath);
+                       if (nsmgr != null)
+                               expr.SetContext (nsmgr);
+                       XPathNodeIterator iter = nav.Select (expr);
+                       ArrayList rgNodes = new ArrayList ();
+                       while (iter.MoveNext ())
+                       {
+                               rgNodes.Add (((XmlDocumentNavigator) iter.Current).Node);
+                       }
+                       return new XmlNodeArrayList (rgNodes);
                }
 
-               [MonoTODO]
-               public virtual XmlNode SelectSingleNode (string xpath)
+               public XmlNode SelectSingleNode (string xpath)
                {
-                       throw new NotImplementedException ();
+                       return SelectSingleNode (xpath, null);
                }
 
                [MonoTODO]
-               public virtual XmlNode SelectSingleNode (string xpath, XmlNamespaceManager nsmgr)
+               public XmlNode SelectSingleNode (string xpath, XmlNamespaceManager nsmgr)
                {
-                       throw new NotImplementedException ();
+                       XPathNavigator nav = CreateNavigator ();
+                       XPathExpression expr = nav.Compile (xpath);
+                       if (nsmgr != null)
+                               expr.SetContext (nsmgr);
+                       XPathNodeIterator iter = nav.Select (expr);
+                       if (!iter.MoveNext ())
+                               return null;
+                       return ((XmlDocumentNavigator) iter.Current).Node;
                }
 
                internal void SetParentNode (XmlNode parent)