XmlNamespaceManager.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. //
  2. // XmlNamespaceManager.cs
  3. //
  4. // Authors:
  5. // Jason Diamond ([email protected])
  6. // Ben Maurer ([email protected])
  7. //
  8. // (C) 2001 Jason Diamond http://injektilo.org/
  9. // (C) 2003 Ben Maurer
  10. //
  11. using System.Collections;
  12. namespace System.Xml
  13. {
  14. public class XmlNamespaceManager : IEnumerable
  15. {
  16. #region Data
  17. struct NsDecl {
  18. public string Prefix, Uri;
  19. }
  20. struct NsScope {
  21. public int DeclCount;
  22. public string DefaultNamespace;
  23. }
  24. NsDecl [] decls;
  25. int declPos = -1;
  26. NsScope [] scopes;
  27. int scopePos = -1;
  28. string defaultNamespace;
  29. int count;
  30. void InitData ()
  31. {
  32. decls = new NsDecl [10];
  33. scopes = new NsScope [40];
  34. }
  35. // precondition declPos == nsDecl.Length
  36. void GrowDecls ()
  37. {
  38. NsDecl [] old = decls;
  39. decls = new NsDecl [declPos * 2 + 1];
  40. if (declPos > 0)
  41. Array.Copy (old, 0, decls, 0, declPos);
  42. }
  43. // precondition scopePos == scopes.Length
  44. void GrowScopes ()
  45. {
  46. NsScope [] old = scopes;
  47. scopes = new NsScope [scopePos * 2 + 1];
  48. if (scopePos > 0)
  49. Array.Copy (old, 0, scopes, 0, scopePos);
  50. }
  51. #endregion
  52. #region Fields
  53. private XmlNameTable nameTable;
  54. internal const string XmlnsXml = "http://www.w3.org/XML/1998/namespace";
  55. internal const string XmlnsXmlns = "http://www.w3.org/2000/xmlns/";
  56. #endregion
  57. #region Constructor
  58. internal XmlNamespaceManager () {}
  59. public XmlNamespaceManager (XmlNameTable nameTable)
  60. {
  61. this.nameTable = nameTable;
  62. nameTable.Add ("xmlns");
  63. nameTable.Add ("xml");
  64. nameTable.Add (String.Empty);
  65. nameTable.Add (XmlnsXmlns);
  66. nameTable.Add (XmlnsXml);
  67. InitData ();
  68. }
  69. #endregion
  70. #region Properties
  71. public virtual string DefaultNamespace {
  72. get { return defaultNamespace == null ? string.Empty : defaultNamespace; }
  73. }
  74. public XmlNameTable NameTable {
  75. get { return nameTable; }
  76. }
  77. #endregion
  78. #region Methods
  79. public virtual void AddNamespace (string prefix, string uri)
  80. {
  81. if (prefix == null)
  82. throw new ArgumentNullException ("prefix", "Value cannot be null.");
  83. if (uri == null)
  84. throw new ArgumentNullException ("uri", "Value cannot be null.");
  85. prefix = nameTable.Add (prefix);
  86. uri = nameTable.Add (uri);
  87. IsValidDeclaration (prefix, uri, true);
  88. if (prefix == string.Empty)
  89. defaultNamespace = uri;
  90. for (int i = declPos; i > declPos - count; i--) {
  91. if (decls [i].Prefix == prefix) {
  92. decls [i].Uri = uri;
  93. return;
  94. }
  95. }
  96. declPos ++;
  97. count ++;
  98. if (declPos == decls.Length)
  99. GrowDecls ();
  100. decls [declPos].Prefix = prefix;
  101. decls [declPos].Uri = uri;
  102. }
  103. internal static string IsValidDeclaration (string prefix, string uri, bool throwException)
  104. {
  105. string message = null;
  106. if (prefix == "xml" && uri != XmlnsXml)
  107. message = String.Format ("Prefix \"xml\" is only allowed to the fixed uri \"{0}\"", XmlnsXml);
  108. else if (uri == XmlnsXml)
  109. message = String.Format ("Namespace URI \"{0}\" can only be declared with the fixed prefix \"xml\"", XmlnsXml);
  110. if (message == null && prefix == "xmlns")
  111. message = "Declaring prefix named \"xmlns\" is not allowed to any namespace.";
  112. if (message == null && uri == XmlnsXmlns)
  113. message = String.Format ("Namespace URI \"{0}\" cannot be declared with any namespace.", XmlnsXmlns);
  114. if (message != null && throwException)
  115. throw new ArgumentException (message);
  116. else
  117. return message;
  118. }
  119. public virtual IEnumerator GetEnumerator ()
  120. {
  121. // In fact it returns such table's enumerator that contains all the namespaces.
  122. // while HasNamespace() ignores pushed namespaces.
  123. Hashtable ht = new Hashtable ();
  124. for (int i = 0; i <= declPos; i++) {
  125. if (decls [i].Prefix != string.Empty && decls [i].Uri != null) {
  126. ht [decls [i].Prefix] = decls [i].Uri;
  127. }
  128. }
  129. ht [string.Empty] = DefaultNamespace;
  130. ht ["xml"] = XmlnsXml;
  131. ht ["xmlns"] = XmlnsXmlns;
  132. return ht.Keys.GetEnumerator ();
  133. }
  134. public virtual bool HasNamespace (string prefix)
  135. {
  136. if (prefix == null || count == 0)
  137. return false;
  138. for (int i = declPos; i > declPos - count; i--) {
  139. if (decls [i].Prefix == prefix)
  140. return true;
  141. }
  142. return false;
  143. }
  144. public virtual string LookupNamespace (string prefix)
  145. {
  146. switch (prefix) {
  147. case "xmlns":
  148. return nameTable.Get (XmlnsXmlns);
  149. case "xml":
  150. return nameTable.Get (XmlnsXml);
  151. case "":
  152. return DefaultNamespace;
  153. }
  154. for (int i = declPos; i >= 0; i--) {
  155. if (decls [i].Prefix == prefix && decls [i].Uri != null /* null == flag for removed */)
  156. return decls [i].Uri;
  157. }
  158. return null;
  159. }
  160. public virtual string LookupPrefix (string uri)
  161. {
  162. if (uri == null)
  163. return null;
  164. if (uri == DefaultNamespace)
  165. return string.Empty;
  166. if (uri == XmlnsXml)
  167. return nameTable.Add ("xml");
  168. if (uri == XmlnsXmlns)
  169. return nameTable.Add ("xmlns");
  170. for (int i = declPos; i >= 0; i--) {
  171. if (decls [i].Uri == uri && decls [i].Prefix != string.Empty) // we already looked for ""
  172. return decls [i].Prefix;
  173. }
  174. // ECMA specifies that this method returns String.Empty
  175. // in case of no match. But actually MS.NET returns null.
  176. // For more information,see
  177. // http://lists.ximian.com/archives/public/mono-list/2003-January/005071.html
  178. //return String.Empty;
  179. return null;
  180. }
  181. public virtual bool PopScope ()
  182. {
  183. if (scopePos == -1)
  184. return false;
  185. declPos -= count;
  186. defaultNamespace = scopes [scopePos].DefaultNamespace;
  187. count = scopes [scopePos].DeclCount;
  188. scopePos --;
  189. return true;
  190. }
  191. public virtual void PushScope ()
  192. {
  193. scopePos ++;
  194. if (scopePos == scopes.Length)
  195. GrowScopes ();
  196. scopes [scopePos].DefaultNamespace = defaultNamespace;
  197. scopes [scopePos].DeclCount = count;
  198. count = 0;
  199. }
  200. public virtual void RemoveNamespace (string prefix, string uri)
  201. {
  202. if (prefix == null)
  203. throw new ArgumentNullException ("prefix");
  204. if (uri == null)
  205. throw new ArgumentNullException ("uri");
  206. if (count == 0)
  207. return;
  208. for (int i = declPos; i > declPos - count; i--) {
  209. if (decls [i].Prefix == prefix && decls [i].Uri == uri)
  210. decls [i].Uri = null;
  211. }
  212. }
  213. #endregion
  214. }
  215. }