XmlNamespaceManager.cs 5.0 KB

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