* ContractReference.cs, DiscoveryClientProtocol.cs,
[mono.git] / mcs / class / System.Web.Services / System.Web.Services.Discovery / ContractReference.cs
1 // \r
2 // System.Web.Services.Discovery.ContractReference.cs\r
3 //\r
4 // Author:\r
5 //   Dave Bettin (javabettin@yahoo.com)\r
6 //   Lluis Sanchez Gual (lluis@ximian.com)\r
7 //\r
8 // Copyright (C) Dave Bettin, 2002\r
9 //\r
10 \r
11 using System.IO;\r
12 using System.Web.Services.Description;\r
13 using System.Xml.Serialization;\r
14 using System.Xml;\r
15 using System.Xml.Schema;\r
16 \r
17 namespace System.Web.Services.Discovery {\r
18 \r
19         [XmlRootAttribute("contractRef", Namespace="http://schemas.xmlsoap.org/disco/scl/", IsNullable=true)]\r
20         public class ContractReference : DiscoveryReference {\r
21 \r
22                 #region Fields\r
23                 \r
24                 public const string Namespace = "http://schemas.xmlsoap.org/disco/scl/";\r
25 \r
26                 private ServiceDescription contract;\r
27                 private string defaultFilename;\r
28                 private string docRef;\r
29                 private string href;\r
30                 \r
31                 #endregion // Fields\r
32                 \r
33                 #region Constructors\r
34 \r
35                 public ContractReference () \r
36                 {\r
37                 }\r
38                 \r
39                 public ContractReference (string href) : this() \r
40                 {\r
41                         this.href = href;\r
42                 }\r
43                 \r
44                 public ContractReference (string href, string docRef)\r
45                 {\r
46                         this.href = href;\r
47                         this.docRef = docRef;\r
48                 }\r
49                 \r
50                 #endregion // Constructors\r
51 \r
52                 #region Properties\r
53 \r
54                 [XmlIgnore]\r
55                 public ServiceDescription Contract {\r
56                         get {\r
57                                 if (ClientProtocol == null) \r
58                                         throw new InvalidOperationException ("The ClientProtocol property is a null reference");\r
59                                 \r
60                                 ServiceDescription desc = ClientProtocol.Documents [Url] as ServiceDescription;\r
61                                 if (desc == null)\r
62                                         throw new Exception ("The Documents property of ClientProtocol does not contain a WSDL document with the url " + Url);\r
63                                         \r
64                                 return desc; \r
65                         }\r
66                 }\r
67 \r
68                 [XmlIgnore]\r
69                 public override string DefaultFilename {\r
70                         get { return FilenameFromUrl (Url) + ".wsdl"; }\r
71                 }\r
72                 \r
73                 [XmlAttribute("docRef")]\r
74                 public string DocRef {\r
75                         get { return docRef; }\r
76                         set { docRef = value; }\r
77                 }\r
78                 \r
79                 [XmlAttribute("ref")]\r
80                 public string Ref {\r
81                         get { return href; }\r
82                         set { href = value; }\r
83                 }\r
84                 \r
85                 [XmlIgnore]\r
86                 public override string Url {\r
87                         get { return href;}                     \r
88                         set { href = value; }\r
89                 }\r
90                 \r
91                 #endregion // Properties\r
92 \r
93                 #region Methods\r
94 \r
95                 public override object ReadDocument (Stream stream)\r
96                 {\r
97                         return ServiceDescription.Read (stream);\r
98                 }\r
99                 \r
100                 protected internal override void Resolve (string contentType, Stream stream) \r
101                 {\r
102                         ServiceDescription wsdl = ServiceDescription.Read (stream);\r
103                         \r
104                         if (!ClientProtocol.References.Contains (Url))\r
105                                 ClientProtocol.References.Add (this);\r
106 \r
107                         ClientProtocol.Documents.Add (Url, wsdl);\r
108                         ResolveInternal (ClientProtocol, wsdl);\r
109                 }\r
110                 \r
111                 internal void ResolveInternal (DiscoveryClientProtocol prot, ServiceDescription wsdl) \r
112                 {\r
113                         if (wsdl.Imports == null) return;\r
114                         \r
115                         foreach (Import import in wsdl.Imports)\r
116                         {\r
117                                 if (prot.Documents.Contains (import.Location))  // Already resolved\r
118                                         continue;\r
119                                         \r
120                                 string url = import.Location;\r
121                                 string contentType = null;\r
122                                 Stream stream = prot.Download (ref url, ref contentType);\r
123                                 XmlTextReader reader = new XmlTextReader (stream);\r
124                                 reader.MoveToContent ();\r
125                                 \r
126                                 DiscoveryReference refe;\r
127                                 if (ServiceDescription.CanRead (reader))\r
128                                 {\r
129                                         ServiceDescription refWsdl = ServiceDescription.Read (reader);\r
130                                         refe = new ContractReference ();\r
131                                         refe.ClientProtocol = prot;\r
132                                         refe.Url = url;\r
133                                         ((ContractReference)refe).ResolveInternal (prot, refWsdl);\r
134                                         prot.Documents.Add (url, refWsdl);\r
135                                 }\r
136                                 else\r
137                                 {\r
138                                         XmlSchema schema = XmlSchema.Read (reader, null);\r
139                                         refe = new SchemaReference ();\r
140                                         refe.ClientProtocol = prot;\r
141                                         refe.Url = url;\r
142                                         prot.Documents.Add (url, schema);\r
143                                 }\r
144                                 \r
145                                 if (!prot.References.Contains (url))\r
146                                         prot.References.Add (refe);\r
147                                         \r
148                                 reader.Close ();\r
149                         }\r
150                 }\r
151                 \r
152         public override void WriteDocument (object document, Stream stream) \r
153                 {\r
154                         ((ServiceDescription)document).Write (stream);\r
155                 }\r
156 \r
157                 #endregion // Methods\r
158         }\r
159 }