XmlNamespaceManager.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. currentScope.Namespaces.Add (nameTable.Add (prefix), nameTable.Add (uri));
  50. }
  51. [MonoTODO]
  52. public virtual IEnumerator GetEnumerator ()
  53. {
  54. throw new NotImplementedException ();
  55. }
  56. public virtual bool HasNamespace (string prefix)
  57. {
  58. return currentScope != null && currentScope.Namespaces != null && currentScope.Namespaces.Contains (prefix);
  59. }
  60. public virtual string LookupNamespace (string prefix)
  61. {
  62. NamespaceScope scope = currentScope;
  63. while (scope != null) {
  64. if (scope.Namespaces != null && scope.Namespaces.Contains (prefix))
  65. return scope.Namespaces[prefix] as string;
  66. scope = scope.Next;
  67. }
  68. switch (prefix) {
  69. case "xmlns":
  70. return nameTable.Get ("http://www.w3.org/2000/xmlns/");
  71. case "xml":
  72. return nameTable.Get ("http://www.w3.org/XML/1998/namespace");
  73. case "":
  74. return nameTable.Get (String.Empty);
  75. }
  76. return null;
  77. }
  78. public virtual string LookupPrefix (string uri)
  79. {
  80. if (uri == null)
  81. return null;
  82. NamespaceScope scope = currentScope;
  83. while (scope != null)
  84. {
  85. if (scope.Namespaces != null && scope.Namespaces.ContainsValue (uri)) {
  86. foreach (DictionaryEntry entry in scope.Namespaces) {
  87. if (entry.Value.ToString() == uri)
  88. return nameTable.Get (entry.Key as string) as string;
  89. }
  90. }
  91. scope = scope.Next;
  92. }
  93. return String.Empty;
  94. }
  95. public virtual bool PopScope ()
  96. {
  97. if (currentScope != null)
  98. currentScope = currentScope.Next;
  99. return currentScope != null;
  100. }
  101. public virtual void PushScope ()
  102. {
  103. NamespaceScope newScope = new NamespaceScope ();
  104. newScope.Next = currentScope;
  105. currentScope = newScope;
  106. }
  107. [MonoTODO]
  108. public virtual void RemoveNamespace (string prefix, string uri)
  109. {
  110. throw new NotImplementedException ();
  111. }
  112. #endregion
  113. }
  114. internal class NamespaceScope
  115. {
  116. internal NamespaceScope Next;
  117. internal Hashtable Namespaces;
  118. }
  119. }