XmlNamespaceManager.cs 4.4 KB

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