XmlResolver.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. //
  2. // System.Xml.XmlResolver.cs
  3. //
  4. // Author:
  5. // Jason Diamond ([email protected])
  6. // Atsushi Enomoto ([email protected])
  7. //
  8. // (C) 2001 Jason Diamond http://injektilo.org/
  9. // (C) 2004 Novell Inc.
  10. //
  11. using System;
  12. using System.IO;
  13. using System.Net;
  14. namespace System.Xml
  15. {
  16. public abstract class XmlResolver
  17. {
  18. public abstract ICredentials Credentials { set; }
  19. public abstract object GetEntity (
  20. Uri absoluteUri,
  21. string role,
  22. Type type);
  23. public virtual Uri ResolveUri (Uri baseUri, string relativeUri)
  24. {
  25. if (baseUri == null) {
  26. if (relativeUri == null)
  27. throw new NullReferenceException ("Either baseUri or relativeUri are required.");
  28. // Don't ignore such case that relativeUri is in fact absolute uri (e.g. ResolveUri (null, "http://foo.com")).
  29. if (relativeUri.StartsWith ("http:") ||
  30. relativeUri.StartsWith ("https:") ||
  31. relativeUri.StartsWith ("file:"))
  32. return new Uri (relativeUri);
  33. else
  34. // extraneous "/a" is required because current Uri stuff
  35. // seems ignorant of difference between "." and "./".
  36. // I'd be appleciate if it is fixed with better solution.
  37. return new Uri (Path.GetFullPath (relativeUri));
  38. // return new Uri (new Uri (Path.GetFullPath ("./a")), EscapeRelativeUriBody (relativeUri));
  39. }
  40. if (relativeUri == null)
  41. return baseUri;
  42. return new Uri (baseUri, EscapeRelativeUriBody (relativeUri));
  43. }
  44. // see also XmlUrlResolver.UnescapeRelativeUriBody().
  45. private string EscapeRelativeUriBody (string src)
  46. {
  47. return src.Replace ("<", "%3C")
  48. .Replace (">", "%3E")
  49. .Replace ("#", "%23")
  50. .Replace ("%", "%25")
  51. .Replace ("\"", "%22");
  52. }
  53. }
  54. }