XmlDataSourceResolver.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // XmlDataSourceResolver.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // (C)2003 Novell inc.
  8. //
  9. #if NET_1_2
  10. using System;
  11. using System.Collections;
  12. using System.Data;
  13. using System.Data.SqlXml;
  14. using System.Net;
  15. namespace System.Xml
  16. {
  17. public class XmlDataSourceResolver : XmlResolver
  18. {
  19. XmlNameTable nameTable;
  20. Hashtable table;
  21. public XmlDataSourceResolver ()
  22. : this (new NameTable ())
  23. {
  24. }
  25. public XmlDataSourceResolver (XmlNameTable nameTable)
  26. {
  27. this.nameTable = nameTable;
  28. table = new Hashtable ();
  29. }
  30. public virtual int Count {
  31. get { return table.Count; }
  32. }
  33. public ICredentials Credentials {
  34. set { throw new NotImplementedException (); }
  35. }
  36. public virtual object this [string query] {
  37. get { return table [new Uri (query, true, true)]; }
  38. }
  39. public virtual void Add (string name, IDbConnection dbConnection)
  40. {
  41. table.Add (new Uri (name), dbConnection);
  42. }
  43. public virtual void Add (string name, IDbTransaction dbTransaction)
  44. {
  45. table.Add (new Uri (name), dbTransaction);
  46. }
  47. public virtual void Add (string name, string sourceUri)
  48. {
  49. table.Add (new Uri (name), sourceUri);
  50. }
  51. public virtual void Add (string name, XmlReader documentReader)
  52. {
  53. table.Add (new Uri (name), documentReader);
  54. }
  55. public virtual void Add (string name, XPathNavigator2 document)
  56. {
  57. table.Add (new Uri (name), document);
  58. }
  59. public virtual void Clear ()
  60. {
  61. table.Clear ();
  62. }
  63. public virtual bool Contains (string name)
  64. {
  65. return table.ContainsKey (new Uri (name, true, true));
  66. }
  67. public override object GetEntity (Uri absoluteUri,
  68. string role,
  69. Type ofObjectToReturn)
  70. {
  71. if (absoluteUri == null)
  72. throw new ArgumentNullException ("absoluteUri");
  73. if (ofObjectToReturn == null)
  74. throw new ArgumentNullException ("ofObjectToReturn");
  75. object o = table [absoluteUri];
  76. if (o == null)
  77. return null;
  78. Type type = o.GetType ();
  79. if (type == ofObjectToReturn)
  80. return o;
  81. else if (type.IsSubClassOf (ofObjectToReturn))
  82. return o;
  83. switch (ofObjectToReturn.FullName) {
  84. case "System.Data.IDbConnection":
  85. throw new NotImplementedException ();
  86. case "System.Xml.XPathNavigator2":
  87. return GetXPathNavigator (o);
  88. case "System.Array": // array of IXPathNavigable
  89. throw new NotImplementedException ();
  90. default:
  91. throw new NotSupportedException ();
  92. }
  93. }
  94. private XPathNavigator2 GetXPathNavigator (object o)
  95. {
  96. if (o is string)
  97. return new XPathDocument2 (new XmlTextReader (o as string)).CreateNavigator ();
  98. else if (o is XmlReader)
  99. return new XPathDocument2 (o as XmlReader).CreateNavigator ();
  100. else
  101. throw new NotImplementedException ();
  102. }
  103. public IDictionaryEnumerator GetEnumerator ()
  104. {
  105. return table.GetEnumerator ();
  106. }
  107. public void Remove (string name)
  108. {
  109. table.Remove (new Uri (name, true, true));
  110. }
  111. public override Uri ResolveUri (Uri baseUri, string relativeUri)
  112. {
  113. // XmlDataSourceResolver has no concept of base URIs.
  114. // Note that this constructor uses new .NET 1.2 feature.
  115. return new Uri (relativeUri, true, true)
  116. }
  117. }
  118. }
  119. #endif