XmlNamespaceManager.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. return _Top != null && _Top.Namespaces != null && _Top.Namespaces.Contains(prefix);
  64. }
  65. public virtual string LookupNamespace(string prefix)
  66. {
  67. NamespaceScope scope = _Top;
  68. while (scope != null)
  69. {
  70. if (scope.Namespaces != null && scope.Namespaces.Contains(prefix))
  71. {
  72. return scope.Namespaces[prefix] as string;
  73. }
  74. scope = scope.Next;
  75. }
  76. switch (prefix)
  77. {
  78. case "xmlns":
  79. return "http://www.w3.org/2000/xmlns/";
  80. case "xml":
  81. return "http://www.w3.org/XML/1998/namespace";
  82. case "":
  83. return String.Empty;
  84. }
  85. return null;
  86. }
  87. public virtual string LookupPrefix(string uri)
  88. {
  89. // TODO: implement me.
  90. throw new NotImplementedException();
  91. }
  92. public virtual bool PopScope()
  93. {
  94. if (_Top != null)
  95. {
  96. _Top = _Top.Next;
  97. return true;
  98. }
  99. return false;
  100. }
  101. public virtual void PushScope()
  102. {
  103. NamespaceScope newScope = new NamespaceScope();
  104. newScope.Next = _Top;
  105. _Top = newScope;
  106. }
  107. public virtual void RemoveNamespace(string prefix, string uri)
  108. {
  109. // TODO: implement me.
  110. throw new NotImplementedException();
  111. }
  112. }
  113. internal class NamespaceScope
  114. {
  115. internal NamespaceScope Next;
  116. internal Hashtable Namespaces;
  117. }
  118. }