XmlNamespaceManager.cs 7.6 KB

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