XmlUrlResolver.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // System.Xml.XmlUrlResolver.cs
  2. //
  3. // Author: Duncan Mak ([email protected])
  4. // Atsushi Enomoto ([email protected])
  5. //
  6. // (C) Ximian, Inc.
  7. //
  8. using System.Net;
  9. using System.IO;
  10. using System.Text;
  11. using Mono.Xml.Native;
  12. namespace System.Xml
  13. {
  14. public class XmlUrlResolver : XmlResolver
  15. {
  16. // Field
  17. ICredentials credential;
  18. WebClient webClientInternal;
  19. WebClient webClient {
  20. get {
  21. if (webClientInternal == null)
  22. webClientInternal = new WebClient ();
  23. return webClientInternal;
  24. }
  25. }
  26. // Constructor
  27. public XmlUrlResolver ()
  28. : base ()
  29. {
  30. }
  31. // Properties
  32. public override ICredentials Credentials
  33. {
  34. set { credential = value; }
  35. }
  36. // Methods
  37. [MonoTODO("Use Credentials; Uri must be absolute.")]
  38. public override object GetEntity (Uri absoluteUri, string role, Type ofObjectToReturn)
  39. {
  40. // (MS documentation says) parameter role isn't used yet.
  41. Stream s = null;
  42. using (s) {
  43. WebClient wc = new WebClient ();
  44. // wc.Credentials = credential;
  45. s = wc.OpenRead (absoluteUri.ToString ());
  46. if (s.GetType ().IsSubclassOf (ofObjectToReturn))
  47. return s;
  48. wc.Dispose ();
  49. }
  50. return null;
  51. }
  52. public override Uri ResolveUri (Uri baseUri, string relativeUri)
  53. {
  54. // Don't expect baseUri is not null here.
  55. // if (relativeUri == null)
  56. // return baseUri;
  57. if (baseUri == null) {
  58. // Don't ignore such case that relativeUri is in fact absolute uri.
  59. // return new Uri (Path.GetFullPath (relativeUri));
  60. // extraneous "/a" is required because current Uri stuff
  61. // seems ignorant of difference between "." and "./".
  62. // I'd be appleciate if it is fixed with better solution.
  63. return new Uri (new Uri (Path.GetFullPath ("./a")), relativeUri);
  64. }
  65. // Do not expect relativeUri.Length > 2.
  66. // if (relativeUri.IndexOf ("://") >= 0)
  67. // return new Uri (relativeUri);
  68. if (relativeUri == null)
  69. return baseUri;
  70. return new Uri (baseUri, relativeUri);
  71. }
  72. }
  73. }