XmlNamespaceManager.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //
  2. // XmlNamespaceManager.cs
  3. //
  4. // Author:
  5. // Jason Diamond ([email protected])
  6. //
  7. // (C) 2001 Jason Diamond http://injektilo.org/
  8. //
  9. using System.Collections;
  10. namespace System.Xml
  11. {
  12. public class XmlNamespaceManager : IEnumerable
  13. {
  14. #region Fields
  15. private XmlNameTable nameTable;
  16. private NamespaceScope currentScope;
  17. #endregion
  18. #region Constructor
  19. public XmlNamespaceManager (XmlNameTable nameTable)
  20. {
  21. this.nameTable = nameTable;
  22. nameTable.Add ("xmlns");
  23. nameTable.Add ("xml");
  24. nameTable.Add (String.Empty);
  25. nameTable.Add ("http://www.w3.org/2000/xmlns/");
  26. nameTable.Add ("http://www.w3.org/XML/1998/namespace");
  27. PushScope ();
  28. }
  29. #endregion
  30. #region Properties
  31. public virtual string DefaultNamespace {
  32. get { return LookupNamespace (String.Empty); }
  33. }
  34. public XmlNameTable NameTable {
  35. get { return nameTable; }
  36. }
  37. #endregion
  38. #region Methods
  39. public virtual void AddNamespace (string prefix, string uri)
  40. {
  41. if (prefix == null)
  42. throw new ArgumentNullException ("prefix", "Value cannot be null.");
  43. if (uri == null)
  44. throw new ArgumentNullException ("uri", "Value cannot be null.");
  45. if (prefix.Length > 2 && prefix.Substring (0, 3).ToLower () == "xml")
  46. throw new ArgumentException ( "Prefixes beginning with \"xml\" (regardless " + "of whether the characters are uppercase, lowercase, or some combination thereof) are reserved for use by XML.", "prefix");
  47. if (currentScope.Namespaces == null)
  48. currentScope.Namespaces = new Hashtable ();
  49. if (prefix == String.Empty)
  50. currentScope.Namespaces [prefix] = nameTable.Add (uri);
  51. else
  52. currentScope.Namespaces.Add (nameTable.Add (prefix), nameTable.Add (uri));
  53. }
  54. public virtual IEnumerator GetEnumerator ()
  55. {
  56. if (currentScope.Namespaces == null)
  57. currentScope.Namespaces = new Hashtable ();
  58. return currentScope.Namespaces.Keys.GetEnumerator ();
  59. }
  60. public virtual bool HasNamespace (string prefix)
  61. {
  62. return currentScope != null && currentScope.Namespaces != null && currentScope.Namespaces.Contains (prefix);
  63. }
  64. public virtual string LookupNamespace (string prefix)
  65. {
  66. NamespaceScope scope = currentScope;
  67. while (scope != null) {
  68. if (scope.Namespaces != null && scope.Namespaces.Contains (prefix))
  69. return scope.Namespaces[prefix] as string;
  70. scope = scope.Next;
  71. }
  72. switch (prefix) {
  73. case "xmlns":
  74. return nameTable.Get ("http://www.w3.org/2000/xmlns/");
  75. case "xml":
  76. return nameTable.Get ("http://www.w3.org/XML/1998/namespace");
  77. case "":
  78. return nameTable.Get (String.Empty);
  79. }
  80. return null;
  81. }
  82. public virtual string LookupPrefix (string uri)
  83. {
  84. if (uri == null)
  85. return null;
  86. NamespaceScope scope = currentScope;
  87. while (scope != null)
  88. {
  89. if (scope.Namespaces != null && scope.Namespaces.ContainsValue (uri)) {
  90. foreach (DictionaryEntry entry in scope.Namespaces) {
  91. if (entry.Value.ToString() == uri)
  92. return nameTable.Get (entry.Key as string) as string;
  93. }
  94. }
  95. scope = scope.Next;
  96. }
  97. // ECMA specifies that this method returns String.Empty
  98. // in case of no match. But actually MS.NET returns null.
  99. // For more information,see
  100. // http://lists.ximian.com/archives/public/mono-list/2003-January/005071.html
  101. //return String.Empty;
  102. return null;
  103. }
  104. public virtual bool PopScope ()
  105. {
  106. if (currentScope != null)
  107. currentScope = currentScope.Next;
  108. return currentScope != null;
  109. }
  110. public virtual void PushScope ()
  111. {
  112. NamespaceScope newScope = new NamespaceScope ();
  113. newScope.Next = currentScope;
  114. currentScope = newScope;
  115. }
  116. public virtual void RemoveNamespace (string prefix, string uri)
  117. {
  118. if (prefix == null)
  119. throw new ArgumentNullException ("prefix");
  120. if (uri == null)
  121. throw new ArgumentNullException ("uri");
  122. if (currentScope == null || currentScope.Namespaces == null)
  123. return;
  124. string p = nameTable.Get (prefix);
  125. string u = nameTable.Get (uri);
  126. if (p == null || u == null)
  127. return;
  128. string storedUri = currentScope.Namespaces [p] as string;
  129. if (storedUri == null || storedUri != u)
  130. return;
  131. currentScope.Namespaces.Remove (p);
  132. }
  133. #endregion
  134. }
  135. internal class NamespaceScope
  136. {
  137. internal NamespaceScope Next;
  138. internal Hashtable Namespaces;
  139. }
  140. }