Merge pull request #762 from echampet/wsdl
[mono.git] / mcs / class / System.Web.Services / System.Web.Services.Description / WebServicesInteroperability.cs
1 // 
2 // System.Web.Services.Description.WebServicesInteroperability.cs
3 //
4 // Author:
5 //   Lluis Sanchez (lluis@novell.com)
6 //
7 // Copyright (C) Novell, Inc., 2004
8 //
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
31 #if NET_2_0
32
33 using System.Collections;
34 using System.Xml.Schema;
35
36 namespace System.Web.Services.Description 
37 {
38         public sealed class WebServicesInteroperability
39         {
40                 private WebServicesInteroperability ()
41                 {
42                 }
43                 
44                 public static bool CheckConformance (WsiProfiles claims, ServiceDescription service, BasicProfileViolationCollection violations)
45                 {
46                         ServiceDescriptionCollection col = new ServiceDescriptionCollection ();
47                         col.Add (service);
48                         ConformanceCheckContext ctx = new ConformanceCheckContext (col, violations);
49                         return Check (claims, ctx, col);
50                 }
51
52                 public static bool CheckConformance (WsiProfiles claims, ServiceDescriptionCollection services, BasicProfileViolationCollection violations)
53                 {
54                         ConformanceCheckContext ctx = new ConformanceCheckContext (services, violations);
55                         return Check (claims, ctx, services);
56                 }
57
58                 public static bool CheckConformance (WsiProfiles claims, WebReference webReference, BasicProfileViolationCollection violations)
59                 {
60                         ConformanceCheckContext ctx = new ConformanceCheckContext (webReference, violations);
61                         return Check (claims, ctx, webReference.Documents.Values);
62                 }
63                 
64                 static bool Check (WsiProfiles claims, ConformanceCheckContext ctx, IEnumerable documents)
65                 {
66                         ConformanceChecker[] checkers = GetCheckers (claims);
67                         if (checkers == null) return true;
68                         
69                         foreach (object doc in documents) {
70                                 if (!(doc is ServiceDescription)) continue;
71                                 
72                                 foreach (ConformanceChecker c in checkers)
73                                         Check (ctx, c, (ServiceDescription)doc);
74                         }
75                                 
76                         return ctx.Violations.Count == 0;
77                 }
78                 
79                 internal static ConformanceChecker[] GetCheckers (WsiProfiles claims)
80                 {
81                         if ((claims & WsiProfiles.BasicProfile1_1) != 0)
82                                 return new ConformanceChecker[] { BasicProfileChecker.Instance };
83                         return null;
84                 }
85
86                 internal static void Check (ConformanceCheckContext ctx, ConformanceChecker checker, Binding b)
87                 {
88                         checker.Check (ctx, b);
89                         CheckExtensions (ctx, checker, b.Extensions);
90
91                         foreach (OperationBinding oper in b.Operations) {
92                                 CheckExtensions (ctx, checker, oper.Extensions);
93
94                                 foreach (MessageBinding mb in oper.Faults) {
95                                         checker.Check (ctx, mb);
96                                         CheckExtensions (ctx, checker, mb.Extensions);
97                                 }
98
99                                 checker.Check (ctx, oper.Input);
100                                 CheckExtensions (ctx, checker, oper.Input.Extensions);
101
102                                 if (oper.Output != null) {
103                                         checker.Check (ctx, oper.Output);
104                                         CheckExtensions (ctx, checker, oper.Output.Extensions);
105                                 }
106                         }
107                 }
108                 
109                 static void Check (ConformanceCheckContext ctx, ConformanceChecker checker, ServiceDescription sd)
110                 {
111                         ctx.ServiceDescription = sd;
112                         ctx.Checker = checker;
113                         
114                         checker.Check (ctx, sd);
115                         CheckExtensions (ctx, checker, sd.Extensions);
116                         
117                         foreach (Import i in sd.Imports) {
118                                 checker.Check (ctx, i);
119                         }
120                         
121                         foreach (Service s in sd.Services) {
122                                 checker.Check (ctx, s);
123                                 foreach (Port p in s.Ports) {
124                                         checker.Check (ctx, p);
125                                         CheckExtensions (ctx, checker, p.Extensions);
126                                 }
127                         }
128
129                         checker.Check (ctx, sd.Bindings);
130                         foreach (Binding b in sd.Bindings)
131                                 Check (ctx, checker, b);
132                         
133                         foreach (PortType pt in sd.PortTypes)
134                         {
135                                 checker.Check (ctx, pt);
136                                 
137                                 foreach (Operation oper in pt.Operations) {
138                                         checker.Check (ctx, oper);
139                                         foreach (OperationMessage msg in oper.Messages)
140                                                 checker.Check (ctx, msg);
141                                         
142                                         foreach (OperationMessage msg in oper.Faults)
143                                                 checker.Check (ctx, msg);
144                                 }
145                         }
146                         
147                         foreach (Message msg in sd.Messages)
148                         {
149                                 checker.Check (ctx, msg);
150                                 foreach (MessagePart part in msg.Parts)
151                                         checker.Check (ctx, part);
152                         }
153                         
154                         if (sd.Types != null) {
155                                 checker.Check (ctx, sd.Types);
156                                 if (sd.Types.Schemas != null) {
157                                         foreach (XmlSchema s in sd.Types.Schemas) {
158                                                 ctx.CurrentSchema = s;
159                                                 checker.Check (ctx, s);
160                                                 CheckObjects (ctx, checker, new Hashtable (), s.Items);
161                                         }
162                                 }
163                         }
164                 }
165                 
166                 static void CheckObjects (ConformanceCheckContext ctx, ConformanceChecker checker, Hashtable visitedObjects, XmlSchemaObjectCollection col)
167                 {
168                         foreach (XmlSchemaObject item in col)
169                                 Check (ctx, checker, visitedObjects, item);
170                 }
171                 
172                 static void Check (ConformanceCheckContext ctx, ConformanceChecker checker, Hashtable visitedObjects, XmlSchemaObject value)
173                 {
174                         if (value == null) return;
175                         
176                         if (visitedObjects.Contains (value)) return;
177                         visitedObjects.Add (value, value);
178                         
179                         if (value is XmlSchemaImport) {
180                                 XmlSchemaImport so = (XmlSchemaImport) value;
181                                 checker.Check (ctx, so);
182                         }
183                         else if (value is XmlSchemaAll) {
184                                 XmlSchemaAll so = (XmlSchemaAll) value;
185                                 checker.Check (ctx, so);
186                                 CheckObjects (ctx, checker, visitedObjects, so.Items);
187                         }
188                         else if (value is XmlSchemaAnnotation) {
189                                 XmlSchemaAnnotation so = (XmlSchemaAnnotation) value;
190                                 checker.Check (ctx, so);
191                                 CheckObjects (ctx, checker, visitedObjects, so.Items);
192                         }
193                         else if (value is XmlSchemaAttribute) {
194                                 XmlSchemaAttribute so = (XmlSchemaAttribute) value;
195                                 checker.Check (ctx, so);
196                         }
197                         else if (value is XmlSchemaAttributeGroup) {
198                                 XmlSchemaAttributeGroup so = (XmlSchemaAttributeGroup) value;
199                                 checker.Check (ctx, so);
200                                 CheckObjects (ctx, checker, visitedObjects, so.Attributes);
201                                 Check (ctx, checker, visitedObjects, so.AnyAttribute);
202                                 Check (ctx, checker, visitedObjects, so.RedefinedAttributeGroup);
203                         }
204                         else if (value is XmlSchemaAttributeGroupRef) {
205                                 XmlSchemaAttributeGroupRef so = (XmlSchemaAttributeGroupRef) value;
206                                 checker.Check (ctx, so);
207                         }
208                         else if (value is XmlSchemaChoice) {
209                                 XmlSchemaChoice so = (XmlSchemaChoice) value;
210                                 checker.Check (ctx, so);
211                                 CheckObjects (ctx, checker, visitedObjects, so.Items);
212                         }
213                         else if (value is XmlSchemaComplexContent) {
214                                 XmlSchemaComplexContent so = (XmlSchemaComplexContent) value;
215                                 checker.Check (ctx, so);
216                                 Check (ctx, checker, visitedObjects, so.Content);
217                         }
218                         else if (value is XmlSchemaComplexContentExtension) {
219                                 XmlSchemaComplexContentExtension so = (XmlSchemaComplexContentExtension) value;
220                                 checker.Check (ctx, so);
221                                 Check (ctx, checker, visitedObjects, so.Particle);
222                                 CheckObjects (ctx, checker, visitedObjects, so.Attributes);
223                                 Check (ctx, checker, visitedObjects, so.AnyAttribute);
224                         }
225                         else if (value is XmlSchemaComplexContentRestriction) {
226                                 XmlSchemaComplexContentRestriction so = (XmlSchemaComplexContentRestriction) value;
227                                 checker.Check (ctx, so);
228                                 Check (ctx, checker, visitedObjects, so.Particle);
229                                 CheckObjects (ctx, checker, visitedObjects, so.Attributes);
230                                 Check (ctx, checker, visitedObjects, so.AnyAttribute);
231                         }
232                         else if (value is XmlSchemaComplexType) {
233                                 XmlSchemaComplexType so = (XmlSchemaComplexType) value;
234                                 checker.Check (ctx, so);
235                                 Check (ctx, checker, visitedObjects, so.ContentModel);
236                                 Check (ctx, checker, visitedObjects, so.Particle);
237                                 CheckObjects (ctx, checker, visitedObjects, so.Attributes);
238                                 Check (ctx, checker, visitedObjects, so.AnyAttribute);
239                                 Check (ctx, checker, visitedObjects, so.ContentTypeParticle);
240                                 Check (ctx, checker, visitedObjects, so.AttributeWildcard);
241                         }
242                         else if (value is XmlSchemaElement) {
243                                 XmlSchemaElement so = (XmlSchemaElement) value;
244                                 checker.Check (ctx, so);
245                                 Check (ctx, checker, visitedObjects, so.SchemaType);
246                                 CheckObjects (ctx, checker, visitedObjects, so.Constraints);
247                         }
248                         else if (value is XmlSchemaGroup) {
249                                 XmlSchemaGroup so = (XmlSchemaGroup) value;
250                                 checker.Check (ctx, so);
251                                 Check (ctx, checker, visitedObjects, so.Particle);
252                         }
253                         else if (value is XmlSchemaGroupRef) {
254                                 XmlSchemaGroupRef so = (XmlSchemaGroupRef) value;
255                                 checker.Check (ctx, so);
256                         }
257                         else if (value is XmlSchemaIdentityConstraint) {
258                                 XmlSchemaIdentityConstraint so = (XmlSchemaIdentityConstraint) value;
259                                 checker.Check (ctx, so);
260                                 CheckObjects (ctx, checker, visitedObjects, so.Fields);
261                                 Check (ctx, checker, visitedObjects, so.Selector);
262                         }
263                         else if (value is XmlSchemaKeyref) {
264                                 XmlSchemaKeyref so = (XmlSchemaKeyref) value;
265                                 checker.Check (ctx, so);
266                         }
267                         else if (value is XmlSchemaRedefine) {
268                                 XmlSchemaRedefine so = (XmlSchemaRedefine) value;
269                                 checker.Check (ctx, so);
270                                 CheckObjects (ctx, checker, visitedObjects, so.Items);
271                         }
272                         else if (value is XmlSchemaSequence) {
273                                 XmlSchemaSequence so = (XmlSchemaSequence) value;
274                                 checker.Check (ctx, so);
275                                 CheckObjects (ctx, checker, visitedObjects, so.Items);
276                         }
277                         else if (value is XmlSchemaSimpleContent) {
278                                 XmlSchemaSimpleContent so = (XmlSchemaSimpleContent) value;
279                                 checker.Check (ctx, so);
280                                 Check (ctx, checker, visitedObjects, so.Content);
281                         }
282                         else if (value is XmlSchemaSimpleContentExtension) {
283                                 XmlSchemaSimpleContentExtension so = (XmlSchemaSimpleContentExtension) value;
284                                 checker.Check (ctx, so);
285                                 CheckObjects (ctx, checker, visitedObjects, so.Attributes);
286                                 Check (ctx, checker, visitedObjects, so.AnyAttribute);
287                         }
288                         else if (value is XmlSchemaSimpleContentRestriction) {
289                                 XmlSchemaSimpleContentRestriction so = (XmlSchemaSimpleContentRestriction) value;
290                                 checker.Check (ctx, so);
291                                 CheckObjects (ctx, checker, visitedObjects, so.Attributes);
292                                 Check (ctx, checker, visitedObjects, so.AnyAttribute);
293                                 CheckObjects (ctx, checker, visitedObjects, so.Facets);
294                         }
295                         else if (value is XmlSchemaSimpleType) {
296                                 XmlSchemaSimpleType so = (XmlSchemaSimpleType) value;
297                                 checker.Check (ctx, so);
298                                 Check (ctx, checker, visitedObjects, so.Content);
299                         }
300                         else if (value is XmlSchemaSimpleTypeList) {
301                                 XmlSchemaSimpleTypeList so = (XmlSchemaSimpleTypeList) value;
302                                 checker.Check (ctx, so);
303                         }
304                         else if (value is XmlSchemaSimpleTypeRestriction) {
305                                 XmlSchemaSimpleTypeRestriction so = (XmlSchemaSimpleTypeRestriction) value;
306                                 checker.Check (ctx, so);
307                                 CheckObjects (ctx, checker, visitedObjects, so.Facets);
308                         }
309                         else if (value is XmlSchemaSimpleTypeUnion) {
310                                 XmlSchemaSimpleTypeUnion so = (XmlSchemaSimpleTypeUnion) value;
311                                 checker.Check (ctx, so);
312                         }
313                 }
314                                 
315                 
316                 static void CheckExtensions (ConformanceCheckContext ctx, ConformanceChecker checker, ServiceDescriptionFormatExtensionCollection extensions)
317                 {
318                         foreach (object o in extensions) {
319                                 ServiceDescriptionFormatExtension ext = o as ServiceDescriptionFormatExtension;
320                                 if (ext != null)
321                                         checker.Check (ctx, ext);
322                         }
323                 }
324         }
325 }
326
327 #endif