XmlDataSourceResolver.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //
  2. // XmlDataSourceResolver.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // (C)2003 Novell inc.
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0
  30. using System;
  31. using System.Collections;
  32. using System.Data;
  33. using System.Data.SqlXml;
  34. using System.Net;
  35. namespace System.Xml
  36. {
  37. public class XmlDataSourceResolver : XmlResolver
  38. {
  39. XmlNameTable nameTable;
  40. Hashtable table;
  41. public XmlDataSourceResolver ()
  42. : this (new NameTable ())
  43. {
  44. }
  45. public XmlDataSourceResolver (XmlNameTable nameTable)
  46. {
  47. this.nameTable = nameTable;
  48. table = new Hashtable ();
  49. }
  50. public virtual int Count {
  51. get { return table.Count; }
  52. }
  53. public ICredentials Credentials {
  54. set { throw new NotImplementedException (); }
  55. }
  56. public virtual object this [string query] {
  57. get { return table [new Uri (query, true, true)]; }
  58. }
  59. public virtual void Add (string name, IDbConnection dbConnection)
  60. {
  61. table.Add (new Uri (name), dbConnection);
  62. }
  63. public virtual void Add (string name, IDbTransaction dbTransaction)
  64. {
  65. table.Add (new Uri (name), dbTransaction);
  66. }
  67. public virtual void Add (string name, string sourceUri)
  68. {
  69. table.Add (new Uri (name), sourceUri);
  70. }
  71. public virtual void Add (string name, XmlReader documentReader)
  72. {
  73. table.Add (new Uri (name), documentReader);
  74. }
  75. public virtual void Add (string name, XPathNavigator2 document)
  76. {
  77. table.Add (new Uri (name), document);
  78. }
  79. public virtual void Clear ()
  80. {
  81. table.Clear ();
  82. }
  83. public virtual bool Contains (string name)
  84. {
  85. return table.ContainsKey (new Uri (name, true, true));
  86. }
  87. public override object GetEntity (Uri absoluteUri,
  88. string role,
  89. Type ofObjectToReturn)
  90. {
  91. if (absoluteUri == null)
  92. throw new ArgumentNullException ("absoluteUri");
  93. if (ofObjectToReturn == null)
  94. throw new ArgumentNullException ("ofObjectToReturn");
  95. object o = table [absoluteUri];
  96. if (o == null)
  97. return null;
  98. Type type = o.GetType ();
  99. if (type == ofObjectToReturn)
  100. return o;
  101. else if (type.IsSubClassOf (ofObjectToReturn))
  102. return o;
  103. switch (ofObjectToReturn.FullName) {
  104. case "System.Data.IDbConnection":
  105. throw new NotImplementedException ();
  106. case "System.Xml.XPathNavigator2":
  107. return GetXPathNavigator (o);
  108. case "System.Array": // array of IXPathNavigable
  109. throw new NotImplementedException ();
  110. default:
  111. throw new NotSupportedException ();
  112. }
  113. }
  114. private XPathNavigator2 GetXPathNavigator (object o)
  115. {
  116. if (o is string)
  117. return new XPathDocument2 (new XmlTextReader (o as string)).CreateNavigator ();
  118. else if (o is XmlReader)
  119. return new XPathDocument2 (o as XmlReader).CreateNavigator ();
  120. else
  121. throw new NotImplementedException ();
  122. }
  123. public IDictionaryEnumerator GetEnumerator ()
  124. {
  125. return table.GetEnumerator ();
  126. }
  127. public void Remove (string name)
  128. {
  129. table.Remove (new Uri (name, true, true));
  130. }
  131. public override Uri ResolveUri (Uri baseUri, string relativeUri)
  132. {
  133. // XmlDataSourceResolver has no concept of base URIs.
  134. // Note that this constructor uses new .NET 1.2 feature.
  135. return new Uri (relativeUri, true, true)
  136. }
  137. }
  138. }
  139. #endif