2002-07-26 Tim Coleman <tim@timcoleman.com>
[mono.git] / mcs / class / System.XML / System.Xml / XmlNode.cs
index 851c0115629e3aeab84c14d4a40067f2ad82285f..59a5c752aa4603cd041fbcf18c3f72828668a836 100644 (file)
@@ -9,6 +9,8 @@
 
 using System;
 using System.Collections;
+using System.IO;
+using System.Text;
 using System.Xml.XPath;
 
 namespace System.Xml
@@ -24,7 +26,7 @@ namespace System.Xml
 
                #region Constructors
 
-               protected internal XmlNode(XmlDocument ownerDocument)
+               internal XmlNode (XmlDocument ownerDocument)
                {
                        this.ownerDocument = ownerDocument;
                }
@@ -38,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 {
@@ -67,13 +68,37 @@ 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 (); }
                }
 
-               [MonoTODO]
+               private void AppendChildValues(XmlNode parent, StringBuilder builder)
+               {
+                       XmlNode node = parent.FirstChild;
+
+                       while (node != null) {
+                               builder.Append (node.Value);
+                               AppendChildValues (node, builder);
+                               node = node.NextSibling;
+                       }
+               }
+
+               [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 (); }
                }
 
@@ -123,9 +148,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 {
@@ -134,9 +158,15 @@ namespace System.Xml
 
                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 {
@@ -147,20 +177,18 @@ 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 {
                        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
@@ -169,9 +197,13 @@ 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;
@@ -195,9 +227,9 @@ namespace System.Xml
                public abstract XmlNode CloneNode (bool deep);
 
                [MonoTODO]
-               public XPathNavigator CreateNavigator ()
+               public virtual XPathNavigator CreateNavigator ()
                {
-                       throw new NotImplementedException ();
+                       return new XmlDocumentNavigator(this);
                }
 
                public IEnumerator GetEnumerator ()
@@ -291,28 +323,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)