2008-03-12 Jb Evain <jbevain@novell.com>
[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                                 checker.Check (ctx, oper.Output);
103                                 CheckExtensions (ctx, checker, oper.Output.Extensions);
104                         }
105                 }
106                 
107                 static void Check (ConformanceCheckContext ctx, ConformanceChecker checker, ServiceDescription sd)
108                 {
109                         ctx.ServiceDescription = sd;
110                         ctx.Checker = checker;
111                         
112                         checker.Check (ctx, sd);
113                         CheckExtensions (ctx, checker, sd.Extensions);
114                         
115                         foreach (Import i in sd.Imports) {
116                                 checker.Check (ctx, i);
117                         }
118                         
119                         foreach (Service s in sd.Services) {
120                                 checker.Check (ctx, s);
121                                 foreach (Port p in s.Ports) {
122                                         checker.Check (ctx, p);
123                                         CheckExtensions (ctx, checker, p.Extensions);
124                                 }
125                         }
126
127                         checker.Check (ctx, sd.Bindings);
128                         foreach (Binding b in sd.Bindings)
129                                 Check (ctx, checker, b);
130                         
131                         foreach (PortType pt in sd.PortTypes)
132                         {
133                                 checker.Check (ctx, pt);
134                                 
135                                 foreach (Operation oper in pt.Operations) {
136                                         checker.Check (ctx, oper);
137                                         foreach (OperationMessage msg in oper.Messages)
138                                                 checker.Check (ctx, msg);
139                                         
140                                         foreach (OperationMessage msg in oper.Faults)
141                                                 checker.Check (ctx, msg);
142                                 }
143                         }
144                         
145                         foreach (Message msg in sd.Messages)
146                         {
147                                 checker.Check (ctx, msg);
148                                 foreach (MessagePart part in msg.Parts)
149                                         checker.Check (ctx, part);
150                         }
151                         
152                         if (sd.Types != null) {
153                                 checker.Check (ctx, sd.Types);
154                                 if (sd.Types.Schemas != null) {
155                                         foreach (XmlSchema s in sd.Types.Schemas) {
156                                                 ctx.CurrentSchema = s;
157                                                 checker.Check (ctx, s);
158                                                 CheckObjects (ctx, checker, new Hashtable (), s.Items);
159                                         }
160                                 }
161                         }
162                 }
163                 
164                 static void CheckObjects (ConformanceCheckContext ctx, ConformanceChecker checker, Hashtable visitedObjects, XmlSchemaObjectCollection col)
165                 {
166                         foreach (XmlSchemaObject item in col)
167                                 Check (ctx, checker, visitedObjects, item);
168                 }
169                 
170                 static void Check (ConformanceCheckContext ctx, ConformanceChecker checker, Hashtable visitedObjects, XmlSchemaObject value)
171                 {
172                         if (value == null) return;
173                         
174                         if (visitedObjects.Contains (value)) return;
175                         visitedObjects.Add (value, value);
176                         
177                         if (value is XmlSchemaImport) {
178                                 XmlSchemaImport so = (XmlSchemaImport) value;
179                                 checker.Check (ctx, so);
180                         }
181                         else if (value is XmlSchemaAll) {
182                                 XmlSchemaAll so = (XmlSchemaAll) value;
183                                 checker.Check (ctx, so);
184                                 CheckObjects (ctx, checker, visitedObjects, so.Items);
185                         }
186                         else if (value is XmlSchemaAnnotation) {
187                                 XmlSchemaAnnotation so = (XmlSchemaAnnotation) value;
188                                 checker.Check (ctx, so);
189                                 CheckObjects (ctx, checker, visitedObjects, so.Items);
190                         }
191                         else if (value is XmlSchemaAttribute) {
192                                 XmlSchemaAttribute so = (XmlSchemaAttribute) value;
193                                 checker.Check (ctx, so);
194                         }
195                         else if (value is XmlSchemaAttributeGroup) {
196                                 XmlSchemaAttributeGroup so = (XmlSchemaAttributeGroup) value;
197                                 checker.Check (ctx, so);
198                                 CheckObjects (ctx, checker, visitedObjects, so.Attributes);
199                                 Check (ctx, checker, visitedObjects, so.AnyAttribute);
200                                 Check (ctx, checker, visitedObjects, so.RedefinedAttributeGroup);
201                         }
202                         else if (value is XmlSchemaAttributeGroupRef) {
203                                 XmlSchemaAttributeGroupRef so = (XmlSchemaAttributeGroupRef) value;
204                                 checker.Check (ctx, so);
205                         }
206                         else if (value is XmlSchemaChoice) {
207                                 XmlSchemaChoice so = (XmlSchemaChoice) value;
208                                 checker.Check (ctx, so);
209                                 CheckObjects (ctx, checker, visitedObjects, so.Items);
210                         }
211                         else if (value is XmlSchemaComplexContent) {
212                                 XmlSchemaComplexContent so = (XmlSchemaComplexContent) value;
213                                 checker.Check (ctx, so);
214                                 Check (ctx, checker, visitedObjects, so.Content);
215                         }
216                         else if (value is XmlSchemaComplexContentExtension) {
217                                 XmlSchemaComplexContentExtension so = (XmlSchemaComplexContentExtension) value;
218                                 checker.Check (ctx, so);
219                                 Check (ctx, checker, visitedObjects, so.Particle);
220                                 CheckObjects (ctx, checker, visitedObjects, so.Attributes);
221                                 Check (ctx, checker, visitedObjects, so.AnyAttribute);
222                         }
223                         else if (value is XmlSchemaComplexContentRestriction) {
224                                 XmlSchemaComplexContentRestriction so = (XmlSchemaComplexContentRestriction) value;
225                                 checker.Check (ctx, so);
226                                 Check (ctx, checker, visitedObjects, so.Particle);
227                                 CheckObjects (ctx, checker, visitedObjects, so.Attributes);
228                                 Check (ctx, checker, visitedObjects, so.AnyAttribute);
229                         }
230                         else if (value is XmlSchemaComplexType) {
231                                 XmlSchemaComplexType so = (XmlSchemaComplexType) value;
232                                 checker.Check (ctx, so);
233                                 Check (ctx, checker, visitedObjects, so.ContentModel);
234                                 Check (ctx, checker, visitedObjects, so.Particle);
235                                 CheckObjects (ctx, checker, visitedObjects, so.Attributes);
236                                 Check (ctx, checker, visitedObjects, so.AnyAttribute);
237                                 Check (ctx, checker, visitedObjects, so.ContentTypeParticle);
238                                 Check (ctx, checker, visitedObjects, so.AttributeWildcard);
239                         }
240                         else if (value is XmlSchemaElement) {
241                                 XmlSchemaElement so = (XmlSchemaElement) value;
242                                 checker.Check (ctx, so);
243                                 Check (ctx, checker, visitedObjects, so.SchemaType);
244                                 CheckObjects (ctx, checker, visitedObjects, so.Constraints);
245                         }
246                         else if (value is XmlSchemaGroup) {
247                                 XmlSchemaGroup so = (XmlSchemaGroup) value;
248                                 checker.Check (ctx, so);
249                                 Check (ctx, checker, visitedObjects, so.Particle);
250                         }
251                         else if (value is XmlSchemaGroupRef) {
252                                 XmlSchemaGroupRef so = (XmlSchemaGroupRef) value;
253                                 checker.Check (ctx, so);
254                         }
255                         else if (value is XmlSchemaIdentityConstraint) {
256                                 XmlSchemaIdentityConstraint so = (XmlSchemaIdentityConstraint) value;
257                                 checker.Check (ctx, so);
258                                 CheckObjects (ctx, checker, visitedObjects, so.Fields);
259                                 Check (ctx, checker, visitedObjects, so.Selector);
260                         }
261                         else if (value is XmlSchemaKeyref) {
262                                 XmlSchemaKeyref so = (XmlSchemaKeyref) value;
263                                 checker.Check (ctx, so);
264                         }
265                         else if (value is XmlSchemaRedefine) {
266                                 XmlSchemaRedefine so = (XmlSchemaRedefine) value;
267                                 checker.Check (ctx, so);
268                                 CheckObjects (ctx, checker, visitedObjects, so.Items);
269                         }
270                         else if (value is XmlSchemaSequence) {
271                                 XmlSchemaSequence so = (XmlSchemaSequence) value;
272                                 checker.Check (ctx, so);
273                                 CheckObjects (ctx, checker, visitedObjects, so.Items);
274                         }
275                         else if (value is XmlSchemaSimpleContent) {
276                                 XmlSchemaSimpleContent so = (XmlSchemaSimpleContent) value;
277                                 checker.Check (ctx, so);
278                                 Check (ctx, checker, visitedObjects, so.Content);
279                         }
280                         else if (value is XmlSchemaSimpleContentExtension) {
281                                 XmlSchemaSimpleContentExtension so = (XmlSchemaSimpleContentExtension) value;
282                                 checker.Check (ctx, so);
283                                 CheckObjects (ctx, checker, visitedObjects, so.Attributes);
284                                 Check (ctx, checker, visitedObjects, so.AnyAttribute);
285                         }
286                         else if (value is XmlSchemaSimpleContentRestriction) {
287                                 XmlSchemaSimpleContentRestriction so = (XmlSchemaSimpleContentRestriction) value;
288                                 checker.Check (ctx, so);
289                                 CheckObjects (ctx, checker, visitedObjects, so.Attributes);
290                                 Check (ctx, checker, visitedObjects, so.AnyAttribute);
291                                 CheckObjects (ctx, checker, visitedObjects, so.Facets);
292                         }
293                         else if (value is XmlSchemaSimpleType) {
294                                 XmlSchemaSimpleType so = (XmlSchemaSimpleType) value;
295                                 checker.Check (ctx, so);
296                                 Check (ctx, checker, visitedObjects, so.Content);
297                         }
298                         else if (value is XmlSchemaSimpleTypeList) {
299                                 XmlSchemaSimpleTypeList so = (XmlSchemaSimpleTypeList) value;
300                                 checker.Check (ctx, so);
301                         }
302                         else if (value is XmlSchemaSimpleTypeRestriction) {
303                                 XmlSchemaSimpleTypeRestriction so = (XmlSchemaSimpleTypeRestriction) value;
304                                 checker.Check (ctx, so);
305                                 CheckObjects (ctx, checker, visitedObjects, so.Facets);
306                         }
307                         else if (value is XmlSchemaSimpleTypeUnion) {
308                                 XmlSchemaSimpleTypeUnion so = (XmlSchemaSimpleTypeUnion) value;
309                                 checker.Check (ctx, so);
310                         }
311                 }
312                                 
313                 
314                 static void CheckExtensions (ConformanceCheckContext ctx, ConformanceChecker checker, ServiceDescriptionFormatExtensionCollection extensions)
315                 {
316                         foreach (object o in extensions) {
317                                 ServiceDescriptionFormatExtension ext = o as ServiceDescriptionFormatExtension;
318                                 if (ext != null)
319                                         checker.Check (ctx, ext);
320                         }
321                 }
322         }
323 }
324
325 #endif