XmlNamespaceManager.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. HighWaterStack decls = new HighWaterStack (50);
  17. HighWaterStack scopes = new HighWaterStack (50);
  18. Namespace defaultNamespace;
  19. int count = 0;
  20. internal const string XmlnsXml = "http://www.w3.org/XML/1998/namespace";
  21. internal const string XmlnsXmlns = "http://www.w3.org/2000/xmlns/";
  22. string XMLNS, XML, XMLNS_URL, XML_URL;
  23. #endregion
  24. #region Constructor
  25. public XmlNamespaceManager (XmlNameTable nameTable)
  26. {
  27. this.nameTable = nameTable;
  28. XMLNS = nameTable.Add ("xmlns");
  29. XML = nameTable.Add ("xml");
  30. XMLNS_URL = nameTable.Add (XmlnsXmlns);
  31. XML_URL = nameTable.Add (XmlnsXml);
  32. }
  33. #endregion
  34. #region Properties
  35. public virtual string DefaultNamespace {
  36. get { return (defaultNamespace == null) ? String.Empty : defaultNamespace.Uri; }
  37. }
  38. public XmlNameTable NameTable {
  39. get { return nameTable; }
  40. }
  41. #endregion
  42. #region Methods
  43. public virtual void AddNamespace (string prefix, string uri)
  44. {
  45. if (prefix == null)
  46. throw new ArgumentNullException ("prefix", "Value cannot be null.");
  47. if (uri == null)
  48. throw new ArgumentNullException ("uri", "Value cannot be null.");
  49. IsValidDeclaration (prefix, uri, true);
  50. prefix = nameTable.Add (prefix);
  51. uri = nameTable.Add (uri);
  52. // Is it already in the table?
  53. for (int i = decls.Length - 1; i >= decls.Length - count; i--) {
  54. Namespace decl = (Namespace)decls [i];
  55. if (AtomStrEq (decl.Prefix, prefix)) {
  56. // Then redefine it
  57. decl.Uri = uri;
  58. return;
  59. }
  60. }
  61. // Otherwise, we are going to add it as a new object
  62. Namespace newDecl = (Namespace) decls.Push ();
  63. if (newDecl == null) {
  64. newDecl = new Namespace ();
  65. decls.AddToTop (newDecl);
  66. }
  67. newDecl.Prefix = prefix;
  68. newDecl.Uri = uri;
  69. count++;
  70. if (prefix == String.Empty)
  71. defaultNamespace = newDecl;
  72. }
  73. internal static string IsValidDeclaration (string prefix, string uri, bool throwException)
  74. {
  75. string message = null;
  76. if (prefix == "xml" && uri != XmlnsXml)
  77. message = String.Format ("Prefix \"xml\" is only allowed to the fixed uri \"{0}\"", XmlnsXml);
  78. else if (uri == XmlnsXml)
  79. message = String.Format ("Namespace URI \"{0}\" can only be declared with the fixed prefix \"xml\"", XmlnsXml);
  80. if (message == null && prefix == "xmlns")
  81. message = "Declaring prefix named \"xmlns\" is not allowed to any namespace.";
  82. if (message == null && uri == XmlnsXmlns)
  83. message = String.Format ("Namespace URI \"{0}\" cannot be declared with any namespace.", XmlnsXmlns);
  84. if (message != null && throwException)
  85. throw new ArgumentException (message);
  86. else
  87. return message;
  88. }
  89. public virtual IEnumerator GetEnumerator ()
  90. {
  91. Hashtable p = new Hashtable (count);
  92. for (int i = decls.Length - 1; i >= decls.Length - count; i--) {
  93. Namespace decl = (Namespace)decls [i];
  94. if (decl.Prefix != String.Empty && decl.Uri != null)
  95. p [decl.Prefix] = decl.Uri;
  96. }
  97. p [String.Empty] = DefaultNamespace;
  98. p [XML] = XML_URL;
  99. p [XMLNS] = XMLNS_URL;
  100. return p.Keys.GetEnumerator ();
  101. }
  102. public virtual bool HasNamespace (string prefix)
  103. {
  104. if (prefix == null) return false;
  105. for (int i = decls.Length - 1; i >= decls.Length - count; i--) {
  106. Namespace decl = (Namespace)decls [i];
  107. if (AtomStrEq (decl.Prefix, prefix) && decl.Uri != null)
  108. return true;
  109. }
  110. return false;
  111. }
  112. public virtual string LookupNamespace (string prefix)
  113. {
  114. if (prefix == null)
  115. return null;
  116. if (prefix == String.Empty)
  117. return DefaultNamespace;
  118. if (AtomStrEq (XML, prefix))
  119. return XML_URL;
  120. if (AtomStrEq (XMLNS, prefix))
  121. return XMLNS_URL;
  122. for (int i = decls.Length - 1; i >= 0; i--) {
  123. Namespace decl = (Namespace)decls [i];
  124. if (AtomStrEq (decl.Prefix, prefix) && decl.Uri != null)
  125. return decl.Uri;
  126. }
  127. return null;
  128. }
  129. public virtual string LookupPrefix (string uri)
  130. {
  131. if (uri == null)
  132. return null;
  133. if (AtomStrEq (DefaultNamespace, uri))
  134. return String.Empty;
  135. if (AtomStrEq (XML_URL, uri))
  136. return XML;
  137. if (AtomStrEq (XMLNS_URL, uri))
  138. return XMLNS;
  139. for (int i = decls.Length - 1; i >= 0; i--) {
  140. Namespace decl = (Namespace)decls [i];
  141. if (AtomStrEq (decl.Uri, uri) && decl.Uri != null)
  142. return decl.Prefix;
  143. }
  144. // ECMA specifies that this method returns String.Empty
  145. // in case of no match. But actually MS.NET returns null.
  146. // For more information,see
  147. // http://lists.ximian.com/archives/public/mono-list/2003-January/005071.html
  148. //return String.Empty;
  149. return null;
  150. }
  151. public virtual bool PopScope ()
  152. {
  153. Scope current = (Scope)scopes.Pop ();
  154. if (current == null) {
  155. return false;
  156. } else {
  157. for (int i = 0; i < count; i++)
  158. decls.Pop ();
  159. defaultNamespace = current.DefaultNamespace;
  160. count = current.Count;
  161. return true;
  162. }
  163. }
  164. public virtual void PushScope ()
  165. {
  166. Scope current = (Scope)scopes.Push ();
  167. if (current == null) {
  168. current = new Scope ();
  169. scopes.AddToTop (current);
  170. }
  171. current.DefaultNamespace = defaultNamespace;
  172. current.Count = count;
  173. count = 0;
  174. }
  175. public virtual void RemoveNamespace (string prefix, string uri)
  176. {
  177. if (prefix == null)
  178. throw new ArgumentNullException ("prefix");
  179. if (uri == null)
  180. throw new ArgumentNullException ("uri");
  181. string p = nameTable.Get (prefix);
  182. string u = nameTable.Get (uri);
  183. if (p == null || u == null)
  184. return;
  185. for (int i = decls.Length - 1; i >= decls.Length - count; i--) {
  186. Namespace n = (Namespace)decls [i];
  187. if (AtomStrEq (n.Prefix, p) && AtomStrEq (n.Uri, u))
  188. n.Uri = null;
  189. }
  190. }
  191. bool AtomStrEq (string a, string b) {
  192. if (String.Equals (a, b) && !Object.ReferenceEquals (a, b)) {
  193. Console.Error.WriteLine ("WARNING: {0} not interned", a);
  194. }
  195. return String.Equals (a, b);
  196. }
  197. #endregion
  198. class Namespace {
  199. public string Prefix, Uri;
  200. }
  201. class Scope {
  202. public Namespace DefaultNamespace;
  203. public int Count;
  204. }
  205. }
  206. }