SchemaReference.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.Schema;
  33. using System.Xml.Serialization;
  34. namespace System.Web.Services.Discovery {
  35. [XmlRootAttribute("schemaRef", Namespace="http://schemas.xmlsoap.org/disco/schema/", IsNullable=true)]
  36. public sealed class SchemaReference : DiscoveryReference {
  37. #region Fields
  38. public const string Namespace = "http://schemas.xmlsoap.org/disco/schema/";
  39. private string defaultFilename;
  40. private string href;
  41. private string targetNamespace;
  42. private XmlSchema schema;
  43. #endregion // Fields
  44. #region Constructors
  45. public SchemaReference ()
  46. {
  47. }
  48. public SchemaReference (string href) : this()
  49. {
  50. this.href = href;
  51. }
  52. #endregion // Constructors
  53. #region Properties
  54. [XmlIgnore]
  55. public override string DefaultFilename {
  56. get { return FilenameFromUrl (Url) + ".xsd"; }
  57. }
  58. [XmlAttribute("ref")]
  59. public string Ref {
  60. get { return href; }
  61. set { href = value; }
  62. }
  63. [XmlIgnore]
  64. public override string Url {
  65. get { return href; }
  66. set { href = value; }
  67. }
  68. [DefaultValue("")]
  69. [XmlAttribute("targetNamespace")]
  70. public string TargetNamespace {
  71. get { return targetNamespace; }
  72. set { targetNamespace = value; }
  73. }
  74. [XmlIgnore]
  75. public XmlSchema Schema {
  76. get {
  77. if (ClientProtocol == null)
  78. throw new InvalidOperationException ("The ClientProtocol property is a null reference");
  79. XmlSchema doc = ClientProtocol.Documents [Url] as XmlSchema;
  80. if (doc == null)
  81. throw new Exception ("The Documents property of ClientProtocol does not contain a schema with the url " + Url);
  82. return doc;
  83. }
  84. }
  85. #endregion // Properties
  86. #region Methods
  87. public override object ReadDocument (Stream stream)
  88. {
  89. return XmlSchema.Read (stream, null);
  90. }
  91. protected internal override void Resolve (string contentType, Stream stream)
  92. {
  93. XmlSchema doc = XmlSchema.Read (stream, null);
  94. ClientProtocol.Documents.Add (Url, doc);
  95. if (!ClientProtocol.References.Contains (Url))
  96. ClientProtocol.References.Add (this);
  97. }
  98. public override void WriteDocument (object document, Stream stream)
  99. {
  100. ((XmlSchema)document).Write (stream);
  101. }
  102. #endregion // Methods
  103. }
  104. }