XmlNodeListChildren implements XmlNodeList for XmlNode.ChildNodes
authorKral Ferch <kral@mono-cvs.ximian.com>
Fri, 8 Mar 2002 04:45:30 +0000 (04:45 -0000)
committerKral Ferch <kral@mono-cvs.ximian.com>
Fri, 8 Mar 2002 04:45:30 +0000 (04:45 -0000)
svn path=/trunk/mcs/; revision=2987

14 files changed:
mcs/class/System.XML/Mono.System.XML.csproj
mcs/class/System.XML/System.Xml/ChangeLog
mcs/class/System.XML/System.Xml/XmlDocument.cs
mcs/class/System.XML/System.Xml/XmlElement.cs
mcs/class/System.XML/System.Xml/XmlLinkedNode.cs
mcs/class/System.XML/System.Xml/XmlNode.cs
mcs/class/System.XML/System.Xml/XmlNodeList.cs
mcs/class/System.XML/System.Xml/XmlNodeListAsArrayList.cs [deleted file]
mcs/class/System.XML/System.Xml/XmlNodeListChildren.cs [new file with mode: 0644]
mcs/class/System.XML/Test/AllTests.cs
mcs/class/System.XML/Test/ChangeLog
mcs/class/System.XML/Test/Microsoft.Test.csproj
mcs/class/System.XML/Test/Mono.Test.csproj
mcs/class/System.XML/Test/XmlNodeListTests.cs [new file with mode: 0644]

index b1e89e8a1319b7df972eef0abd903584e1c2137f..cfb344b6f04f3d6d2160068bc6d8d07c8b6aedde 100644 (file)
                     BuildAction = "Compile"
                 />
                 <File
-                    RelPath = "System.Xml\XmlNodeListAsArrayList.cs"
+                    RelPath = "System.Xml\XmlNodeListChildren.cs"
                     SubType = "Code"
                     BuildAction = "Compile"
                 />
index 038ccc6a7ed207ce6f366e9845b6ba034077c843..8404c838592962b970f6491ff91f252120612a83 100644 (file)
@@ -1,3 +1,18 @@
+2002-03-08  Kral Ferch <kral_ferch@hotmail.com>
+
+       * XmlNodeList.cs, XmlDocument.cs, XmlLinkedNode.cs,
+       XmlNode.cs: Formatting.
+       
+       * XmlNodeListChildren.cs: Implementation of XmlNodeList
+       for XmlNode.ChildNodes property.
+       
+       * XmlNodeListAsArrayList.cs: Removed file.  Using different
+       data structure (circular list) in XmlNode so this file
+       is no longer valid.
+       
+       * XmlDocument.cs, XmlElement.cs: New ChildNodes tests found
+       bug in setter property of LastLinkedChild so fixed it.
+       
 2002-03-06  Jason Diamond  <jason@injektilo.org>
 
        * XmlInputSource.cs, XmlNames_1_0.cs, XmlParse.cs: Removed files.
