XmlNamespaceManager.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. internal const string XmlnsXml = "http://www.w3.org/XML/1998/namespace";
  18. internal const string XmlnsXmlns = "http://www.w3.org/2000/xmlns/";
  19. #endregion
  20. #region Constructor
  21. public XmlNamespaceManager (XmlNameTable nameTable)
  22. {
  23. this.nameTable = nameTable;
  24. nameTable.Add ("xmlns");
  25. nameTable.Add ("xml");
  26. nameTable.Add (String.Empty);
  27. nameTable.Add (XmlnsXmlns);
  28. nameTable.Add (XmlnsXml);
  29. PushScope ();
  30. currentScope.Namespaces = new Hashtable ();
  31. currentScope.Namespaces.Add ("xml", XmlnsXml);
  32. currentScope.Namespaces.Add ("xmlns", XmlnsXmlns);
  33. }
  34. #endregion
  35. #region Properties
  36. public virtual string DefaultNamespace {
  37. get { return LookupNamespace (String.Empty); }
  38. }
  39. public XmlNameTable NameTable {
  40. get { return nameTable; }
  41. }
  42. #endregion
  43. #region Methods
  44. public virtual void AddNamespace (string prefix, string uri)
  45. {
  46. if (prefix == null)
  47. throw new ArgumentNullException ("prefix", "Value cannot be null.");
  48. if (uri == null)
  49. throw new ArgumentNullException ("uri", "Value cannot be null.");
  50. IsValidDeclaration (prefix, uri, true);
  51. if (currentScope.Namespaces == null)
  52. currentScope.Namespaces = new Hashtable ();
  53. if (prefix != String.Empty)
  54. nameTable.Add (prefix);
  55. currentScope.Namespaces [prefix] = nameTable.Add (uri);
  56. }
  57. internal static string IsValidDeclaration (string prefix, string uri, bool throwException)
  58. {
  59. string message = null;
  60. if (prefix == "xml" && uri != XmlnsXml)
  61. message = String.Format ("Prefix \"xml\" is only allowed to the fixed uri \"{0}\"", XmlnsXml);
  62. else if (uri == XmlnsXml)
  63. message = String.Format ("Namespace URI \"{0}\" can only be declared with the fixed prefix \"xml\"", XmlnsXml);
  64. if (message == null && prefix == "xmlns")
  65. message = "Declaring prefix named \"xmlns\" is not allowed to any namespace.";
  66. if (message == null && uri == XmlnsXmlns)
  67. message = String.Format ("Namespace URI \"{0}\" cannot be declared with any namespace.", XmlnsXmlns);
  68. if (message != null && throwException)
  69. throw new ArgumentException (message);
  70. else
  71. return message;
  72. }
  73. public virtual IEnumerator GetEnumerator ()
  74. {
  75. if (currentScope.Namespaces == null)
  76. currentScope.Namespaces = new Hashtable ();
  77. return currentScope.Namespaces.Keys.GetEnumerator ();
  78. }
  79. public virtual bool HasNamespace (string prefix)
  80. {
  81. return currentScope != null && currentScope.Namespaces != null && currentScope.Namespaces.Contains (prefix);
  82. }
  83. public virtual string LookupNamespace (string prefix)
  84. {
  85. NamespaceScope scope = currentScope;
  86. while (scope != null) {
  87. if (scope.Namespaces != null && scope.Namespaces.Contains (prefix))
  88. return scope.Namespaces[prefix] as string;
  89. scope = scope.Next;
  90. }
  91. switch (prefix) {
  92. case "xmlns":
  93. return nameTable.Get (XmlnsXmlns);
  94. case "xml":
  95. return nameTable.Get (XmlnsXml);
  96. case "":
  97. return nameTable.Get (String.Empty);
  98. }
  99. return null;
  100. }
  101. public virtual string LookupPrefix (string uri)
  102. {
  103. if (uri == null)
  104. return null;
  105. NamespaceScope scope = currentScope;
  106. while (scope != null)
  107. {
  108. if (scope.Namespaces != null && scope.Namespaces.ContainsValue (uri)) {
  109. foreach (DictionaryEntry entry in scope.Namespaces) {
  110. if (entry.Value.ToString() == uri)
  111. return nameTable.Get (entry.Key as string) as string;
  112. }
  113. }
  114. scope = scope.Next;
  115. }
  116. // ECMA specifies that this method returns String.Empty
  117. // in case of no match. But actually MS.NET returns null.
  118. // For more information,see
  119. // http://lists.ximian.com/archives/public/mono-list/2003-January/005071.html
  120. //return String.Empty;
  121. return null;
  122. }
  123. public virtual bool PopScope ()
  124. {
  125. if (currentScope != null)
  126. currentScope = currentScope.Next;
  127. return currentScope != null;
  128. }
  129. public virtual void PushScope ()
  130. {
  131. NamespaceScope newScope = new NamespaceScope ();
  132. newScope.Next = currentScope;
  133. currentScope = newScope;
  134. }
  135. public virtual void RemoveNamespace (string prefix, string uri)
  136. {
  137. if (prefix == null)
  138. throw new ArgumentNullException ("prefix");
  139. if (uri == null)
  140. throw new ArgumentNullException ("uri");
  141. if (currentScope == null || currentScope.Namespaces == null)
  142. return;
  143. string p = nameTable.Get (prefix);
  144. string u = nameTable.Get (uri);
  145. if (p == null || u == null)
  146. return;
  147. string storedUri = currentScope.Namespaces [p] as string;
  148. if (storedUri == null || storedUri != u)
  149. return;
  150. currentScope.Namespaces.Remove (p);
  151. }
  152. #endregion
  153. }
  154. internal class NamespaceScope
  155. {
  156. internal NamespaceScope Next;
  157. internal Hashtable Namespaces;
  158. }
  159. }