XmlNamespaceManager.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. using System.Collections.Specialized;
  13. namespace System.Xml
  14. {
  15. public class XmlNamespaceManager : IEnumerable
  16. {
  17. #region Data
  18. struct NsDecl {
  19. public string Prefix, Uri;
  20. }
  21. struct NsScope {
  22. public int DeclCount;
  23. public string DefaultNamespace;
  24. }
  25. NsDecl [] decls;
  26. int declPos = -1;
  27. NsScope [] scopes;
  28. int scopePos = -1;
  29. string defaultNamespace;
  30. int count;
  31. void InitData ()
  32. {
  33. decls = new NsDecl [10];
  34. scopes = new NsScope [40];
  35. }
  36. // precondition declPos == nsDecl.Length
  37. void GrowDecls ()
  38. {
  39. NsDecl [] old = decls;
  40. decls = new NsDecl [declPos * 2 + 1];
  41. if (declPos > 0)
  42. Array.Copy (old, 0, decls, 0, declPos);
  43. }
  44. // precondition scopePos == scopes.Length
  45. void GrowScopes ()
  46. {
  47. NsScope [] old = scopes;
  48. scopes = new NsScope [scopePos * 2 + 1];
  49. if (scopePos > 0)
  50. Array.Copy (old, 0, scopes, 0, scopePos);
  51. }
  52. #endregion
  53. #region Fields
  54. private XmlNameTable nameTable;
  55. internal const string XmlnsXml = "http://www.w3.org/XML/1998/namespace";
  56. internal const string XmlnsXmlns = "http://www.w3.org/2000/xmlns/";
  57. internal const string PrefixXml = "xml";
  58. internal const string PrefixXmlns = "xmlns";
  59. #endregion
  60. #region Constructor
  61. public XmlNamespaceManager (XmlNameTable nameTable)
  62. {
  63. this.nameTable = nameTable;
  64. nameTable.Add (PrefixXmlns);
  65. nameTable.Add (PrefixXml);
  66. nameTable.Add (String.Empty);
  67. nameTable.Add (XmlnsXmlns);
  68. nameTable.Add (XmlnsXml);
  69. InitData ();
  70. }
  71. #endregion
  72. #region Properties
  73. public virtual string DefaultNamespace {
  74. get { return defaultNamespace == null ? string.Empty : defaultNamespace; }
  75. }
  76. public XmlNameTable NameTable {
  77. get { return nameTable; }
  78. }
  79. #endregion
  80. #region Methods
  81. public virtual void AddNamespace (string prefix, string uri)
  82. {
  83. AddNamespace (prefix, uri, false);
  84. }
  85. #if NET_2_0
  86. public virtual void AddNamespace (string prefix, string uri, bool atomizedNames)
  87. #else
  88. internal virtual void AddNamespace (string prefix, string uri, bool atomizedNames)
  89. #endif
  90. {
  91. if (prefix == null)
  92. throw new ArgumentNullException ("prefix", "Value cannot be null.");
  93. if (uri == null)
  94. throw new ArgumentNullException ("uri", "Value cannot be null.");
  95. if (!atomizedNames) {
  96. prefix = nameTable.Add (prefix);
  97. uri = nameTable.Add (uri);
  98. }
  99. IsValidDeclaration (prefix, uri, true);
  100. if (prefix.Length == 0)
  101. defaultNamespace = uri;
  102. for (int i = declPos; i > declPos - count; i--) {
  103. if (object.ReferenceEquals (decls [i].Prefix, prefix)) {
  104. decls [i].Uri = uri;
  105. return;
  106. }
  107. }
  108. declPos ++;
  109. count ++;
  110. if (declPos == decls.Length)
  111. GrowDecls ();
  112. decls [declPos].Prefix = prefix;
  113. decls [declPos].Uri = uri;
  114. }
  115. internal static string IsValidDeclaration (string prefix, string uri, bool throwException)
  116. {
  117. string message = null;
  118. if (prefix == PrefixXml && uri != XmlnsXml)
  119. message = String.Format ("Prefix \"xml\" is only allowed to the fixed uri \"{0}\"", XmlnsXml);
  120. else if (uri == XmlnsXml)
  121. message = String.Format ("Namespace URI \"{0}\" can only be declared with the fixed prefix \"xml\"", XmlnsXml);
  122. if (message == null && prefix == "xmlns")
  123. message = "Declaring prefix named \"xmlns\" is not allowed to any namespace.";
  124. if (message == null && uri == XmlnsXmlns)
  125. message = String.Format ("Namespace URI \"{0}\" cannot be declared with any namespace.", XmlnsXmlns);
  126. if (message != null && throwException)
  127. throw new ArgumentException (message);
  128. else
  129. return message;
  130. }
  131. public virtual IEnumerator GetEnumerator ()
  132. {
  133. // In fact it returns such table's enumerator that contains all the namespaces.
  134. // while HasNamespace() ignores pushed namespaces.
  135. Hashtable ht = new Hashtable ();
  136. for (int i = 0; i <= declPos; i++) {
  137. if (decls [i].Prefix != string.Empty && decls [i].Uri != null) {
  138. ht [decls [i].Prefix] = decls [i].Uri;
  139. }
  140. }
  141. ht [string.Empty] = DefaultNamespace;
  142. ht [PrefixXml] = XmlnsXml;
  143. ht [PrefixXmlns] = XmlnsXmlns;
  144. return ht.Keys.GetEnumerator ();
  145. }
  146. #if NET_2_0
  147. [MonoTODO]
  148. public virtual StringDictionary GetNamespacesInScope (XmlNamespaceScope scope)
  149. {
  150. throw new NotImplementedException ();
  151. }
  152. #endif
  153. public virtual bool HasNamespace (string prefix)
  154. {
  155. if (prefix == null || count == 0)
  156. return false;
  157. for (int i = declPos; i > declPos - count; i--) {
  158. if (decls [i].Prefix == prefix)
  159. return true;
  160. }
  161. return false;
  162. }
  163. public virtual string LookupNamespace (string prefix)
  164. {
  165. return LookupNamespace (prefix, true);
  166. }
  167. #if NET_2_0
  168. public virtual string LookupNamespace (string prefix, bool atomizedName)
  169. #else
  170. internal virtual string LookupNamespace (string prefix, bool atomizedName)
  171. #endif
  172. {
  173. switch (prefix) {
  174. case PrefixXmlns:
  175. return nameTable.Get (XmlnsXmlns);
  176. case PrefixXml:
  177. return nameTable.Get (XmlnsXml);
  178. case "":
  179. return DefaultNamespace;
  180. case null:
  181. return null;
  182. }
  183. for (int i = declPos; i >= 0; i--) {
  184. if (CompareString (decls [i].Prefix, prefix, atomizedName) && decls [i].Uri != null /* null == flag for removed */)
  185. return decls [i].Uri;
  186. }
  187. return null;
  188. }
  189. public virtual string LookupPrefix (string uri)
  190. {
  191. return LookupPrefix (uri, true);
  192. }
  193. private bool CompareString (string s1, string s2, bool atomizedNames)
  194. {
  195. if (atomizedNames)
  196. return object.ReferenceEquals (s1, s2);
  197. else
  198. return s1 == s2;
  199. }
  200. #if NET_2_0
  201. public string LookupPrefix (string uri, bool atomizedName)
  202. #else
  203. internal string LookupPrefix (string uri, bool atomizedName)
  204. #endif
  205. {
  206. if (uri == null)
  207. return null;
  208. if (CompareString (uri, DefaultNamespace, atomizedName))
  209. return string.Empty;
  210. if (CompareString (uri, XmlnsXml, atomizedName))
  211. return PrefixXml;
  212. if (CompareString (uri, XmlnsXmlns, atomizedName))
  213. return PrefixXmlns;
  214. for (int i = declPos; i >= 0; i--) {
  215. if (CompareString (decls [i].Uri, uri, atomizedName) && decls [i].Prefix.Length > 0) // we already looked for ""
  216. return decls [i].Prefix;
  217. }
  218. // ECMA specifies that this method returns String.Empty
  219. // in case of no match. But actually MS.NET returns null.
  220. // For more information,see
  221. // http://lists.ximian.com/archives/public/mono-list/2003-January/005071.html
  222. //return String.Empty;
  223. return null;
  224. }
  225. public virtual bool PopScope ()
  226. {
  227. if (scopePos == -1)
  228. return false;
  229. declPos -= count;
  230. defaultNamespace = scopes [scopePos].DefaultNamespace;
  231. count = scopes [scopePos].DeclCount;
  232. scopePos --;
  233. return true;
  234. }
  235. public virtual void PushScope ()
  236. {
  237. scopePos ++;
  238. if (scopePos == scopes.Length)
  239. GrowScopes ();
  240. scopes [scopePos].DefaultNamespace = defaultNamespace;
  241. scopes [scopePos].DeclCount = count;
  242. count = 0;
  243. }
  244. // It is rarely used, so we don't need NameTable optimization on it.
  245. public virtual void RemoveNamespace (string prefix, string uri)
  246. {
  247. RemoveNamespace (prefix, uri, false);
  248. }
  249. #if NET_2_0
  250. public virtual void RemoveNamespace (string prefix, string uri, bool atomizedNames)
  251. #else
  252. internal virtual void RemoveNamespace (string prefix, string uri, bool atomizedNames)
  253. #endif
  254. {
  255. if (prefix == null)
  256. throw new ArgumentNullException ("prefix");
  257. if (uri == null)
  258. throw new ArgumentNullException ("uri");
  259. if (count == 0)
  260. return;
  261. for (int i = declPos; i > declPos - count; i--) {
  262. if (CompareString (decls [i].Prefix, prefix, atomizedNames) && CompareString (decls [i].Uri, uri, atomizedNames))
  263. decls [i].Uri = null;
  264. }
  265. }
  266. #endregion
  267. }
  268. }