index 987112960e714f0a214ad8b8922f92cea36bfbdc..29dc09edad3334519191f3b477ab780141812a4b 100644 (file)
@@ -17,12 +17,22 @@ namespace System.Xml
        public class XmlDocument : XmlNode
        {
                #region Fields
+               ///////////////////////////////////////////////////////////////////////
+               //
+               //      Fields
+               //
+               ///////////////////////////////////////////////////////////////////////
 
                private XmlLinkedNode lastChild;
 
                #endregion
 
                #region Constructors
+               ///////////////////////////////////////////////////////////////////////
+               //
+               //      Constructors
+               //
+               ///////////////////////////////////////////////////////////////////////
 
                public XmlDocument () : base (null) { }
 
@@ -41,6 +51,11 @@ namespace System.Xml
                #endregion
 
                #region Events
+               ///////////////////////////////////////////////////////////////////////
+               //
+               //      Events
+               //
+               ///////////////////////////////////////////////////////////////////////
 
                public event XmlNodeChangedEventHandler NodeChanged;
 
@@ -57,6 +72,11 @@ namespace System.Xml
                #endregion
 
                #region Properties
+               ///////////////////////////////////////////////////////////////////////
+               //
+               //      Properties
+               //
+               ///////////////////////////////////////////////////////////////////////
 
                [MonoTODO]
                public override string BaseURI {
@@ -124,6 +144,7 @@ namespace System.Xml
                                
                                value.NextLinkedSibling = LastLinkedChild.NextLinkedSibling;
                                LastLinkedChild.NextLinkedSibling = value;
+                               lastChild = value;
 
                                SetParentNode(this);
                        }
@@ -165,6 +186,11 @@ namespace System.Xml
                #endregion
 
                #region Methods
+               ///////////////////////////////////////////////////////////////////////
+               //
+               //      Methods
+               //
+               ///////////////////////////////////////////////////////////////////////
 
                [MonoTODO]
                public override XmlNode CloneNode (bool deep)
index 857517c24f4532734dac8fd556f9ab46b564ad3d..a86d86af1f37577e20d24da147cb98498c6a48a0 100644 (file)
@@ -115,6 +115,7 @@ namespace System.Xml
                                
                                value.NextLinkedSibling = LastLinkedChild.NextLinkedSibling;
                                LastLinkedChild.NextLinkedSibling = value;
+                               lastChild = value;
 
                                SetParentNode(this);
                        }
index e1675724e6ccf77793c95c70718ef6d261bfdc44..2641eefeccfcd4febce859fb3915367011d0550e 100644 (file)
@@ -4,15 +4,34 @@ namespace System.Xml
 {
        public abstract class XmlLinkedNode : XmlNode
        {
+               #region Fields
+               ///////////////////////////////////////////////////////////////////////
+               //
+               //      Fields
+               //
+               ///////////////////////////////////////////////////////////////////////
+
                XmlLinkedNode nextSibling;
 
+               #endregion
+
                #region Constructors
+               ///////////////////////////////////////////////////////////////////////
+               //
+               //      Constructors
+               //
+               ///////////////////////////////////////////////////////////////////////
 
                protected internal XmlLinkedNode(XmlDocument doc) : base(doc) { }
 
                #endregion
 
                #region Properties
+               ///////////////////////////////////////////////////////////////////////
+               //
+               //      Properties
+               //
+               ///////////////////////////////////////////////////////////////////////
 
                public override XmlNode NextSibling
                {
@@ -41,6 +60,5 @@ namespace System.Xml
                }
 
                #endregion
-
        }
 }
\ No newline at end of file
index bfe9a942761d5bfb809c86eecf380a373edcb663..cac9cc9c9c542899481c06c02a06a288c4e78792 100644 (file)
@@ -1,5 +1,5 @@
 //
-// System.Xml.XmlProcessingInstruction
+// System.Xml.XmlNode
 //
 // Author:
 //   Kral Ferch <kral_ferch@hotmail.com>
