2006-12-05 Atsushi Enomoto <atsushi@ximian.com>
authorAtsushi Eno <atsushieno@gmail.com>
Tue, 5 Dec 2006 10:07:29 +0000 (10:07 -0000)
committerAtsushi Eno <atsushieno@gmail.com>
Tue, 5 Dec 2006 10:07:29 +0000 (10:07 -0000)
* XmlNode.cs : GetPrefixOfNamespace() was not correctly searching
  ancestors' namespaces.

* XPathEditableDocument.cs : those writers should lookup prefix when
  WriteStartElement/WriteStartAttribute are passed null prefix.
  Removed nodeStack in XmlDocumentInsertionWriter, since nodes are
  always added to current node immediately.

* XmlNodeTests.cs : added another test for GetPrefixOfNamespace().

* XPathNavigatorTests.cs : added test for XPath navigator editor's
  LookupPrefix().

svn path=/trunk/mcs/; revision=69021

mcs/class/System.XML/Mono.Xml.XPath/ChangeLog
mcs/class/System.XML/Mono.Xml.XPath/XPathEditableDocument.cs
mcs/class/System.XML/System.Xml/ChangeLog
mcs/class/System.XML/System.Xml/XmlNode.cs
mcs/class/System.XML/Test/System.Xml.XPath/ChangeLog
mcs/class/System.XML/Test/System.Xml.XPath/XPathNavigatorTests.cs
mcs/class/System.XML/Test/System.Xml/ChangeLog
mcs/class/System.XML/Test/System.Xml/XmlNodeTests.cs

index b21d0e774ce11c9b70d26434082f02c6936f6048..ee3ffbabb02683f4633603ca2cf1f39e00c43a63 100644 (file)
@@ -1,3 +1,10 @@
+2006-12-05  Atsushi Enomoto <atsushi@ximian.com>
+
+       * XPathEditableDocument.cs : those writers should lookup prefix when
+         WriteStartElement/WriteStartAttribute are passed null prefix.
+         Removed nodeStack in XmlDocumentInsertionWriter, since nodes are
+         always added to current node immediately.
+
 2006-11-10  Atsushi Enomoto <atsushi@ximian.com>
 
        * XPathNavigatorReader.cs : removed node type restriction on 
index bda7695922584adbb683a4056547c94f871632aa..d7a008ebcfb0aa854710b866330901d4a5339a7b 100644 (file)
@@ -74,7 +74,8 @@ namespace Mono.Xml.XPath
                XmlNode parent;
                XmlNode current;
                XmlNode nextSibling;
-               Stack nodeStack = new Stack ();
+               WriteState state;
+               XmlAttribute attribute;
 
                public XmlDocumentInsertionWriter (XmlNode owner, XmlNode nextSibling)
                {
@@ -96,20 +97,15 @@ namespace Mono.Xml.XPath
                        state = WriteState.Content;
                }
 
