XmlNamespaceManager.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
  2. //
  3. // System.Xml.XmlNamespaceManager.cs
  4. //
  5. // Author:
  6. // Jason Diamond ([email protected])
  7. //
  8. // (C) 2001 Jason Diamond http://injektilo.org/
  9. //
  10. using System.Collections;
  11. namespace System.Xml
  12. {
  13. public class XmlNamespaceManager : IEnumerable
  14. {
  15. private XmlNameTable _NameTable;
  16. NamespaceScope _Top;
  17. public XmlNamespaceManager(XmlNameTable nameTable)
  18. {
  19. _NameTable = nameTable;
  20. PushScope();
  21. }
  22. public virtual string DefaultNamespace
  23. {
  24. get
  25. {
  26. return LookupNamespace(String.Empty);
  27. }
  28. }
  29. public XmlNameTable NameTable
  30. {
  31. get
  32. {
  33. return _NameTable;
  34. }
  35. }
  36. public virtual void AddNamespace(string prefix, string uri)
  37. {
  38. if (prefix == null)
  39. {
  40. throw new ArgumentNullException("prefix", "Value cannot be null.");
  41. }
  42. if (uri == null)
  43. {
  44. throw new ArgumentNullException("uri", "Value cannot be null.");
  45. }
  46. if (prefix.Length > 2 && prefix.Substring(0, 3).ToLower() == "xml")
  47. {
  48. 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");
  49. }
  50. if (_Top.Namespaces == null)
  51. {
  52. _Top.Namespaces = new Hashtable();
  53. }
  54. _Top.Namespaces.Add(prefix, uri);
  55. }
  56. public virtual IEnumerator GetEnumerator()
  57. {
  58. // TODO: implement me.
  59. throw new NotImplementedException();
  60. }
  61. public virtual bool HasNamespace(string prefix)
  62. {
  63. NamespaceScope scope = _Top;
  64. while (scope != null)
  65. {
  66. if (scope.Namespaces != null)
  67. {
  68. if (scope.Namespaces.Contains(prefix))
  69. {
  70. return true;
  71. }
  72. }
  73. scope = scope.Next;
  74. }
  75. return false;
  76. }
  77. public virtual string LookupNamespace(string prefix)
  78. {
  79. NamespaceScope scope = _Top;
  80. while (scope != null)
  81. {
  82. if (scope.Namespaces != null)
  83. {
  84. if (scope.Namespaces.Contains(prefix))
  85. {
  86. string uri = scope.Namespaces[prefix] as string;
  87. return uri;
  88. }
  89. }
  90. scope = scope.Next;
  91. }
  92. switch (prefix)
  93. {
  94. case "xmlns":
  95. return "http://www.w3.org/2000/xmlns/";
  96. case "xml":
  97. return "http://www.w3.org/XML/1998/namespace";
  98. case "":
  99. return String.Empty;
  100. }
  101. return null;
  102. }
  103. public virtual string LookupPrefix(string uri)
  104. {
  105. // TODO: implement me.
  106. throw new NotImplementedException();
  107. }
  108. public virtual bool PopScope()
  109. {
  110. if (_Top != null)
  111. {
  112. _Top = _Top.Next;
  113. return true;
  114. }
  115. return false;
  116. }
  117. public virtual void PushScope()
  118. {
  119. NamespaceScope newScope = new NamespaceScope();
  120. newScope.Next = _Top;
  121. _Top = newScope;
  122. }
  123. public virtual void RemoveNamespace(string prefix, string uri)
  124. {
  125. // TODO: implement me.
  126. throw new NotImplementedException();
  127. }
  128. }
  129. internal class NamespaceScope
  130. {
  131. internal NamespaceScope Next;
  132. internal Hashtable Namespaces;
  133. }
  134. }