XmlNamespaceManager.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. //
  2. // XmlNamespaceManager.cs
  3. //
  4. // Authors:
  5. // Jason Diamond ([email protected])
  6. // Ben Maurer ([email protected])
  7. // Atsushi Enomoto ([email protected])
  8. //
  9. // (C) 2001 Jason Diamond http://injektilo.org/
  10. // (C) 2003 Ben Maurer
  11. // (C) 2004 Novell Inc.
  12. //
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using System.Collections;
  34. #if NET_2_0
  35. using System.Collections.Generic;
  36. #endif
  37. using System.Collections.Specialized;
  38. namespace System.Xml
  39. {
  40. public class XmlNamespaceManager : IXmlNamespaceResolver, IEnumerable
  41. {
  42. #region Data
  43. struct NsDecl {
  44. public string Prefix, Uri;
  45. }
  46. struct NsScope {
  47. public int DeclCount;
  48. public string DefaultNamespace;
  49. }
  50. NsDecl [] decls;
  51. int declPos = -1;
  52. NsScope [] scopes;
  53. int scopePos = -1;
  54. string defaultNamespace;
  55. int count;
  56. void InitData ()
  57. {
  58. decls = new NsDecl [10];
  59. scopes = new NsScope [40];
  60. }
  61. // precondition declPos == nsDecl.Length
  62. void GrowDecls ()
  63. {
  64. NsDecl [] old = decls;
  65. decls = new NsDecl [declPos * 2 + 1];
  66. if (declPos > 0)
  67. Array.Copy (old, 0, decls, 0, declPos);
  68. }
  69. // precondition scopePos == scopes.Length
  70. void GrowScopes ()
  71. {
  72. NsScope [] old = scopes;
  73. scopes = new NsScope [scopePos * 2 + 1];
  74. if (scopePos > 0)
  75. Array.Copy (old, 0, scopes, 0, scopePos);
  76. }
  77. #endregion
  78. #region Fields
  79. private XmlNameTable nameTable;
  80. internal const string XmlnsXml = "http://www.w3.org/XML/1998/namespace";
  81. internal const string XmlnsXmlns = "http://www.w3.org/2000/xmlns/";
  82. internal const string PrefixXml = "xml";
  83. internal const string PrefixXmlns = "xmlns";
  84. #endregion
  85. #region Constructor
  86. public XmlNamespaceManager (XmlNameTable nameTable)
  87. {
  88. this.nameTable = nameTable;
  89. nameTable.Add (PrefixXmlns);
  90. nameTable.Add (PrefixXml);
  91. nameTable.Add (String.Empty);
  92. nameTable.Add (XmlnsXmlns);
  93. nameTable.Add (XmlnsXml);
  94. InitData ();
  95. }
  96. #endregion
  97. #region Properties
  98. public virtual string DefaultNamespace {
  99. get { return defaultNamespace == null ? string.Empty : defaultNamespace; }
  100. }
  101. public XmlNameTable NameTable {
  102. get { return nameTable; }
  103. }
  104. #endregion
  105. #region Methods
  106. public virtual void AddNamespace (string prefix, string uri)
  107. {
  108. AddNamespace (prefix, uri, false);
  109. }
  110. internal virtual void AddNamespace (string prefix, string uri, bool atomizedNames)
  111. {
  112. if (prefix == null)
  113. throw new ArgumentNullException ("prefix", "Value cannot be null.");
  114. if (uri == null)
  115. throw new ArgumentNullException ("uri", "Value cannot be null.");
  116. if (!atomizedNames) {
  117. prefix = nameTable.Add (prefix);
  118. uri = nameTable.Add (uri);
  119. }
  120. IsValidDeclaration (prefix, uri, true);
  121. if (prefix.Length == 0)
  122. defaultNamespace = uri;
  123. for (int i = declPos; i > declPos - count; i--) {
  124. if (object.ReferenceEquals (decls [i].Prefix, prefix)) {
  125. decls [i].Uri = uri;
  126. return;
  127. }
  128. }
  129. declPos ++;
  130. count ++;
  131. if (declPos == decls.Length)
  132. GrowDecls ();
  133. decls [declPos].Prefix = prefix;
  134. decls [declPos].Uri = uri;
  135. }
  136. internal static string IsValidDeclaration (string prefix, string uri, bool throwException)
  137. {
  138. string message = null;
  139. if (prefix == PrefixXml && uri != XmlnsXml)
  140. message = String.Format ("Prefix \"xml\" can only be bound to the fixed namespace URI \"{0}\". \"{1}\" is invalid.", XmlnsXml, uri);
  141. else if (message == null && prefix == "xmlns")
  142. message = "Declaring prefix named \"xmlns\" is not allowed to any namespace.";
  143. else if (message == null && uri == XmlnsXmlns)
  144. message = String.Format ("Namespace URI \"{0}\" cannot be declared with any namespace.", XmlnsXmlns);
  145. if (message != null && throwException)
  146. throw new ArgumentException (message);
  147. else
  148. return message;
  149. }
  150. public virtual IEnumerator GetEnumerator ()
  151. {
  152. // In fact it returns such table's enumerator that contains all the namespaces.
  153. // while HasNamespace() ignores pushed namespaces.
  154. Hashtable ht = new Hashtable ();
  155. for (int i = 0; i <= declPos; i++) {
  156. if (decls [i].Prefix != string.Empty && decls [i].Uri != null) {
  157. ht [decls [i].Prefix] = decls [i].Uri;
  158. }
  159. }
  160. ht [string.Empty] = DefaultNamespace;
  161. ht [PrefixXml] = XmlnsXml;
  162. ht [PrefixXmlns] = XmlnsXmlns;
  163. return ht.Keys.GetEnumerator ();
  164. }
  165. #if NET_2_0
  166. public virtual IDictionary<string, string> GetNamespacesInScope (XmlNamespaceScope scope)
  167. {
  168. IDictionary namespaceTable = GetNamespacesInScopeImpl (scope);
  169. IDictionary<string, string> namespaces = new Dictionary<string, string>(namespaceTable.Count);
  170. foreach (DictionaryEntry entry in namespaceTable) {
  171. namespaces[(string) entry.Key] = (string) entry.Value;
  172. }
  173. return namespaces;
  174. }
  175. #else
  176. IDictionary IXmlNamespaceResolver.GetNamespacesInScope (XmlNamespaceScope scope)
  177. {
  178. return GetNamespacesInScopeImpl (scope);
  179. }
  180. #endif
  181. internal virtual IDictionary GetNamespacesInScopeImpl (XmlNamespaceScope scope)
  182. {
  183. Hashtable table = new Hashtable ();
  184. if (scope == XmlNamespaceScope.Local) {
  185. for (int i = 0; i < count; i++)
  186. if (decls [declPos - i].Prefix == String.Empty && decls [declPos - i].Uri == String.Empty) {
  187. if (table.Contains (String.Empty))
  188. table.Remove (String.Empty);
  189. }
  190. else if (decls [declPos - i].Uri != null)
  191. table.Add (decls [declPos - i].Prefix, decls [declPos - i].Uri);
  192. return table;
  193. } else {
  194. for (int i = 0; i <= declPos; i++) {
  195. if (decls [i].Prefix == String.Empty && decls [i].Uri == String.Empty) {
  196. // removal of default namespace
  197. if (table.Contains (String.Empty))
  198. table.Remove (String.Empty);
  199. }
  200. else if (decls [i].Uri != null)
  201. table [decls [i].Prefix] = decls [i].Uri;
  202. }
  203. if (scope == XmlNamespaceScope.All)
  204. table.Add ("xml", XmlNamespaceManager.XmlnsXml);
  205. return table;
  206. }
  207. }
  208. public virtual bool HasNamespace (string prefix)
  209. {
  210. return HasNamespace (prefix, false);
  211. }
  212. internal virtual bool HasNamespace (string prefix, bool atomizedNames)
  213. {
  214. if (prefix == null || count == 0)
  215. return false;
  216. for (int i = declPos; i > declPos - count; i--) {
  217. if (decls [i].Prefix == prefix)
  218. return true;
  219. }
  220. return false;
  221. }
  222. public virtual string LookupNamespace (string prefix)
  223. {
  224. #if NET_2_0
  225. return LookupNamespace (prefix, false);
  226. #else
  227. return LookupNamespace (prefix, true);
  228. #endif
  229. }
  230. internal virtual string LookupNamespace (string prefix, bool atomizedNames)
  231. {
  232. switch (prefix) {
  233. case PrefixXmlns:
  234. return nameTable.Get (XmlnsXmlns);
  235. case PrefixXml:
  236. return nameTable.Get (XmlnsXml);
  237. case "":
  238. return DefaultNamespace;
  239. case null:
  240. return null;
  241. }
  242. for (int i = declPos; i >= 0; i--) {
  243. if (CompareString (decls [i].Prefix, prefix, atomizedNames) && decls [i].Uri != null /* null == flag for removed */)
  244. return decls [i].Uri;
  245. }
  246. return null;
  247. }
  248. public virtual string LookupPrefix (string uri)
  249. {
  250. #if NET_2_0
  251. return LookupPrefix (uri, false);
  252. #else
  253. return LookupPrefix (uri, true);
  254. #endif
  255. }
  256. private bool CompareString (string s1, string s2, bool atomizedNames)
  257. {
  258. if (atomizedNames)
  259. return object.ReferenceEquals (s1, s2);
  260. else
  261. return s1 == s2;
  262. }
  263. internal string LookupPrefix (string uri, bool atomizedName)
  264. {
  265. if (uri == null)
  266. return null;
  267. if (CompareString (uri, DefaultNamespace, atomizedName))
  268. return string.Empty;
  269. if (CompareString (uri, XmlnsXml, atomizedName))
  270. return PrefixXml;
  271. if (CompareString (uri, XmlnsXmlns, atomizedName))
  272. return PrefixXmlns;
  273. for (int i = declPos; i >= 0; i--) {
  274. if (CompareString (decls [i].Uri, uri, atomizedName) && decls [i].Prefix.Length > 0) // we already looked for ""
  275. return decls [i].Prefix;
  276. }
  277. // ECMA specifies that this method returns String.Empty
  278. // in case of no match. But actually MS.NET returns null.
  279. // For more information,see
  280. // http://lists.ximian.com/archives/public/mono-list/2003-January/005071.html
  281. //return String.Empty;
  282. return null;
  283. }
  284. public virtual bool PopScope ()
  285. {
  286. if (scopePos == -1)
  287. return false;
  288. declPos -= count;
  289. defaultNamespace = scopes [scopePos].DefaultNamespace;
  290. count = scopes [scopePos].DeclCount;
  291. scopePos --;
  292. return true;
  293. }
  294. public virtual void PushScope ()
  295. {
  296. scopePos ++;
  297. if (scopePos == scopes.Length)
  298. GrowScopes ();
  299. scopes [scopePos].DefaultNamespace = defaultNamespace;
  300. scopes [scopePos].DeclCount = count;
  301. count = 0;
  302. }
  303. // It is rarely used, so we don't need NameTable optimization on it.
  304. public virtual void RemoveNamespace (string prefix, string uri)
  305. {
  306. RemoveNamespace (prefix, uri, false);
  307. }
  308. internal virtual void RemoveNamespace (string prefix, string uri, bool atomizedNames)
  309. {
  310. if (prefix == null)
  311. throw new ArgumentNullException ("prefix");
  312. if (uri == null)
  313. throw new ArgumentNullException ("uri");
  314. if (count == 0)
  315. return;
  316. for (int i = declPos; i > declPos - count; i--) {
  317. if (CompareString (decls [i].Prefix, prefix, atomizedNames) && CompareString (decls [i].Uri, uri, atomizedNames))
  318. decls [i].Uri = null;
  319. }
  320. }
  321. #endregion
  322. }
  323. }