-               WriteState state;
-               XmlAttribute attribute;
-
                public override WriteState WriteState {
                        get { return state; }
                }
 
                public override void Close ()
                {
-                       while (nodeStack.Count > 0) {
-                               XmlNode n = nodeStack.Pop () as XmlNode;
-                               n.AppendChild (current);
-                               current = n;
-                       }
+                       while (current.ParentNode != null)
+                               current = current.ParentNode;
+
                        parent.InsertBefore ((XmlDocumentFragment) current, nextSibling);
                        if (Closed != null)
                                Closed (this);
@@ -117,8 +113,6 @@ namespace Mono.Xml.XPath
 
                internal event XmlWriterClosedEventHandler Closed;
 
-               internal XmlNode AppendedFirstChild;
-
                public override void Flush ()
                {
                }
@@ -132,6 +126,8 @@ namespace Mono.Xml.XPath
                {
                        if (state != WriteState.Content)
                                throw new InvalidOperationException ("Current state is not inside element. Cannot start attribute.");
+                       if (prefix == null && ns != null && ns.Length > 0)
+                               prefix = LookupPrefix (ns);
                        attribute = current.OwnerDocument.CreateAttribute (prefix, name, ns);
                        state = WriteState.Attribute;
                }
@@ -156,17 +152,18 @@ namespace Mono.Xml.XPath
 
                public override void WriteStartElement (string prefix, string name, string ns)
                {
+                       if (prefix == null && ns != null && ns.Length > 0)
+                               prefix = LookupPrefix (ns);
                        XmlElement el = current.OwnerDocument.CreateElement (prefix, name, ns);
                        current.AppendChild (el);
-                       nodeStack.Push (current);
                        current = el;
                }
 
                public override void WriteEndElement ()
                {
-                       if (nodeStack.Count == 0)
+                       current = current.ParentNode;
+                       if (current == null)
                                throw new InvalidOperationException ("No element is opened.");
-                       current = nodeStack.Pop () as XmlNode;
                }
 
                public override void WriteFullEndElement ()
@@ -268,6 +265,8 @@ namespace Mono.Xml.XPath
        internal class XmlDocumentAttributeWriter : XmlWriter
        {
                XmlElement element;
+               WriteState state;
+               XmlAttribute attribute;
 
                public XmlDocumentAttributeWriter (XmlNode owner)
                {
@@ -277,9 +276,6 @@ namespace Mono.Xml.XPath
                        state = WriteState.Content;
                }
 
-               WriteState state;
-               XmlAttribute attribute;
-
                public override WriteState WriteState {
                        get { return state; }
                }
@@ -301,6 +297,8 @@ namespace Mono.Xml.XPath
                {
                        if (state != WriteState.Content)
                                throw new InvalidOperationException ("Current state is not inside element. Cannot start attribute.");
+                       if (prefix == null && ns != null && ns.Length > 0)
+                               prefix = LookupPrefix (ns);
                        attribute = element.OwnerDocument.CreateAttribute (prefix, name, ns);
                        state = WriteState.Attribute;
                }
index dc487fe79abf6113ebae24049c7f4df6e1899874..2afff057ca9d5a6a9e362d35f19e46a669c97a84 100644 (file)
@@ -1,3 +1,8 @@
+2006-12-05  Atsushi Enomoto <atsushi@ximian.com>
+
+       * XmlNode.cs : GetPrefixOfNamespace() was not correctly searching
+         ancestors' namespaces.
+
 2006-11-20  Atsushi Enomoto <atsushi@ximian.com>
 
        * XmlTextWriter2.cs : fixed some relationship between
index 1b79d55fbec92ae59bf21b923a0ac1f06b32b0ce..bd6b5cc788023d3a82dac9fc4ff7232d96a833ba 100644 (file)
@@ -415,7 +415,8 @@ namespace System.Xml
                        while (node != null) {
                                if (node.Prefix == prefix)
                                        return node.NamespaceURI;
-                               if (node.Attributes != null) {
+                               if (node.NodeType == XmlNodeType.Element &&
+                                   ((XmlElement) node).HasAttributes) {
                                        int count = node.Attributes.Count;
                                        for (int i = 0; i < count; i++) {
                                                XmlAttribute attr = node.Attributes [i];
@@ -453,13 +454,16 @@ namespace System.Xml
                                break;
                        }
 
-                       while (node != null && node.Attributes != null) {
-                               for (int i = 0; i < Attributes.Count; i++) {
-                                       XmlAttribute attr = Attributes [i];
-                                       if (attr.Prefix == "xmlns" && attr.Value == namespaceURI)
-                                               return attr.LocalName;
-                                       else if (attr.Name == "xmlns" && attr.Value == namespaceURI)
-                                               return String.Empty;
+                       while (node != null) {
+                               if (node.NodeType == XmlNodeType.Element &&
+                                   ((XmlElement) node).HasAttributes) {
+                                       for (int i = 0; i < node.Attributes.Count; i++) {
+                                               XmlAttribute attr = node.Attributes [i];
+                                               if (attr.Prefix == "xmlns" && attr.Value == namespaceURI)
+                                                       return attr.LocalName;
+                                               else if (attr.Name == "xmlns" && attr.Value == namespaceURI)
+                                                       return String.Empty;
+                                       }
                                }
                                node = node.ParentNode;
                        }
index 84fb39e10fc0f30829c3b573415edf9b9ce57acb..0c0cffb1b7cb845c67c1e7b0beee9748dc7ffa79 100644 (file)
@@ -1,3 +1,8 @@
+2006-12-05  Atsushi Enomoto <atsushi@ximian.com>
+
+       * XPathNavigatorTests.cs : added test for XPath navigator editor's
+         LookupPrefix().
+
 2006-11-10  Atsushi Enomoto <atsushi@ximian.com>
 
        * XPathNavigatorTests.cs : added test for bug #79875, and some
index 70527943fbea6e3214a87b7f5b9fdaa008ae5c61..21248578a14f0a9b207414e3b529b838606c9e9c 100644 (file)
@@ -4,9 +4,11 @@
 // Authors:
 //   Jason Diamond <jason@injektilo.org>
 //   Martin Willemoes Hansen <mwh@sysrq.dk>
+//   Atsushi Enomoto <atsushi@ximian.com>
 //
 // (C) 2002 Jason Diamond
 // (C) 2003 Martin Willemoes Hansen
+// (C) 2004-2006 Novell, Inc.
 //
 
 using System;
@@ -592,6 +594,23 @@ namespace MonoTests.System.Xml
                        iter.MoveNext ();
                        AssertEquals ("val&quot;1&#10;&gt;", iter.Current.InnerXml);
                }
+
+               [Test]
+               public void WriterAttributePrefix ()
+               {
+                       XmlDocument doc = new XmlDocument ();
+                       XmlWriter w = doc.CreateNavigator ().AppendChild ();
+                       w.WriteStartElement ("foo");
+                       w.WriteAttributeString ("xmlns", "x", "http://www.w3.org/2000/xmlns/", "urn:foo");
+                       AssertEquals ("#0", "x", w.LookupPrefix ("urn:foo"));
+                       w.WriteStartElement (null, "bar", "urn:foo");
+                       w.WriteAttributeString (null, "ext", "urn:foo", "bah");
+                       w.WriteEndElement ();
+                       w.WriteEndElement ();
+                       w.Close ();
+                       AssertEquals ("#1", "x", doc.FirstChild.FirstChild.Prefix);
+                       AssertEquals ("#2", "x", doc.FirstChild.FirstChild.Attributes [0].Prefix);
+               }
 #endif
        }
 }
index 41a59bc0851ed9e8d493fd31ac78142ea9b2f2db..073bf88842c396d345970375642ad049973150e3 100644 (file)
@@ -1,3 +1,7 @@
+2006-12-05  Atsushi Enomoto <atsushi@ximian.com>
+
+       * XmlNodeTests.cs : added another test for GetPrefixOfNamespace().
+
 2006-11-20  Atsushi Enomoto <atsushi@ximian.com>
 
        * XmlWriterSettingsTests.cs : added tests for relationship between
index 6499419542cb69ec16969e2b940fc4f67a780565..d8ddda4cec1f7d565bc16c7386cb06f871c8e1dd 100644 (file)
@@ -348,6 +348,19 @@ namespace MonoTests.System.Xml
                        AssertEquals (String.Empty, n.GetPrefixOfNamespace ("foo"));
                }
 
+               [Test]
+               public void GetPrefixOfNamespace2 ()
+               {
+                       XmlDocument doc = new XmlDocument ();
+                       doc.AppendChild (doc.CreateElement ("foo"));
+                       doc.DocumentElement.SetAttributeNode (
+                       doc.CreateAttribute ("xmlns", "u", "http://www.w3.org/2000/xmlns/"));
+                       doc.DocumentElement.Attributes [0].Value = "urn:foo";
+                       XmlElement el = doc.CreateElement ("bar");
+                       doc.DocumentElement.AppendChild (el);
+                       AssertEquals ("u", el.GetPrefixOfNamespace ("urn:foo"));
+               }
+
                [Test]
                public void ReplaceChild ()
                {