Author: atsushi
Date: 2007-05-04 15:00:24 -0400 (Fri, 04 May 2007)
New Revision: 76717

Added:
   trunk/olive/class/System.Xml.Linq/Test/System.Xml.Linq/ExtensionsTest.cs
Modified:
   trunk/olive/class/System.Xml.Linq/System.Xml.Linq/ChangeLog
   trunk/olive/class/System.Xml.Linq/System.Xml.Linq/Extensions.cs
   trunk/olive/class/System.Xml.Linq/System.Xml.Linq/XAttribute.cs
   trunk/olive/class/System.Xml.Linq/System.Xml.Linq/XContainer.cs
   trunk/olive/class/System.Xml.Linq/System.Xml.Linq/XElement.cs
   trunk/olive/class/System.Xml.Linq/System.Xml.Linq/XNode.cs
   trunk/olive/class/System.Xml.Linq/System.Xml.Linq/XUtil.cs
   trunk/olive/class/System.Xml.Linq/System.Xml.Linq_test.dll.sources
   trunk/olive/class/System.Xml.Linq/Test/System.Xml.Linq/ChangeLog
   trunk/olive/class/System.Xml.Linq/Test/System.Xml.Linq/XAttributeTest.cs
   trunk/olive/class/System.Xml.Linq/Test/System.Xml.Linq/XElementTest.cs
Log:
2007-05-04  Atsushi Enomoto  <[EMAIL PROTECTED]>

        * XNode.cs : Nodes() is safe for removal of current item.
        * XElement.cs : Attributes() is safe for ditto. Removed unused code.
        * XUtil.cs : ToString() handles any type of object.
        * Extensions.cs : implemented everything but sorter.
        * XAttribute.cs : SetValue() and ToString() are done.
        * XNode.cs : set missing tree link info in AddBeforeSelf() and
          AddAfterSelf(). ReplaceWith() are done.

        * XElementTest.cs : added tests for Add[Before|After]Self().
        * XAttributeTest.cs :
          added tests for SetValue(), ToString() and ctor.
        * ExtensionsTest.cs : new file, tests for extension methods.



Modified: trunk/olive/class/System.Xml.Linq/System.Xml.Linq/ChangeLog
===================================================================
--- trunk/olive/class/System.Xml.Linq/System.Xml.Linq/ChangeLog 2007-05-04 
18:39:24 UTC (rev 76716)
+++ trunk/olive/class/System.Xml.Linq/System.Xml.Linq/ChangeLog 2007-05-04 
19:00:24 UTC (rev 76717)
@@ -1,5 +1,15 @@
 2007-05-04  Atsushi Enomoto  <[EMAIL PROTECTED]>
 
+       * XNode.cs : Nodes() is safe for removal of current item.
+       * XElement.cs : Attributes() is safe for ditto. Removed unused code.
+       * XUtil.cs : ToString() handles any type of object.
+       * Extensions.cs : implemented everything but sorter.
+       * XAttribute.cs : SetValue() and ToString() are done.
+       * XNode.cs : set missing tree link info in AddBeforeSelf() and
+         AddAfterSelf(). ReplaceWith() are done.
+
+2007-05-04  Atsushi Enomoto  <[EMAIL PROTECTED]>
+
        * XNode.cs
          XElement.cs
          XAttribute.cs

Modified: trunk/olive/class/System.Xml.Linq/System.Xml.Linq/Extensions.cs
===================================================================
--- trunk/olive/class/System.Xml.Linq/System.Xml.Linq/Extensions.cs     
2007-05-04 18:39:24 UTC (rev 76716)
+++ trunk/olive/class/System.Xml.Linq/System.Xml.Linq/Extensions.cs     
2007-05-04 19:00:24 UTC (rev 76717)
@@ -51,60 +51,68 @@
                                        yield return attr;
                }
 
-               [MonoTODO]
                public static IEnumerable<XNode> DescendantNodes<T> (
                        this IEnumerable<T> source) where T : XContainer
                {
-                       throw new NotImplementedException ();
+                       foreach (XContainer item in source)
+                               foreach (XNode n in item.DescendantNodes ())
+                                       yield return n;
                }
 
