DiscoveryDocumentReference.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // System.Web.Services.Discovery.DiscoveryDocumentReference.cs
  3. //
  4. // Author:
  5. // Dave Bettin ([email protected])
  6. // Tim Coleman ([email protected])
  7. // Lluis Sanchez Gual ([email protected])
  8. //
  9. // Copyright (C) Dave Bettin, 2002
  10. // Copyright (C) Tim Coleman, 2002
  11. //
  12. using System.IO;
  13. using System.Web.Services.Description;
  14. using System.Xml.Serialization;
  15. namespace System.Web.Services.Discovery {
  16. [XmlRootAttribute("discoveryRef", Namespace="http://schemas.xmlsoap.org/disco/", IsNullable=true)]
  17. public sealed class DiscoveryDocumentReference : DiscoveryReference {
  18. #region Fields
  19. private DiscoveryDocument document;
  20. private string defaultFilename;
  21. private string href;
  22. #endregion // Fields
  23. #region Constructors
  24. public DiscoveryDocumentReference ()
  25. {
  26. href = String.Empty;
  27. }
  28. public DiscoveryDocumentReference (string href)
  29. {
  30. this.href = href;
  31. }
  32. #endregion // Constructors
  33. #region Properties
  34. [XmlIgnore]
  35. public DiscoveryDocument Document {
  36. get {
  37. if (ClientProtocol == null)
  38. throw new InvalidOperationException ("The ClientProtocol property is a null reference");
  39. DiscoveryDocument doc = ClientProtocol.Documents [Url] as DiscoveryDocument;
  40. if (doc == null)
  41. throw new Exception ("The Documents property of ClientProtocol does not contain a discovery document with the url " + Url);
  42. return doc;
  43. }
  44. }
  45. [XmlIgnore]
  46. public override string DefaultFilename {
  47. get { return FilenameFromUrl (Url) + ".disco"; }
  48. }
  49. [XmlAttribute("ref")]
  50. public string Ref {
  51. get { return href; }
  52. set { href = value; }
  53. }
  54. [XmlIgnore]
  55. public override string Url {
  56. get { return href; }
  57. set { href = value; }
  58. }
  59. #endregion // Properties
  60. #region Methods
  61. public override object ReadDocument (Stream stream)
  62. {
  63. return DiscoveryDocument.Read (stream);
  64. }
  65. protected internal override void Resolve (string contentType, Stream stream)
  66. {
  67. DiscoveryDocument doc = DiscoveryDocument.Read (stream);
  68. ClientProtocol.Documents.Add (Url, doc);
  69. if (!ClientProtocol.References.Contains (Url))
  70. ClientProtocol.References.Add (this);
  71. foreach (DiscoveryReference re in doc.References)
  72. {
  73. re.ClientProtocol = ClientProtocol;
  74. ClientProtocol.References.Add (re.Url, re);
  75. }
  76. }
  77. public void ResolveAll ()
  78. {
  79. if (ClientProtocol.Documents.Contains (Url)) // Already resolved
  80. return;
  81. Resolve ();
  82. DiscoveryDocument doc = document;
  83. foreach (DiscoveryReference re in doc.References)
  84. {
  85. try
  86. {
  87. if (re is DiscoveryDocumentReference)
  88. ((DiscoveryDocumentReference)re).ResolveAll ();
  89. else
  90. re.Resolve ();
  91. }
  92. catch (Exception ex)
  93. {
  94. ReportError (re.Url, ex);
  95. }
  96. }
  97. }
  98. public override void WriteDocument (object document, Stream stream)
  99. {
  100. ((DiscoveryDocument)document).Write (stream);
  101. }
  102. #endregion // Methods
  103. }
  104. }