XmlNamespaceManager.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // XmlNamespaceManager.cs
  3. //
  4. // Author:
  5. // Jason Diamond ([email protected])
  6. //
  7. // (C) 2001 Jason Diamond http://injektilo.org/
  8. //
  9. using System.Collections;
  10. namespace System.Xml
  11. {
  12. public class XmlNamespaceManager : IEnumerable
  13. {
  14. #region Fields
  15. private XmlNameTable nameTable;
  16. private NamespaceScope currentScope;
  17. #endregion
  18. #region Constructor
  19. public XmlNamespaceManager (XmlNameTable nameTable)
  20. {
  21. this.nameTable = nameTable;
  22. nameTable.Add ("xmlns");
  23. nameTable.Add ("xml");
  24. nameTable.Add (String.Empty);
  25. nameTable.Add ("http://www.w3.org/2000/xmlns/");
  26. nameTable.Add ("http://www.w3.org/XML/1998/namespace");
  27. PushScope ();
  28. }
  29. #endregion
  30. #region Properties
  31. public virtual string DefaultNamespace {
  32. get { return LookupNamespace (String.Empty); }
  33. }
  34. public XmlNameTable NameTable {
  35. get { return nameTable; }
  36. }
  37. #endregion
  38. #region Methods
  39. public virtual void AddNamespace (string prefix, string uri)
  40. {
  41. if (prefix == null)
  42. throw new ArgumentNullException ("prefix", "Value cannot be null.");
  43. if (uri == null)
  44. throw new ArgumentNullException ("uri", "Value cannot be null.");
  45. if (prefix.Length > 2 && prefix.Substring (0, 3).ToLower () == "xml")
  46. throw new ArgumentException ("Prefixes beginning with \"xml\" (regardless of whether the characters are uppercase, lowercase, or some combination thereof) are reserved for use by XML.", "prefix");
  47. if (currentScope.Namespaces == null)
  48. currentScope.Namespaces = new Hashtable ();
  49. if (prefix == String.Empty)
  50. currentScope.Namespaces [prefix] = nameTable.Add (uri);
  51. else
  52. currentScope.Namespaces.Add (nameTable.Add (prefix), nameTable.Add (uri));
  53. }
  54. [MonoTODO]
  55. public virtual IEnumerator GetEnumerator ()
  56. {
  57. throw new NotImplementedException ();
  58. }
  59. public virtual bool HasNamespace (string prefix)
  60. {
  61. return currentScope != null && currentScope.Namespaces != null && currentScope.Namespaces.Contains (prefix);
  62. }
  63. public virtual string LookupNamespace (string prefix)
  64. {
  65. NamespaceScope scope = currentScope;
  66. while (scope != null) {
  67. if (scope.Namespaces != null && scope.Namespaces.Contains (prefix))
  68. return scope.Namespaces[prefix] as string;
  69. scope = scope.Next;
  70. }
  71. switch (prefix) {
  72. case "xmlns":
  73. return nameTable.Get ("http://www.w3.org/2000/xmlns/");
  74. case "xml":
  75. return nameTable.Get ("http://www.w3.org/XML/1998/namespace");
  76. case "":
  77. return nameTable.Get (String.Empty);
  78. }
  79. return null;
  80. }
  81. public virtual string LookupPrefix (string uri)
  82. {
  83. if (uri == null)
  84. return null;
  85. NamespaceScope scope = currentScope;
  86. while (scope != null)
  87. {
  88. if (scope.Namespaces != null && scope.Namespaces.ContainsValue (uri)) {
  89. foreach (DictionaryEntry entry in scope.Namespaces) {
  90. if (entry.Value.ToString() == uri)
  91. return nameTable.Get (entry.Key as string) as string;
  92. }
  93. }
  94. scope = scope.Next;
  95. }
  96. return String.Empty;
  97. }
  98. public virtual bool PopScope ()
  99. {
  100. if (currentScope != null)
  101. currentScope = currentScope.Next;
  102. return currentScope != null;
  103. }
  104. public virtual void PushScope ()
  105. {
  106. NamespaceScope newScope = new NamespaceScope ();
  107. newScope.Next = currentScope;
  108. currentScope = newScope;
  109. }
  110. [MonoTODO]
  111. public virtual void RemoveNamespace (string prefix, string uri)
  112. {
  113. throw new NotImplementedException ();
  114. }
  115. #endregion
  116. }
  117. internal class NamespaceScope
  118. {
  119. internal NamespaceScope Next;
  120. internal Hashtable Namespaces;
  121. }
  122. }