-               [MonoTODO]
                public static IEnumerable<XNode> DescendantNodesAndSelf (
                        this IEnumerable<XElement> source)
                {
-                       throw new NotImplementedException ();
+                       foreach (XElement item in source)
+                               foreach (XNode n in item.DescendantNodesAndSelf 
())
+                                       yield return n;
                }
 
-               [MonoTODO]
                public static IEnumerable<XElement> Descendants<T> (
                        this IEnumerable<T> source) where T : XContainer
                {
-                       throw new NotImplementedException ();
+                       foreach (XContainer item in source)
+                               foreach (XElement n in item.Descendants ())
+                                       yield return n;
                }
 
-               [MonoTODO]
                public static IEnumerable<XElement> Descendants<T> (
                        this IEnumerable<T> source, XName name) where T : 
XContainer
                {
-                       throw new NotImplementedException ();
+                       foreach (XContainer item in source)
+                               foreach (XElement n in item.Descendants (name))
+                                       yield return n;
                }
 
-               [MonoTODO]
                public static IEnumerable<XElement> DescendantsAndSelf (
                        this IEnumerable<XElement> source)
                {
-                       throw new NotImplementedException ();
+                       foreach (XElement item in source)
+                               foreach (XElement n in item.DescendantsAndSelf 
())
+                                       yield return n;
                }
 
-               [MonoTODO]
                public static IEnumerable<XElement> DescendantsAndSelf (
                        this IEnumerable<XElement> source, XName name)
                {
-                       throw new NotImplementedException ();
+                       foreach (XElement item in source)
+                               foreach (XElement n in item.DescendantsAndSelf 
(name))
+                                       yield return n;
                }
 
-               [MonoTODO]
                public static IEnumerable<XElement> Elements<T> (
                        this IEnumerable<T> source) where T : XContainer
                {
-                       throw new NotImplementedException ();
+                       foreach (XContainer item in source)
+                               foreach (XElement n in item.Elements ())
+                                       yield return n;
                }
 
-               [MonoTODO]
                public static IEnumerable<XElement> Elements<T> (
                        this IEnumerable<T> source, XName name) where T : 
XContainer
                {
-                       throw new NotImplementedException ();
+                       foreach (XContainer item in source)
+                               foreach (XElement n in item.Elements (name))
+                                       yield return n;
                }
 
                [MonoTODO]
@@ -114,23 +122,24 @@
                        throw new NotImplementedException ();
                }
 
-               [MonoTODO]
                public static IEnumerable<XNode> Nodes<T> (
                        this IEnumerable<T> source) where T : XContainer
                {
-                       throw new NotImplementedException ();
+                       foreach (XContainer item in source)
+                               foreach (XNode n in item.Nodes ())
+                                       yield return n;
                }
 
-               [MonoTODO]
                public static void Remove (this IEnumerable<XAttribute> source)
                {
-                       throw new NotImplementedException ();
+                       foreach (XAttribute item in source)
+                               item.Remove ();
                }
 
-               [MonoTODO]
                public static void Remove<T> (this IEnumerable<T> source) where 
T : XNode
                {
-                       throw new NotImplementedException ();
+                       foreach (T item in source)
+                               item.Remove ();
                }
        }
 }

Modified: trunk/olive/class/System.Xml.Linq/System.Xml.Linq/XAttribute.cs
===================================================================
--- trunk/olive/class/System.Xml.Linq/System.Xml.Linq/XAttribute.cs     
2007-05-04 18:39:24 UTC (rev 76716)
+++ trunk/olive/class/System.Xml.Linq/System.Xml.Linq/XAttribute.cs     
2007-05-04 19:00:24 UTC (rev 76717)
@@ -1,5 +1,6 @@
 using System;
 using System.Collections.Generic;
+using System.Text;
 using System.Xml;
 
 namespace System.Xml.Linq
@@ -13,7 +14,7 @@
                }
 
                XName name;
-               object value;
+               string value;
                XAttribute next;
                XAttribute previous;
 
@@ -77,16 +78,40 @@
                        previous = null;
                }
 
-               [MonoTODO]
                public void SetValue (object value)
                {
+                       if (value == null)
+                               throw new ArgumentNullException ("value");
                        this.value = XUtil.ToString (value);
                }
 
