XmlNamespaceManager.cs 7.8 KB

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