SchemaReference.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //
  2. // System.Web.Services.Discovery.SchemaReference.cs
  3. //
  4. // Author:
  5. // Dave Bettin ([email protected])
  6. // Lluis Sanchez Gual ([email protected])
  7. //
  8. // Copyright (C) Dave Bettin, 2002
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.ComponentModel;
  31. using System.IO;
  32. using System.Xml;
  33. using System.Xml.Schema;
  34. using System.Xml.Serialization;
  35. namespace System.Web.Services.Discovery {
  36. [XmlRootAttribute("schemaRef", Namespace="http://schemas.xmlsoap.org/disco/schema/", IsNullable=true)]
  37. public sealed class SchemaReference : DiscoveryReference {
  38. #region Fields
  39. public const string Namespace = "http://schemas.xmlsoap.org/disco/schema/";
  40. private string defaultFilename;
  41. private string href;
  42. private string targetNamespace;
  43. private XmlSchema schema;
  44. #endregion // Fields
  45. #region Constructors
  46. public SchemaReference ()
  47. {
  48. }
  49. public SchemaReference (string href) : this()
  50. {
  51. this.href = href;
  52. }
  53. #endregion // Constructors
  54. #region Properties
  55. [XmlIgnore]
  56. public override string DefaultFilename {
  57. get { return FilenameFromUrl (Url) + ".xsd"; }
  58. }
  59. [XmlAttribute("ref")]
  60. public string Ref {
  61. get { return href; }
  62. set { href = value; }
  63. }
  64. [XmlIgnore]
  65. public override string Url {
  66. get { return href; }
  67. set { href = value; }
  68. }
  69. [DefaultValue(null)]
  70. [XmlAttribute("targetNamespace")]
  71. public string TargetNamespace {
  72. get { return targetNamespace; }
  73. set { targetNamespace = value; }
  74. }
  75. [XmlIgnore]
  76. public XmlSchema Schema {
  77. get {
  78. if (ClientProtocol == null)
  79. throw new InvalidOperationException ("The ClientProtocol property is a null reference");
  80. XmlSchema doc = ClientProtocol.Documents [Url] as XmlSchema;
  81. if (doc == null)
  82. throw new Exception ("The Documents property of ClientProtocol does not contain a schema with the url " + Url);
  83. return doc;
  84. }
  85. }
  86. #endregion // Properties
  87. #region Methods
  88. public override object ReadDocument (Stream stream)
  89. {
  90. return XmlSchema.Read (stream, null);
  91. }
  92. protected internal override void Resolve (string contentType, Stream stream)
  93. {
  94. XmlSchema doc = XmlSchema.Read (stream, null);
  95. ClientProtocol.Documents.Add (Url, doc);
  96. if (!ClientProtocol.References.Contains (Url))
  97. ClientProtocol.References.Add (this);
  98. }
  99. internal void ResolveInternal (DiscoveryClientProtocol prot, XmlSchema xsd)
  100. {
  101. if (xsd.Includes.Count == 0) return;
  102. foreach (XmlSchemaExternal ext in xsd.Includes)
  103. {
  104. if (ext.SchemaLocation == null)
  105. continue;
  106. // Make relative uris to absoulte
  107. Uri uri = new Uri (BaseUri, ext.SchemaLocation);
  108. string url = uri.ToString ();
  109. if (prot.Documents.Contains (url)) // Already resolved
  110. continue;
  111. try
  112. {
  113. string contentType = null;
  114. Stream stream = prot.Download (ref url, ref contentType);
  115. XmlTextReader reader = new XmlTextReader (url, stream);
  116. reader.XmlResolver = null;
  117. reader.MoveToContent ();
  118. DiscoveryReference refe;
  119. XmlSchema schema = XmlSchema.Read (reader, null);
  120. refe = new SchemaReference ();
  121. refe.ClientProtocol = prot;
  122. refe.Url = url;
  123. prot.Documents.Add (url, schema);
  124. ((SchemaReference)refe).ResolveInternal (prot, schema);
  125. if (!prot.References.Contains (url))
  126. prot.References.Add (refe);
  127. stream.Close ();
  128. }
  129. catch (Exception ex)
  130. {
  131. ReportError (url, ex);
  132. }
  133. }
  134. }
  135. public override void WriteDocument (object document, Stream stream)
  136. {
  137. ((XmlSchema)document).Write (stream);
  138. }
  139. #endregion // Methods
  140. }
  141. }