-               [MonoTODO]
+               static readonly char [] escapeChars = new char [] {'<', '>', 
'&', '"'};
+
                public override string ToString ()
                {
-                       throw new NotImplementedException ();
+                       StringBuilder sb = new StringBuilder ();
+                       sb.Append (name.ToString ());
+                       sb.Append ("=\"");
+                       int start = 0;
+                       do {
+                               int idx = value.IndexOfAny (escapeChars, start);
+                               if (idx < 0) {
+                                       if (start > 0)
+                                               sb.Append (value, start, 
value.Length - start);
+                                       else
+                                               sb.Append (value);
+                                       sb.Append ("\"");
+                                       return sb.ToString ();
+                               }
+                               sb.Append (value, start, idx - start);
+                               switch (value [idx]) {
+                               case '&': sb.Append ("&amp;"); break;
+                               case '<': sb.Append ("&lt;"); break;
+                               case '>': sb.Append ("&gt;"); break;
+                               case '"': sb.Append ("&quot;"); break;
+                               }
+                               start = idx + 1;
+                       } while (true);
                }
        }
 }

Modified: trunk/olive/class/System.Xml.Linq/System.Xml.Linq/XContainer.cs
===================================================================
--- trunk/olive/class/System.Xml.Linq/System.Xml.Linq/XContainer.cs     
2007-05-04 18:39:24 UTC (rev 76716)
+++ trunk/olive/class/System.Xml.Linq/System.Xml.Linq/XContainer.cs     
2007-05-04 19:00:24 UTC (rev 76717)
@@ -112,9 +112,11 @@
 
                public IEnumerable <XNode> Nodes ()
                {
-                       //return new XChildrenIterator (this);
-                       for (XNode n = FirstNode; n != null; n = n.NextNode)
+                       XNode next;
+                       for (XNode n = FirstNode; n != null; n = next) {
+                               next = n.NextNode;
                                yield return n;
+                       }
                }
 
                public IEnumerable<XNode> DescendantNodes ()

Modified: trunk/olive/class/System.Xml.Linq/System.Xml.Linq/XElement.cs
===================================================================
--- trunk/olive/class/System.Xml.Linq/System.Xml.Linq/XElement.cs       
2007-05-04 18:39:24 UTC (rev 76716)
+++ trunk/olive/class/System.Xml.Linq/System.Xml.Linq/XElement.cs       
2007-05-04 19:00:24 UTC (rev 76717)
@@ -45,16 +45,6 @@
                        Add (contents);
                }
 
-               /*
-               internal List <XAttribute> SafeAttributes {
-                       get {
-                               if (attributes == null)
-                                       attributes = new List <XAttribute> ();
-                               return attributes;
-                       }
-               }
-               */
-
                public XAttribute FirstAttribute {
                        get { return attr_first; }
                        internal set { attr_first = value; }
@@ -132,8 +122,11 @@
 
                public IEnumerable <XAttribute> Attributes ()
                {
-                       for (XAttribute a = attr_first; a != null; a = 
a.NextAttribute)
+                       XAttribute next;
+                       for (XAttribute a = attr_first; a != null; a = next) {
+                               next = a.NextAttribute;
                                yield return a;
+                       }
                }
 
                // huh?
@@ -177,23 +170,6 @@
                }
                */
 