@@ -15,10 +15,24 @@ namespace System.Xml
 {
        public abstract class XmlNode : ICloneable, IEnumerable, IXPathNavigable
        {
+               #region Fields
+               ///////////////////////////////////////////////////////////////////////
+               //
+               //      Fields
+               //
+               ///////////////////////////////////////////////////////////////////////
+
                XmlDocument ownerDocument;
                XmlNode parentNode;
 
+               #endregion
+
                #region Constructors
+               ///////////////////////////////////////////////////////////////////////
+               //
+               //      Constructors
+               //
+               ///////////////////////////////////////////////////////////////////////
 
                protected internal XmlNode(XmlDocument ownerDocument)
                {
@@ -28,6 +42,11 @@ namespace System.Xml
                #endregion
 
                #region Properties
+               ///////////////////////////////////////////////////////////////////////
+               //
+               //      Properties
+               //
+               ///////////////////////////////////////////////////////////////////////
 
                public virtual XmlAttributeCollection Attributes
                {
@@ -40,14 +59,13 @@ namespace System.Xml
                        get { throw new NotImplementedException (); }
                }
 
-               [MonoTODO]
-               public virtual XmlNodeList ChildNodes
-               {
-                       get { throw new NotImplementedException (); }
+               public virtual XmlNodeList ChildNodes {
+                       get {
+                               return new XmlNodeListChildren(LastLinkedChild);
+                       }
                }
 
-               public virtual XmlNode FirstChild
-               {
+               public virtual XmlNode FirstChild {
                        get {
                                if (LastChild != null) {
                                        return LastLinkedChild.NextLinkedSibling;
@@ -58,114 +76,91 @@ namespace System.Xml
                        }
                }
 
-               public virtual bool HasChildNodes
-               {
+               public virtual bool HasChildNodes {
                        get { return LastChild != null; }
                }
 
                [MonoTODO]
-               public virtual string InnerText
-               {
+               public virtual string InnerText {
                        get { throw new NotImplementedException (); }
                        set { throw new NotImplementedException (); }
                }
 
                [MonoTODO]
-               public virtual string InnerXml
-               {
+               public virtual string InnerXml {
                        get { throw new NotImplementedException (); }
                        set { throw new NotImplementedException (); }
                }
 
                [MonoTODO]
-               public virtual bool IsReadOnly
-               {
+               public virtual bool IsReadOnly {
                        get { throw new NotImplementedException (); }
                }
 
                [MonoTODO]
-               public virtual XmlElement this[string name]
-               {
+               [System.Runtime.CompilerServices.IndexerName("Item")]
+               public virtual XmlElement this [string name] {
                        get { throw new NotImplementedException (); }
                }
 
                [MonoTODO]
-               public virtual XmlElement this[string localname, string ns]
-               {
+               [System.Runtime.CompilerServices.IndexerName("Item")]
+               public virtual XmlElement this [string localname, string ns] {
                        get { throw new NotImplementedException (); }
                }
 
-               public virtual XmlNode LastChild
-               {
+               public virtual XmlNode LastChild {
                        get { return LastLinkedChild; }
                }
 
-               internal virtual XmlLinkedNode LastLinkedChild
-               {
+               internal virtual XmlLinkedNode LastLinkedChild {
                        get { return null; }
                        set { }
                }
 
                [MonoTODO]
-               public abstract string LocalName
-               {
-                       get;
-               }
+               public abstract string LocalName { get; }
 
                [MonoTODO]
-               public abstract string Name
-               {
-                       get;
-               }
+               public abstract string Name     { get; }
 
                [MonoTODO]
-               public virtual string NamespaceURI
-               {
+               public virtual string NamespaceURI {
                        get { throw new NotImplementedException (); }
                }
 
-               public virtual XmlNode NextSibling
-               {
+               public virtual XmlNode NextSibling {
                        get { return null; }
                }
 
                [MonoTODO]
-               public abstract XmlNodeType NodeType
-               {
-                       get;
-               }
+               public abstract XmlNodeType NodeType { get;     }
 
                [MonoTODO]
-               public virtual string OuterXml
-               {
+               public virtual string OuterXml {
                        get { throw new NotImplementedException (); }
                }
 
-               public virtual XmlDocument OwnerDocument
-               {
+               public virtual XmlDocument OwnerDocument {
                        get { return ownerDocument; }
                }
 
-               public virtual XmlNode ParentNode
-               {
+               public virtual XmlNode ParentNode {
                        get { return parentNode; }
                }
 
                [MonoTODO]
-               public virtual string Prefix
-               {
+               public virtual string Prefix {
                        get { throw new NotImplementedException (); }
                        set { throw new NotImplementedException (); }
                }
 
-               public virtual XmlNode PreviousSibling
-               {
+               public virtual XmlNode PreviousSibling {
                        get { return null; }
                }
 
                [MonoTODO]
-               public virtual string Value
-               {
+               public virtual string Value {
                        get { throw new NotImplementedException (); }
                        set { throw new NotImplementedException (); }
                }
@@ -173,6 +168,11 @@ namespace System.Xml
                #endregion
 
                #region Methods
+               ///////////////////////////////////////////////////////////////////////
+               //
+               //      Methods
+               //
+               ///////////////////////////////////////////////////////////////////////
 
                public virtual XmlNode AppendChild (XmlNode newChild)
                {
index 1d166b9f118c0300cdd9cfbd9e68ba93c7b5f402..ef788fafb7cf016849aea1a89eb5bd888c0e4246 100644 (file)
@@ -1,42 +1,57 @@
+//
+// System.Xml.XmlNodeList
+//
+// Author:
+//   Kral Ferch <kral_ferch@hotmail.com>
+//
+// (C) 2002 Kral Ferch
+//
+
 using System;
 using System.Collections;
 
 namespace System.Xml
 {
-       /// <summary>
-       /// Abstract class XmlNodeList.
-       /// </summary>
        public abstract class XmlNodeList : IEnumerable
        {
-               // public properties
-               public abstract int Count { get; }
+               #region Constructors
+               ///////////////////////////////////////////////////////////////////////
+               //
+               //      Constructors
+               //
+               ///////////////////////////////////////////////////////////////////////
+
+               protected internal XmlNodeList() { }
+
+               #endregion
+
+               #region Properties
+               ///////////////////////////////////////////////////////////////////////
+               //
+               //      Properties
+               //
+               ///////////////////////////////////////////////////////////////////////
+
+               public abstract int Count {     get; }
 
                [System.Runtime.CompilerServices.IndexerName("ItemOf")]
-               public virtual XmlNode this[int i] 
-               {
-                       get
-                       {
-                               return Item(i);
-                       }
+               public virtual XmlNode this [int i]     {
+                       get { return Item(i); }
                }
 
-               // Public Methods
-               /// <summary>
-               /// Abstract.  Return the enumerator for the class.
-               /// </summary>
-               /// <returns>Enumerator</returns>
-               public abstract IEnumerator GetEnumerator();
-
-               /// <summary>
-               /// Abstract.  Returns the item at index.  Index is 0-based.
-               /// </summary>
-               /// <param name="index"></param>
-               /// <returns></returns>
-               public abstract XmlNode Item(int index);
-               
-               public XmlNodeList()
-               {
-                       // TODO: What should be done in constructor for XmlNodeList.XmlNodeList()? (nothing)
-               }
+               #endregion
+
+               #region Methods
+               ///////////////////////////////////////////////////////////////////////
+               //
+               //      Methods
+               //
+               ///////////////////////////////////////////////////////////////////////
+
+               public abstract IEnumerator GetEnumerator ();
+
+               public abstract XmlNode Item (int index);
+
+               #endregion
        }
 }
diff --git a/mcs/class/System.XML/System.Xml/XmlNodeListAsArrayList.cs b/mcs/class/System.XML/System.Xml/XmlNodeListAsArrayList.cs
deleted file mode 100644 (file)
index dccb526..0000000
+++ /dev/null
@@ -1,57 +0,0 @@
-using System;
-using System.Collections;
-
-namespace System.Xml
-{
-       /// <summary>
-       /// Internal implementation of XmlNodeList for XmlNode
-       /// </summary>
-       internal class XmlNodeListAsArrayList : XmlNodeList
-       {
-               // Private data members
-               ArrayList _items;
-
-               // Public Methods
-               //===========================================================================
-               public override int Count
-               {
-                       get
-                       {
-                               return _items.Count;
-                       }
-               }
-
-               public IList data
-               {
-                       get
-                       {
-                               return _items;
-                       }
-               }
-               
-               // Public Methods
-               //===========================================================================
-               public override IEnumerator GetEnumerator()
-               {
-                       return _items.GetEnumerator();
-               }
-
-               public void Add(XmlNode node)
-               {
-                       _items.Add(node);
-               }
-
-               public override XmlNode Item(int index)
-               {
-                       if ((index >= 0) & (index < _items.Count))
-                               return _items[index] as XmlNode;
-                       else
-                               return null;
-               }
-
-               public XmlNodeListAsArrayList() : base()
-               {
-                       _items = new ArrayList();
-               }
-       }
-}
diff --git a/mcs/class/System.XML/System.Xml/XmlNodeListChildren.cs b/mcs/class/System.XML/System.Xml/XmlNodeListChildren.cs
new file mode 100644 (file)
index 0000000..dde6c42
--- /dev/null
@@ -0,0 +1,154 @@
+//
+// System.Xml.XmlNodeList
+//
+// Author:
+//   Kral Ferch <kral_ferch@hotmail.com>
+//
+// (C) 2002 Kral Ferch
+//
+
+using System;
+using System.Collections;
+
+namespace System.Xml
+{
+       public class XmlNodeListChildren : XmlNodeList
+       {
+               #region Enumerator
+               ///////////////////////////////////////////////////////////////////////
+               //
+               //      Enumerator
+               //
+               ///////////////////////////////////////////////////////////////////////
+               private class Enumerator : IEnumerator
+               {
+                       XmlLinkedNode currentChild;
+                       XmlLinkedNode lastChild;
+
+                       internal Enumerator (XmlLinkedNode lastChild)
+                       {
+                               currentChild = null;
+                               this.lastChild = lastChild;
+                       }
+
+                       public virtual object Current {
+                               get {
+                                       return currentChild;
+                               }
+                       }
+
+                       public virtual bool MoveNext()
+                       {
+                               bool passedEndOfCollection = Object.ReferenceEquals(currentChild, lastChild);
+
+                               if (currentChild == null) {
+                                       currentChild = lastChild;
+                               }
+
+                               currentChild = currentChild.NextLinkedSibling;
+
+                               return passedEndOfCollection;
+                       }
+
+                       public virtual void Reset()
+                       {
+                               currentChild = null;
+                       }
+               }
+
+               #endregion
+
+               #region Fields
+               ///////////////////////////////////////////////////////////////////////
+               //
+               //      Fields
+               //
+               ///////////////////////////////////////////////////////////////////////
+
+               XmlLinkedNode lastChild;
+
+               #endregion
+
+               #region Constructors
+               ///////////////////////////////////////////////////////////////////////
+               //
+               //      Constructors
+               //
+               ///////////////////////////////////////////////////////////////////////
+
+               public XmlNodeListChildren(XmlLinkedNode lastChild)
+               {
+                       this.lastChild = lastChild;
+               }
+
+               #endregion
+
+               #region Properties
+               ///////////////////////////////////////////////////////////////////////
+               //
+               //      Properties
+               //
+               ///////////////////////////////////////////////////////////////////////
+
+               public override int Count {
+                       get {
+                               int count = 0;
+
+                               if (lastChild != null) {
+                                       XmlLinkedNode currentChild = lastChild.NextLinkedSibling;
+                                       
+                                       count = 1;
+                                       while (!Object.ReferenceEquals(currentChild, lastChild)) {
+                                               currentChild = currentChild.NextLinkedSibling;
+                                               count++;
+                                       }
+                               }
+
+                               return count;
+                       }
+               }
+
+               #endregion
+
+               #region Methods
+               ///////////////////////////////////////////////////////////////////////
+               //
+               //      Methods
+               //
+               ///////////////////////////////////////////////////////////////////////
+
+               public override IEnumerator GetEnumerator ()
+               {
+                       return new Enumerator(lastChild);
+               }
+
+               public override XmlNode Item (int index)
+               {
+                       XmlNode requestedNode = null;
+
+                       // Instead of checking for && index < Count which has to walk
+                       // the whole list to get a count, we'll just keep a count since
+                       // we have to walk the list anyways to get to index.
+                       if ((index >= 0) && (lastChild != null)) {
+                               XmlLinkedNode currentChild = lastChild.NextLinkedSibling;
+                               int count = 0;
+
+                               while ((count < index) && !Object.ReferenceEquals(currentChild, lastChild)) 
+                               {
+                                       currentChild = currentChild.NextLinkedSibling;
+                                       count++;
+                               }
+
+                               if (count == index) {
+                                       requestedNode = currentChild;
+                               }
+                       }
+
+                       return requestedNode;
+               }
+
+               #endregion
+       }
+
+       
+}
index cd3ccf5c8dd39c8c3f6dfaf216eadeb78a606f4b..9df4e5019bd12d86aafa6c377470d07a66730dc2 100644 (file)
@@ -25,6 +25,7 @@ namespace Ximian.Mono.Tests
                                suite.AddTest (new TestSuite (typeof (XmlDocumentTests)));
                                suite.AddTest (new TestSuite (typeof (NameTableTests)));
                                suite.AddTest (new TestSuite (typeof (XmlElementTests)));
+                               suite.AddTest (new TestSuite (typeof (XmlNodeListTests)));
                                return suite;
                        }
                }
index f71e2d88dfd9fb6042d848fe4bb0bd91b015ec68..6934e040acf71dd3e5b79b8a92c2069a8978fdf8 100644 (file)
@@ -1,3 +1,9 @@
+2002-03-08  Kral Ferch <kral_ferch@hotmail.com>
+
+       * XmlNodeListTests.cs: New file.
+
+       * AllTests.cs: Added XmlNodeListTests.
+
 2002/03/08  Nick Drochak <ndrochak@gol.com>
 
        * System_test.build: Don't build test dll by default.  Only build
index 5e8e50353933c7427ffe6ab5202b37d391ad27b3..d573a5eb42814a8cdf9943a32fc2a47aca5002b6 100644 (file)
                     SubType = "Code"
                     BuildAction = "Compile"
                 />
+                <File
+                    RelPath = "XmlNodeListTests.cs"
+                    SubType = "Code"
+                    BuildAction = "Compile"
+                />
                 <File
                     RelPath = "XmlTextReaderTests.cs"
                     SubType = "Code"
index 22c55508c8389367fa981e09d6f40e6c3f97205f..2b0d0a4822f98d055d4d56e27ff601ddd5ba981c 100644 (file)
                     SubType = "Code"
                     BuildAction = "Compile"
                 />
+                <File
+                    RelPath = "XmlNodeListTests.cs"
+                    SubType = "Code"
+                    BuildAction = "Compile"
+                />
                 <File
                     RelPath = "XmlTextReaderTests.cs"
                     SubType = "Code"
diff --git a/mcs/class/System.XML/Test/XmlNodeListTests.cs b/mcs/class/System.XML/Test/XmlNodeListTests.cs
new file mode 100644 (file)
index 0000000..b786b8a
--- /dev/null
@@ -0,0 +1,53 @@
+using System;
+using System.Xml;
+
+using NUnit.Framework;
+
+namespace Ximian.Mono.Tests
+{
+       public class XmlNodeListTests : TestCase
+       {
+               public XmlNodeListTests () : base ("Ximian.Mono.Tests.XmlNodeListTests testsuite") {}
+               public XmlNodeListTests (string name) : base (name) {}
+
+               private XmlElement element;
+
+               protected override void SetUp ()
+               {
+                       XmlDocument document = new XmlDocument ();
+
+                       document.LoadXml ("<foo><child1/><child2/><child3/></foo>");
+                       element = document.DocumentElement;
+               }
+
+               ///////////////////////////////////////////////////////////////////////
+               //
+               //  XmlNodeListChildren tests.
+               //
+               ///////////////////////////////////////////////////////////////////////
+               
+               public void TestChildren()
+               {
+                       Assert ("Incorrect number of children returned from Count property.", element.ChildNodes.Count == 3);
+                       AssertNull ("Index less than zero should have returned null.", element.ChildNodes [-1]);
+                       AssertNull ("Index greater than or equal to Count should have returned null.", element.ChildNodes [3]);
+                       AssertEquals ("Didn't return the correct child.", element.FirstChild, element.ChildNodes[0]);
+                       AssertEquals ("Didn't return the correct child.", "child1", element.ChildNodes[0].LocalName);
+                       AssertEquals ("Didn't return the correct child.", "child2", element.ChildNodes[1].LocalName);
+                       AssertEquals ("Didn't return the correct child.", "child3", element.ChildNodes[2].LocalName);
+
+                       int index = 1;
+                       foreach (XmlNode node in element.ChildNodes) {
+                               AssertEquals ("Enumerator didn't return correct node.", "child" + index.ToString(), node.LocalName);
+                               index++;
+                       }
+               }
+
+
+               ///////////////////////////////////////////////////////////////////////
+               //
+               //  XmlNodeListSelect tests.
+               //
+               ///////////////////////////////////////////////////////////////////////
+       }
+}