XmlNamespaceManager.cs 11 KB

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