-/*
-               // Only XAttribute.set_Parent() can invoke this.
-               internal void InternalAppendAttribute (XAttribute attr)
-               {
-                       if (attr.Parent != this)
-                               throw new SystemException ("INTERNAL ERROR: 
should not happen.");
-                       SafeAttributes.Add (attr);
-               }
-
-               internal void InternalRemoveAttribute (XAttribute attr)
-               {
-                       if (attr.Parent != this)
-                               throw new SystemException ("INTERNAL ERROR: 
should not happen.");
-                       attributes.Remove (attr);
-               }
-*/
-
                public static XElement Load (string uri)
                {
                        return Load (uri, LoadOptions.None);

Modified: trunk/olive/class/System.Xml.Linq/System.Xml.Linq/XNode.cs
===================================================================
--- trunk/olive/class/System.Xml.Linq/System.Xml.Linq/XNode.cs  2007-05-04 
18:39:24 UTC (rev 76716)
+++ trunk/olive/class/System.Xml.Linq/System.Xml.Linq/XNode.cs  2007-05-04 
19:00:24 UTC (rev 76717)
@@ -74,6 +74,9 @@
                        XNode n = XUtil.ToNode (content);
                        n.SetOwner (Parent);
                        n.previous = this;
+                       n.next = next;
+                       if (next != null)
+                               next.previous = n;
                        next = n;
                        if (Parent.LastNode == this)
                                Parent.LastNode = n;
@@ -83,8 +86,11 @@
                {
                        if (Parent == null)
                                throw new InvalidOperationException ();
-                       foreach (object o in content)
-                               AddAfterSelf (o);
+                       XNode n = this;
+                       foreach (object o in content) {
+                               n.AddAfterSelf (o);
+                               n = n.NextNode;
+                       }
                }
 
                public void AddBeforeSelf (object content)
@@ -93,7 +99,10 @@
                                throw new InvalidOperationException ();
                        XNode n = XUtil.ToNode (content);
                        n.SetOwner (Parent);
+                       n.previous = previous;
                        n.next = this;
+                       if (previous != null)
+                               previous.next = n;
                        previous = n;
                        if (Parent.FirstNode == this)
                                Parent.FirstNode = n;
@@ -103,8 +112,8 @@
                {
                        if (Parent == null)
                                throw new InvalidOperationException ();
-                       for (int i = content.Length - 1; i >= 0; i--)
-                               AddBeforeSelf (content [i]);
+                       foreach (object o in content)
+                               AddBeforeSelf (o);
                }
 
                public static XNode ReadFrom (XmlReader r)
@@ -144,7 +153,17 @@
 
                public void Remove ()
                {
-                       PreviousNode.next = NextNode;
+                       if (Parent == null)
+                               throw new InvalidOperationException ("Parent is 
missing");
+
+                       if (Parent.FirstNode == this)
+                               Parent.FirstNode = next;
+                       if (Parent.LastNode == this)
+                               Parent.LastNode = previous;
+                       if (previous != null)
+                               previous.next = next;
+                       if (next != null)
+                               next.previous = previous;
                        previous = null;
                        next = null;
                        SetOwner (null);
@@ -228,16 +247,16 @@
                                yield return n;
                }
 
-               [MonoTODO]
                public void ReplaceWith (object item)
                {
-                       throw new NotImplementedException ();
+                       AddAfterSelf (item);
+                       Remove ();
                }
 
-               [MonoTODO]
-               public void ReplaceWith (params object [] item)
+               public void ReplaceWith (params object [] items)
                {
-                       throw new NotImplementedException ();
+                       AddAfterSelf (items);
+                       Remove ();
                }
        }
 }

