New test.
[mono.git] / mcs / class / System.XML / System.Xml / XmlNamespaceManager.cs
index 8cfa80974f286eb7990d597c97902b6bf1494904..50bb0401b5f37be721d0bd707e4b6bdcfff00530 100644 (file)
@@ -4,17 +4,43 @@
 // Authors:
 //   Jason Diamond (jason@injektilo.org)
 //   Ben Maurer (bmaurer@users.sourceforge.net)
+//   Atsushi Enomoto (atsushi@ximian.com)
 //
 // (C) 2001 Jason Diamond  http://injektilo.org/
 // (C) 2003 Ben Maurer
+// (C) 2004 Novell Inc.
+//
+
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
 using System.Collections;
+#if NET_2_0
+using System.Collections.Generic;
+#endif
 using System.Collections.Specialized;
 
 namespace System.Xml
 {
-       public class XmlNamespaceManager : IEnumerable
+       public class XmlNamespaceManager : IXmlNamespaceResolver, IEnumerable
        {
                #region Data
                struct NsDecl {
@@ -94,7 +120,11 @@ namespace System.Xml
                        get { return defaultNamespace == null ? string.Empty : defaultNamespace; }
                }
 
+#if NET_2_0
+               public virtual XmlNameTable NameTable {
+#else
                public XmlNameTable NameTable {
+#endif
                        get { return nameTable; }
                }
 
@@ -107,11 +137,7 @@ namespace System.Xml
                        AddNamespace (prefix, uri, false);
                }
 
-#if NET_2_0
-               public virtual void AddNamespace (string prefix, string uri, bool atomizedNames)
-#else
                internal virtual void AddNamespace (string prefix, string uri, bool atomizedNames)
-#endif
                {
                        if (prefix == null)
                                throw new ArgumentNullException ("prefix", "Value cannot be null.");
@@ -144,16 +170,16 @@ namespace System.Xml
                        decls [declPos].Uri = uri;
                }
 
-               internal static string IsValidDeclaration (string prefix, string uri, bool throwException)
+               static string IsValidDeclaration (string prefix, string uri, bool throwException)
                {
                        string message = null;
+                       // It is funky, but it does not check whether prefix
+                       // is equivalent to "xml" in case-insensitive means.
                        if (prefix == PrefixXml && uri != XmlnsXml)
-                               message = String.Format ("Prefix \"xml\" is only allowed to the fixed uri \"{0}\"", XmlnsXml);
-                       else if (uri == XmlnsXml)
-                               message = String.Format ("Namespace URI \"{0}\" is reserved to be mapped to \"xml\" and cannot be declared.", XmlnsXml);
-                       if (message == null && prefix == "xmlns")
+                               message = String.Format ("Prefix \"xml\" can only be bound to the fixed namespace URI \"{0}\". \"{1}\" is invalid.", XmlnsXml, uri);
+                       else if (message == null && prefix == "xmlns")
                                message = "Declaring prefix named \"xmlns\" is not allowed to any namespace.";
-                       if (message == null && uri == XmlnsXmlns)
+                       else if (message == null && uri == XmlnsXmlns)
                                message = String.Format ("Namespace URI \"{0}\" cannot be declared with any namespace.", XmlnsXmlns);
                        if (message != null && throwException)
                                throw new ArgumentException (message);
@@ -179,15 +205,61 @@ namespace System.Xml
                        
                        return ht.Keys.GetEnumerator ();
                }
+
 #if NET_2_0
-               [MonoTODO]
-               public virtual StringDictionary GetNamespacesInScope (XmlNamespaceScope scope)
+               public virtual IDictionary<string, string> GetNamespacesInScope (XmlNamespaceScope scope)
                {
-                       throw new NotImplementedException ();
+                       IDictionary namespaceTable = GetNamespacesInScopeImpl (scope);
+                       IDictionary<string, string> namespaces = new Dictionary<string, string>(namespaceTable.Count);
+
+                       foreach (DictionaryEntry entry in namespaceTable) {
+                               namespaces[(string) entry.Key] = (string) entry.Value;
+                       }
+                       return namespaces;
+               }
+#else
+               IDictionary IXmlNamespaceResolver.GetNamespacesInScope (XmlNamespaceScope scope)
+               {
+                       return GetNamespacesInScopeImpl (scope);
                }
 #endif
 
+               internal virtual IDictionary GetNamespacesInScopeImpl (XmlNamespaceScope scope)
+               {
+                       Hashtable table = new Hashtable ();
+
+                       if (scope == XmlNamespaceScope.Local) {
+                               for (int i = 0; i < count; i++)
+                                       if (decls [declPos - i].Prefix == String.Empty && decls [declPos - i].Uri == String.Empty) {
+                                               if (table.Contains (String.Empty))
+                                                       table.Remove (String.Empty);
+                                       }
+                                       else if (decls [declPos - i].Uri != null)
+                                               table.Add (decls [declPos - i].Prefix, decls [declPos - i].Uri);
+                               return table;
+                       } else {
+                               for (int i = 0; i <= declPos; i++) {
+                                       if (decls [i].Prefix == String.Empty && decls [i].Uri == String.Empty) {
+                                               // removal of default namespace
+                                               if (table.Contains (String.Empty))
+                                                       table.Remove (String.Empty);
+                                       }
+                                       else if (decls [i].Uri != null)
+                                               table [decls [i].Prefix] = decls [i].Uri;
+                               }
+
+                               if (scope == XmlNamespaceScope.All)
+                                       table.Add ("xml", XmlNamespaceManager.XmlnsXml);
+                               return table;
+                       }
+               }
+
                public virtual bool HasNamespace (string prefix)
+               {
+                       return HasNamespace (prefix, false);
+               }
+
+               internal virtual bool HasNamespace (string prefix, bool atomizedNames)
                {
                        if (prefix == null || count == 0)
                                return false;
@@ -202,14 +274,14 @@ namespace System.Xml
 
                public virtual string LookupNamespace (string prefix)
                {
-                       return LookupNamespace (prefix, true);
-               }
-
 #if NET_2_0
-               public virtual string LookupNamespace (string prefix, bool atomizedName)
+                       return LookupNamespace (prefix, false);
 #else
-               internal virtual string LookupNamespace (string prefix, bool atomizedName)
+                       return LookupNamespace (prefix, true);
 #endif
+               }
+
+               internal virtual string LookupNamespace (string prefix, bool atomizedNames)
                {
                        switch (prefix) {
                        case PrefixXmlns:
@@ -223,7 +295,7 @@ namespace System.Xml
                        }
 
                        for (int i = declPos; i >= 0; i--) {
-                               if (CompareString (decls [i].Prefix, prefix, atomizedName) && decls [i].Uri != null /* null == flag for removed */)
+                               if (CompareString (decls [i].Prefix, prefix, atomizedNames) && decls [i].Uri != null /* null == flag for removed */)
                                        return decls [i].Uri;
                        }
                        
@@ -232,7 +304,11 @@ namespace System.Xml
 
                public virtual string LookupPrefix (string uri)
                {
+#if NET_2_0
+                       return LookupPrefix (uri, false);
+#else
                        return LookupPrefix (uri, true);
+#endif
                }
 
                private bool CompareString (string s1, string s2, bool atomizedNames)
@@ -243,11 +319,17 @@ namespace System.Xml
                                return s1 == s2;
                }
 
-#if NET_2_0
-               public string LookupPrefix (string uri, bool atomizedName)
-#else
                internal string LookupPrefix (string uri, bool atomizedName)
-#endif
+               {
+                       return LookupPrefixCore (uri, atomizedName, false);
+               }
+
+               internal string LookupPrefixExclusive (string uri, bool atomizedName)
+               {
+                       return LookupPrefixCore (uri, atomizedName, true);
+               }
+
+               string LookupPrefixCore (string uri, bool atomizedName, bool excludeOverriden)
                {
                        if (uri == null)
                                return null;
@@ -263,7 +345,8 @@ namespace System.Xml
 
                        for (int i = declPos; i >= 0; i--) {
                                if (CompareString (decls [i].Uri, uri, atomizedName) && decls [i].Prefix.Length > 0) // we already looked for ""
-                                       return decls [i].Prefix;
+                                       if (!excludeOverriden || !IsOverriden (i))
+                                               return decls [i].Prefix;
                        }
 
                        // ECMA specifies that this method returns String.Empty
@@ -274,6 +357,17 @@ namespace System.Xml
                        return null;
                }
 
+               bool IsOverriden (int idx)
+               {
+                       if (idx == declPos)
+                               return false;
+                       string prefix = decls [idx + 1].Prefix;
+                       for (int i = idx + 1; i <= declPos; i++)
+                               if ((object) decls [idx].Prefix == prefix)
+                                       return true;
+                       return false;
+               }
+
                public virtual bool PopScope ()
                {
                        if (scopePos == -1)
@@ -303,11 +397,7 @@ namespace System.Xml
                        RemoveNamespace (prefix, uri, false);
                }
 
-#if NET_2_0
-               public virtual void RemoveNamespace (string prefix, string uri, bool atomizedNames)
-#else
                internal virtual void RemoveNamespace (string prefix, string uri, bool atomizedNames)
-#endif
                {
                        if (prefix == null)
                                throw new ArgumentNullException ("prefix");