XmlNamespaceManager.cs 8.8 KB

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