Modified: trunk/olive/class/System.Xml.Linq/System.Xml.Linq/XUtil.cs
===================================================================
--- trunk/olive/class/System.Xml.Linq/System.Xml.Linq/XUtil.cs  2007-05-04 
18:39:24 UTC (rev 76716)
+++ trunk/olive/class/System.Xml.Linq/System.Xml.Linq/XUtil.cs  2007-05-04 
19:00:24 UTC (rev 76717)
@@ -19,9 +19,11 @@
                // FIXME: implement
                public static string ToString (object o)
                {
+                       if (o == null)
+                               throw new InvalidOperationException ("Attempt 
to get string from null");
                        if (o is string)
                                return (string) o;
-                       throw new NotImplementedException ();
+                       return o.ToString ();
                }
 
                public static bool ToBoolean (object o)

Modified: trunk/olive/class/System.Xml.Linq/System.Xml.Linq_test.dll.sources
===================================================================
--- trunk/olive/class/System.Xml.Linq/System.Xml.Linq_test.dll.sources  
2007-05-04 18:39:24 UTC (rev 76716)
+++ trunk/olive/class/System.Xml.Linq/System.Xml.Linq_test.dll.sources  
2007-05-04 19:00:24 UTC (rev 76717)
@@ -1,3 +1,4 @@
+System.Xml.Linq/ExtensionsTest.cs
 System.Xml.Linq/XAttributeTest.cs
 System.Xml.Linq/XDocumentTest.cs
 System.Xml.Linq/XElementTest.cs

Modified: trunk/olive/class/System.Xml.Linq/Test/System.Xml.Linq/ChangeLog
===================================================================
--- trunk/olive/class/System.Xml.Linq/Test/System.Xml.Linq/ChangeLog    
2007-05-04 18:39:24 UTC (rev 76716)
+++ trunk/olive/class/System.Xml.Linq/Test/System.Xml.Linq/ChangeLog    
2007-05-04 19:00:24 UTC (rev 76717)
@@ -1,5 +1,12 @@
 2007-05-04  Atsushi Enomoto  <[EMAIL PROTECTED]>
 
+       * XElementTest.cs : added tests for Add[Before|After]Self().
+       * XAttributeTest.cs :
+         added tests for SetValue(), ToString() and ctor.
+       * ExtensionsTest.cs : new file, tests for extension methods.
+
+2007-05-04  Atsushi Enomoto  <[EMAIL PROTECTED]>
+
        * XAttributeTest.cs : new test, for XAttribute.
        * XElementTest.cs : attribute related tests.
 

Added: trunk/olive/class/System.Xml.Linq/Test/System.Xml.Linq/ExtensionsTest.cs
===================================================================
--- trunk/olive/class/System.Xml.Linq/Test/System.Xml.Linq/ExtensionsTest.cs    
2007-05-04 18:39:24 UTC (rev 76716)
+++ trunk/olive/class/System.Xml.Linq/Test/System.Xml.Linq/ExtensionsTest.cs    
2007-05-04 19:00:24 UTC (rev 76717)
@@ -0,0 +1,22 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Xml;
+using System.Xml.Linq;
+
+using NUnit.Framework;
+
+namespace MonoTests.System.Xml.Linq
+{
+       [TestFixture]
+       public class ExtensionsTest
+       {
+               [Test]
+               public void Remove ()
+               {
+                       XDocument doc = XDocument.Parse 
("<root><foo/><bar/><baz/></root>");
+                       doc.Root.Nodes ().Remove ();
+                       Assert.IsNull (doc.Root.FirstNode, "#1");
+               }
+       }
+}

Modified: 
trunk/olive/class/System.Xml.Linq/Test/System.Xml.Linq/XAttributeTest.cs
===================================================================
--- trunk/olive/class/System.Xml.Linq/Test/System.Xml.Linq/XAttributeTest.cs    
2007-05-04 18:39:24 UTC (rev 76716)
+++ trunk/olive/class/System.Xml.Linq/Test/System.Xml.Linq/XAttributeTest.cs    
2007-05-04 19:00:24 UTC (rev 76717)
@@ -10,7 +10,24 @@
        [TestFixture]
        public class XAttributeTest
        {
+
                [Test]
+               [ExpectedException (typeof (ArgumentNullException))]
+               public void ConstructorNameNull ()
+               {
+                       XAttribute a = new XAttribute (null, "v");
+                       
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentNullException))]
+               public void ConstructorValueNull ()
+               {
+                       XAttribute a = new XAttribute (XName.Get ("a"), null);
+                       
+               }
+
+               [Test]
                public void IsNamespaceDeclaration ()
                {
                        string xml = "<root a='v' xmlns='urn:foo' 
xmlns:x='urn:x' x:a='v' xmlns:xml='http://www.w3.org/XML/1998/namespace' />";
@@ -45,5 +62,24 @@
                                Assert.AreEqual (doc, a.Document, "#2");
                        Assert.AreEqual (doc, doc.Document, "#3");
                }
+
+               [Test]
+               public void SetValue ()
+               {
+                       XAttribute a = new XAttribute (XName.Get ("a"), "v");
+                       a.SetValue (new XDeclaration ("1.0", null, null));
+                       // value object is converted to a string.
+                       Assert.AreEqual ("<?xml version=\"1.0\"?>", a.Value, 
"#1");
+               }
+
+               [Test]
+               public void ToString ()
+               {
+                       XAttribute a = new XAttribute (XName.Get ("a"), "v");
+                       Assert.AreEqual ("a=\"v\"", a.ToString ());
+
+                       a = new XAttribute (XName.Get ("a"), " >_< ");
+                       Assert.AreEqual ("a=\" &gt;_&lt; \"", a.ToString ());
+               }
        }
 }

