DiscoveryDocumentReference.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System.IO;
  33. using System.Web.Services.Description;
  34. using System.Xml.Serialization;
  35. namespace System.Web.Services.Discovery {
  36. [XmlRootAttribute("discoveryRef", Namespace="http://schemas.xmlsoap.org/disco/", IsNullable=true)]
  37. public sealed class DiscoveryDocumentReference : DiscoveryReference {
  38. #region Fields
  39. private DiscoveryDocument document;
  40. private string defaultFilename;
  41. private string href;
  42. #endregion // Fields
  43. #region Constructors
  44. public DiscoveryDocumentReference ()
  45. {
  46. href = String.Empty;
  47. }
  48. public DiscoveryDocumentReference (string href)
  49. {
  50. this.href = href;
  51. }
  52. #endregion // Constructors
  53. #region Properties
  54. [XmlIgnore]
  55. public DiscoveryDocument Document {
  56. get {
  57. if (ClientProtocol == null)
  58. throw new InvalidOperationException ("The ClientProtocol property is a null reference");
  59. DiscoveryDocument doc = ClientProtocol.Documents [Url] as DiscoveryDocument;
  60. if (doc == null)
  61. throw new Exception ("The Documents property of ClientProtocol does not contain a discovery document with the url " + Url);
  62. return doc;
  63. }
  64. }
  65. [XmlIgnore]
  66. public override string DefaultFilename {
  67. get { return FilenameFromUrl (Url) + ".disco"; }
  68. }
  69. [XmlAttribute("ref")]
  70. public string Ref {
  71. get { return href; }
  72. set { href = value; }
  73. }
  74. [XmlIgnore]
  75. public override string Url {
  76. get { return href; }
  77. set { href = value; }
  78. }
  79. #endregion // Properties
  80. #region Methods
  81. public override object ReadDocument (Stream stream)
  82. {
  83. return DiscoveryDocument.Read (stream);
  84. }
  85. protected internal override void Resolve (string contentType, Stream stream)
  86. {
  87. DiscoveryDocument doc = DiscoveryDocument.Read (stream);
  88. ClientProtocol.Documents.Add (Url, doc);
  89. if (!ClientProtocol.References.Contains (Url))
  90. ClientProtocol.References.Add (this);
  91. foreach (DiscoveryReference re in doc.References)
  92. {
  93. re.ClientProtocol = ClientProtocol;
  94. ClientProtocol.References.Add (re.Url, re);
  95. }
  96. }
  97. public void ResolveAll ()
  98. {
  99. if (ClientProtocol.Documents.Contains (Url)) // Already resolved
  100. return;
  101. Resolve ();
  102. DiscoveryDocument doc = document;
  103. foreach (DiscoveryReference re in doc.References)
  104. {
  105. try
  106. {
  107. if (re is DiscoveryDocumentReference)
  108. ((DiscoveryDocumentReference)re).ResolveAll ();
  109. else
  110. re.Resolve ();
  111. }
  112. catch (Exception ex)
  113. {
  114. ReportError (re.Url, ex);
  115. }
  116. }
  117. }
  118. public override void WriteDocument (object document, Stream stream)
  119. {
  120. ((DiscoveryDocument)document).Write (stream);
  121. }
  122. #endregion // Methods
  123. }
  124. }