XmlNamespaceManager.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. internal bool internalAtomizedNames;
  85. #endregion
  86. #region Constructor
  87. public XmlNamespaceManager (XmlNameTable nameTable)
  88. {
  89. if (nameTable == null)
  90. throw new ArgumentNullException ("nameTable");
  91. this.nameTable = nameTable;
  92. nameTable.Add (PrefixXmlns);
  93. nameTable.Add (PrefixXml);
  94. nameTable.Add (String.Empty);
  95. nameTable.Add (XmlnsXmlns);
  96. nameTable.Add (XmlnsXml);
  97. InitData ();
  98. }
  99. #endregion
  100. #region Properties
  101. public virtual string DefaultNamespace {
  102. get { return defaultNamespace == null ? string.Empty : defaultNamespace; }
  103. }
  104. #if NET_2_0
  105. public virtual XmlNameTable NameTable {
  106. #else
  107. public XmlNameTable NameTable {
  108. #endif
  109. get { return nameTable; }
  110. }
  111. #endregion
  112. #region Methods
  113. public virtual void AddNamespace (string prefix, string uri)
  114. {
  115. AddNamespace (prefix, uri, false);
  116. }
  117. void AddNamespace (string prefix, string uri, bool atomizedNames)
  118. {
  119. if (prefix == null)
  120. throw new ArgumentNullException ("prefix", "Value cannot be null.");
  121. if (uri == null)
  122. throw new ArgumentNullException ("uri", "Value cannot be null.");
  123. if (!atomizedNames) {
  124. prefix = nameTable.Add (prefix);
  125. uri = nameTable.Add (uri);
  126. }
  127. if (prefix == PrefixXml && uri == XmlnsXml)
  128. return;
  129. IsValidDeclaration (prefix, uri, true);
  130. if (prefix.Length == 0)
  131. defaultNamespace = uri;
  132. for (int i = declPos; i > declPos - count; i--) {
  133. if (object.ReferenceEquals (decls [i].Prefix, prefix)) {
  134. decls [i].Uri = uri;
  135. return;
  136. }
  137. }
  138. declPos ++;
  139. count ++;
  140. if (declPos == decls.Length)
  141. GrowDecls ();
  142. decls [declPos].Prefix = prefix;
  143. decls [declPos].Uri = uri;
  144. }
  145. static string IsValidDeclaration (string prefix, string uri, bool throwException)
  146. {
  147. string message = null;
  148. // It is funky, but it does not check whether prefix
  149. // is equivalent to "xml" in case-insensitive means.
  150. if (prefix == PrefixXml && uri != XmlnsXml)
  151. message = String.Format ("Prefix \"xml\" can only be bound to the fixed namespace URI \"{0}\". \"{1}\" is invalid.", XmlnsXml, uri);
  152. else if (message == null && prefix == "xmlns")
  153. message = "Declaring prefix named \"xmlns\" is not allowed to any namespace.";
  154. else if (message == null && uri == XmlnsXmlns)
  155. message = String.Format ("Namespace URI \"{0}\" cannot be declared with any namespace.", XmlnsXmlns);
  156. if (message != null && throwException)
  157. throw new ArgumentException (message);
  158. else
  159. return message;
  160. }
  161. public virtual IEnumerator GetEnumerator ()
  162. {
  163. // In fact it returns such table's enumerator that contains all the namespaces.
  164. // while HasNamespace() ignores pushed namespaces.
  165. Hashtable ht = new Hashtable ();
  166. for (int i = 0; i <= declPos; i++) {
  167. if (decls [i].Prefix != string.Empty && decls [i].Uri != null) {
  168. ht [decls [i].Prefix] = decls [i].Uri;
  169. }
  170. }
  171. ht [string.Empty] = DefaultNamespace;
  172. ht [PrefixXml] = XmlnsXml;
  173. ht [PrefixXmlns] = XmlnsXmlns;
  174. return ht.Keys.GetEnumerator ();
  175. }
  176. #if NET_2_0
  177. public virtual IDictionary<string, string> GetNamespacesInScope (XmlNamespaceScope scope)
  178. {
  179. IDictionary namespaceTable = GetNamespacesInScopeImpl (scope);
  180. IDictionary<string, string> namespaces = new Dictionary<string, string>(namespaceTable.Count);
  181. foreach (DictionaryEntry entry in namespaceTable) {
  182. namespaces[(string) entry.Key] = (string) entry.Value;
  183. }
  184. return namespaces;
  185. }
  186. #else
  187. IDictionary IXmlNamespaceResolver.GetNamespacesInScope (XmlNamespaceScope scope)
  188. {
  189. return GetNamespacesInScopeImpl (scope);
  190. }
  191. #endif
  192. internal virtual IDictionary GetNamespacesInScopeImpl (XmlNamespaceScope scope)
  193. {
  194. Hashtable table = new Hashtable ();
  195. if (scope == XmlNamespaceScope.Local) {
  196. for (int i = 0; i < count; i++)
  197. if (decls [declPos - i].Prefix == String.Empty && decls [declPos - i].Uri == String.Empty) {
  198. if (table.Contains (String.Empty))
  199. table.Remove (String.Empty);
  200. }
  201. else if (decls [declPos - i].Uri != null)
  202. table.Add (decls [declPos - i].Prefix, decls [declPos - i].Uri);
  203. return table;
  204. } else {
  205. for (int i = 0; i <= declPos; i++) {
  206. if (decls [i].Prefix == String.Empty && decls [i].Uri == String.Empty) {
  207. // removal of default namespace
  208. if (table.Contains (String.Empty))
  209. table.Remove (String.Empty);
  210. }
  211. else if (decls [i].Uri != null)
  212. table [decls [i].Prefix] = decls [i].Uri;
  213. }
  214. if (scope == XmlNamespaceScope.All)
  215. table.Add ("xml", XmlNamespaceManager.XmlnsXml);
  216. return table;
  217. }
  218. }
  219. public virtual bool HasNamespace (string prefix)
  220. {
  221. return HasNamespace (prefix, false);
  222. }
  223. bool HasNamespace (string prefix, bool atomizedNames)
  224. {
  225. if (prefix == null || count == 0)
  226. return false;
  227. for (int i = declPos; i > declPos - count; i--) {
  228. if (decls [i].Prefix == prefix)
  229. return true;
  230. }
  231. return false;
  232. }
  233. public virtual string LookupNamespace (string prefix)
  234. {
  235. switch (prefix) {
  236. case PrefixXmlns:
  237. return nameTable.Get (XmlnsXmlns);
  238. case PrefixXml:
  239. return nameTable.Get (XmlnsXml);
  240. case "":
  241. return DefaultNamespace;
  242. case null:
  243. return null;
  244. }
  245. for (int i = declPos; i >= 0; i--) {
  246. if (CompareString (decls [i].Prefix, prefix, internalAtomizedNames) && decls [i].Uri != null /* null == flag for removed */)
  247. return decls [i].Uri;
  248. }
  249. return null;
  250. }
  251. internal string LookupNamespace (string prefix, bool atomizedNames)
  252. {
  253. internalAtomizedNames = atomizedNames;
  254. string ret = LookupNamespace (prefix);
  255. internalAtomizedNames = false;
  256. return ret;
  257. }
  258. public virtual string LookupPrefix (string uri)
  259. {
  260. #if NET_2_0
  261. return LookupPrefix (uri, false);
  262. #else
  263. return LookupPrefix (uri, true);
  264. #endif
  265. }
  266. private bool CompareString (string s1, string s2, bool atomizedNames)
  267. {
  268. if (atomizedNames)
  269. return object.ReferenceEquals (s1, s2);
  270. else
  271. return s1 == s2;
  272. }
  273. internal string LookupPrefix (string uri, bool atomizedName)
  274. {
  275. return LookupPrefixCore (uri, atomizedName, false);
  276. }
  277. internal string LookupPrefixExclusive (string uri, bool atomizedName)
  278. {
  279. return LookupPrefixCore (uri, atomizedName, true);
  280. }
  281. string LookupPrefixCore (string uri, bool atomizedName, bool excludeOverriden)
  282. {
  283. if (uri == null)
  284. return null;
  285. if (CompareString (uri, DefaultNamespace, atomizedName))
  286. return string.Empty;
  287. if (CompareString (uri, XmlnsXml, atomizedName))
  288. return PrefixXml;
  289. if (CompareString (uri, XmlnsXmlns, atomizedName))
  290. return PrefixXmlns;
  291. for (int i = declPos; i >= 0; i--) {
  292. if (CompareString (decls [i].Uri, uri, atomizedName) && decls [i].Prefix.Length > 0) // we already looked for ""
  293. if (!excludeOverriden || !IsOverriden (i))
  294. return decls [i].Prefix;
  295. }
  296. // ECMA specifies that this method returns String.Empty
  297. // in case of no match. But actually MS.NET returns null.
  298. // For more information,see
  299. // http://lists.ximian.com/archives/public/mono-list/2003-January/005071.html
  300. //return String.Empty;
  301. return null;
  302. }
  303. bool IsOverriden (int idx)
  304. {
  305. if (idx == declPos)
  306. return false;
  307. string prefix = decls [idx + 1].Prefix;
  308. for (int i = idx + 1; i <= declPos; i++)
  309. if ((object) decls [idx].Prefix == (object) prefix)
  310. return true;
  311. return false;
  312. }
  313. public virtual bool PopScope ()
  314. {
  315. if (scopePos == -1)
  316. return false;
  317. declPos -= count;
  318. defaultNamespace = scopes [scopePos].DefaultNamespace;
  319. count = scopes [scopePos].DeclCount;
  320. scopePos --;
  321. return true;
  322. }
  323. public virtual void PushScope ()
  324. {
  325. scopePos ++;
  326. if (scopePos == scopes.Length)
  327. GrowScopes ();
  328. scopes [scopePos].DefaultNamespace = defaultNamespace;
  329. scopes [scopePos].DeclCount = count;
  330. count = 0;
  331. }
  332. // It is rarely used, so we don't need NameTable optimization on it.
  333. public virtual void RemoveNamespace (string prefix, string uri)
  334. {
  335. RemoveNamespace (prefix, uri, false);
  336. }
  337. void RemoveNamespace (string prefix, string uri, bool atomizedNames)
  338. {
  339. if (prefix == null)
  340. throw new ArgumentNullException ("prefix");
  341. if (uri == null)
  342. throw new ArgumentNullException ("uri");
  343. if (count == 0)
  344. return;
  345. for (int i = declPos; i > declPos - count; i--) {
  346. if (CompareString (decls [i].Prefix, prefix, atomizedNames) && CompareString (decls [i].Uri, uri, atomizedNames))
  347. decls [i].Uri = null;
  348. }
  349. }
  350. #endregion
  351. }
  352. }