Modified: trunk/olive/class/System.Xml.Linq/Test/System.Xml.Linq/XElementTest.cs
===================================================================
--- trunk/olive/class/System.Xml.Linq/Test/System.Xml.Linq/XElementTest.cs      
2007-05-04 18:39:24 UTC (rev 76716)
+++ trunk/olive/class/System.Xml.Linq/Test/System.Xml.Linq/XElementTest.cs      
2007-05-04 19:00:24 UTC (rev 76717)
@@ -148,5 +148,78 @@
                        Assert.IsNull (el.FirstAttribute, "#4-4");
                        Assert.IsNull (el.LastAttribute, "#4-5");
                }
+
+               [Test]
+               public void AddAfterSelf ()
+               {
+                       XElement el = XElement.Parse 
("<root><foo/><bar/></root>");
+                       el.FirstNode.AddAfterSelf ("text");
+                       XText t = el.FirstNode.NextNode as XText;
+                       Assert.IsNotNull (t, "#1");
+                       Assert.AreEqual ("text", t.Value, "#2");
+                       XElement bar = t.NextNode as XElement;
+                       Assert.IsNotNull (bar, "#3");
+                       Assert.AreEqual ("bar", bar.Name.LocalName, "#4");
+               }
+
+               [Test]
+               public void AddAfterSelfList ()
+               {
+                       XElement el = XElement.Parse 
("<root><foo/><bar/></root>");
+                       el.FirstNode.AddAfterSelf (new XText [] {
+                               new XText ("t1"),
+                               new XText ("t2"),
+                               new XText ("t3")});
+                       XText t = el.FirstNode.NextNode as XText;
+                       Assert.IsNotNull (t, "#1");
+                       Assert.AreEqual ("t1", t.Value, "#2");
+                       Assert.AreEqual ("t2", ((XText) t.NextNode).Value, 
"#3");
+                       Assert.AreEqual ("t3", ((XText) 
t.NextNode.NextNode).Value, "#4");
+                       XElement bar = t.NextNode.NextNode.NextNode as XElement;
+                       Assert.IsNotNull (bar, "#5");
+                       Assert.AreEqual ("bar", bar.Name.LocalName, "#6");
+               }
+
+               [Test]
+               public void AddBeforeSelf ()
+               {
+                       XElement el = XElement.Parse 
("<root><foo/><bar/></root>");
+                       el.FirstNode.AddBeforeSelf ("text");
+                       XText t = el.FirstNode as XText;
+                       Assert.IsNotNull (t, "#1");
+                       Assert.AreEqual ("text", t.Value, "#2");
+                       XElement foo = t.NextNode as XElement;
+                       Assert.IsNotNull (foo, "#3");
+                       Assert.AreEqual ("foo", foo.Name.LocalName, "#4");
+               }
+
+               [Test]
+               public void AddBeforeSelfList ()
+               {
+                       XElement el = XElement.Parse 
("<root><foo/><bar/></root>");
+                       el.FirstNode.AddBeforeSelf (new XText [] {
+                               new XText ("t1"),
+                               new XText ("t2"),
+                               new XText ("t3")});
+                       XText t = el.FirstNode as XText;
+                       Assert.IsNotNull (t, "#1");
+                       Assert.AreEqual ("t1", t.Value, "#2");
+                       Assert.AreEqual ("t2", ((XText) t.NextNode).Value, 
"#3");
+                       Assert.AreEqual ("t3", ((XText) 
t.NextNode.NextNode).Value, "#4");
+                       XElement foo = t.NextNode.NextNode.NextNode as XElement;
+                       Assert.IsNotNull (foo, "#5");
+                       Assert.AreEqual ("foo", foo.Name.LocalName, "#6");
+               }
+
+               [Test]
+               public void ReplaceWith ()
+               {
+                       XElement el = XElement.Parse 
("<root><foo/><bar/></root>");
+                       XNode fc = el.FirstNode;
+                       fc.ReplaceWith("test");
+                       XText t = el.FirstNode as XText;
+                       Assert.IsNotNull (t, "#1");
+                       Assert.AreEqual ("test", t.Value, "#2");
+               }
        }
 }

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to