XmlNamespaceManager.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. nameTable.Add (prefix);
  51. currentScope.Namespaces [prefix] = nameTable.Add (uri);
  52. }
  53. public virtual IEnumerator GetEnumerator ()
  54. {
  55. if (currentScope.Namespaces == null)
  56. currentScope.Namespaces = new Hashtable ();
  57. return currentScope.Namespaces.Keys.GetEnumerator ();
  58. }
  59. public virtual bool HasNamespace (string prefix)
  60. {
  61. return currentScope != null && currentScope.Namespaces != null && currentScope.Namespaces.Contains (prefix);
  62. }
  63. public virtual string LookupNamespace (string prefix)
  64. {
  65. NamespaceScope scope = currentScope;
  66. while (scope != null) {
  67. if (scope.Namespaces != null && scope.Namespaces.Contains (prefix))
  68. return scope.Namespaces[prefix] as string;
  69. scope = scope.Next;
  70. }
  71. switch (prefix) {
  72. case "xmlns":
  73. return nameTable.Get ("http://www.w3.org/2000/xmlns/");
  74. case "xml":
  75. return nameTable.Get ("http://www.w3.org/XML/1998/namespace");
  76. case "":
  77. return nameTable.Get (String.Empty);
  78. }
  79. return null;
  80. }
  81. public virtual string LookupPrefix (string uri)
  82. {
  83. if (uri == null)
  84. return null;
  85. NamespaceScope scope = currentScope;
  86. while (scope != null)
  87. {
  88. if (scope.Namespaces != null && scope.Namespaces.ContainsValue (uri)) {
  89. foreach (DictionaryEntry entry in scope.Namespaces) {
  90. if (entry.Value.ToString() == uri)
  91. return nameTable.Get (entry.Key as string) as string;
  92. }
  93. }
  94. scope = scope.Next;
  95. }
  96. // ECMA specifies that this method returns String.Empty
  97. // in case of no match. But actually MS.NET returns null.
  98. // For more information,see
  99. // http://lists.ximian.com/archives/public/mono-list/2003-January/005071.html
  100. //return String.Empty;
  101. return null;
  102. }
  103. public virtual bool PopScope ()
  104. {
  105. if (currentScope != null)
  106. currentScope = currentScope.Next;
  107. return currentScope != null;
  108. }
  109. public virtual void PushScope ()
  110. {
  111. NamespaceScope newScope = new NamespaceScope ();
  112. newScope.Next = currentScope;
  113. currentScope = newScope;
  114. }
  115. public virtual void RemoveNamespace (string prefix, string uri)
  116. {
  117. if (prefix == null)
  118. throw new ArgumentNullException ("prefix");
  119. if (uri == null)
  120. throw new ArgumentNullException ("uri");
  121. if (currentScope == null || currentScope.Namespaces == null)
  122. return;
  123. string p = nameTable.Get (prefix);
  124. string u = nameTable.Get (uri);
  125. if (p == null || u == null)
  126. return;
  127. string storedUri = currentScope.Namespaces [p] as string;
  128. if (storedUri == null || storedUri != u)
  129. return;
  130. currentScope.Namespaces.Remove (p);
  131. }
  132. #endregion
  133. }
  134. internal class NamespaceScope
  135. {
  136. internal NamespaceScope Next;
  137. internal Hashtable Namespaces;
  138. }
  139. }