2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System.Web.Services / System.Web.Services.Description / ConformanceChecker.cs
1 // 
2 // System.Web.Services.Description.ConformanceChecker.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.Xml.Schema;
34 using System.Xml.Serialization;
35
36 namespace System.Web.Services.Description 
37 {
38         internal abstract class ConformanceChecker
39         {
40                 public abstract WsiClaims Claims { get; }
41
42                 public virtual void Check (ConformanceCheckContext ctx, Binding value) { }
43                 public virtual void Check (ConformanceCheckContext ctx, MessageBinding value) { }
44                 public virtual void Check (ConformanceCheckContext ctx, Import value) { }
45                 public virtual void Check (ConformanceCheckContext ctx, Message value) { }
46                 public virtual void Check (ConformanceCheckContext ctx, MessagePart value) { }
47                 public virtual void Check (ConformanceCheckContext ctx, Operation value) { }
48                 public virtual void Check (ConformanceCheckContext ctx, OperationBinding value) { }
49                 public virtual void Check (ConformanceCheckContext ctx, OperationMessage value) { }
50                 public virtual void Check (ConformanceCheckContext ctx, Port value) { }
51                 public virtual void Check (ConformanceCheckContext ctx, PortType value) { }
52                 public virtual void Check (ConformanceCheckContext ctx, Service value) { }
53                 public virtual void Check (ConformanceCheckContext ctx, ServiceDescription value) { }
54                 public virtual void Check (ConformanceCheckContext ctx, Types value) { }
55                 public virtual void Check (ConformanceCheckContext ctx, ServiceDescriptionFormatExtension value) {}
56                 public virtual void Check (ConformanceCheckContext ctx, XmlSchemaObject value) {}
57                 
58                 public virtual void Check (ConformanceCheckContext ctx, XmlSchema s) {}
59                 public virtual void Check (ConformanceCheckContext ctx, XmlSchemaImport value) {}
60                 public virtual void Check (ConformanceCheckContext ctx, XmlSchemaAll value) {}
61                 public virtual void Check (ConformanceCheckContext ctx, XmlSchemaAnnotation value) {}
62                 public virtual void Check (ConformanceCheckContext ctx, XmlSchemaAttribute value) {}
63                 public virtual void Check (ConformanceCheckContext ctx, XmlSchemaAttributeGroup value) {}
64                 public virtual void Check (ConformanceCheckContext ctx, XmlSchemaAttributeGroupRef value) {}
65                 public virtual void Check (ConformanceCheckContext ctx, XmlSchemaComplexContentExtension value) {}
66                 public virtual void Check (ConformanceCheckContext ctx, XmlSchemaChoice value) {}
67                 public virtual void Check (ConformanceCheckContext ctx, XmlSchemaComplexContent value) {}
68                 public virtual void Check (ConformanceCheckContext ctx, XmlSchemaComplexContentRestriction value) {}
69                 public virtual void Check (ConformanceCheckContext ctx, XmlSchemaComplexType value) {}
70                 public virtual void Check (ConformanceCheckContext ctx, XmlSchemaElement value) {}
71                 public virtual void Check (ConformanceCheckContext ctx, XmlSchemaGroup value) {}
72                 public virtual void Check (ConformanceCheckContext ctx, XmlSchemaGroupRef value) {}
73                 public virtual void Check (ConformanceCheckContext ctx, XmlSchemaIdentityConstraint value) {}
74                 public virtual void Check (ConformanceCheckContext ctx, XmlSchemaKeyref value) {}
75                 public virtual void Check (ConformanceCheckContext ctx, XmlSchemaRedefine value) {}
76                 public virtual void Check (ConformanceCheckContext ctx, XmlSchemaSequence value) {}
77                 public virtual void Check (ConformanceCheckContext ctx, XmlSchemaSimpleContent value) {}
78                 public virtual void Check (ConformanceCheckContext ctx, XmlSchemaSimpleContentExtension value) {}
79                 public virtual void Check (ConformanceCheckContext ctx, XmlSchemaSimpleContentRestriction value) {}
80                 public virtual void Check (ConformanceCheckContext ctx, XmlSchemaSimpleType value) {}
81                 public virtual void Check (ConformanceCheckContext ctx, XmlSchemaSimpleTypeList value) {}
82                 public virtual void Check (ConformanceCheckContext ctx, XmlSchemaSimpleTypeRestriction value) {}
83                 public virtual void Check (ConformanceCheckContext ctx, XmlSchemaSimpleTypeUnion value) {}
84         }
85         
86         internal class ConformanceRule
87         {
88                 public string NormativeStatement;
89                 public string Details;
90                 public string Recommendation;
91                 
92                 public ConformanceRule (string name, string desc, string rec)
93                 {
94                         NormativeStatement = name;
95                         Details = desc;
96                         Recommendation = rec;
97                 }
98         }
99         
100         internal class ConformanceCheckContext
101         {
102                 BasicProfileViolationCollection violations;
103                 ServiceDescriptionCollection collection;
104                 WebReference webReference;
105                 ConformanceChecker checker;
106                 public ServiceDescription ServiceDescription;
107                 public XmlSchema CurrentSchema;
108                 XmlSchemas schemas = new XmlSchemas ();
109                 ServiceDescriptionCollection services;
110                 
111                 public ConformanceCheckContext (ServiceDescriptionCollection collection, BasicProfileViolationCollection violations)
112                 {
113                         this.collection = collection;
114                         this.violations = violations;
115                         foreach (ServiceDescription sd in collection) {
116                                 if (sd.Types != null && sd.Types.Schemas != null)
117                                         schemas.Add (sd.Types.Schemas);
118                         }
119                         services = collection;
120                 }
121                 
122                 public ConformanceCheckContext (WebReference webReference, BasicProfileViolationCollection violations)
123                 {
124                         this.webReference = webReference;
125                         this.violations = violations;
126                         services = new ServiceDescriptionCollection ();
127                         
128                         foreach (object doc in webReference.Documents.Values) 
129                         {
130                                 if (doc is XmlSchema)
131                                         schemas.Add ((XmlSchema)doc);
132                                 else if (doc is ServiceDescription) {
133                                         ServiceDescription sd = (ServiceDescription) doc;
134                                         services.Add (sd);
135                                         if (sd.Types != null && sd.Types.Schemas != null)
136                                                 schemas.Add (sd.Types.Schemas);
137                                 }
138                         }
139                 }
140                 
141                 public ConformanceChecker Checker {
142                         get { return checker; }
143                         set { checker = value; }
144                 }
145                 
146                 public BasicProfileViolationCollection Violations {
147                         get { return violations; }
148                 }
149                 
150                 public XmlSchemas Schemas {
151                         get { return schemas; }
152                 }
153                 
154                 public ServiceDescriptionCollection Services {
155                         get { return services; }
156                 }
157                 
158                 public object GetDocument (string url)
159                 {
160                         if (collection != null)
161                                 return null;
162                         else
163                                 return webReference.Documents [url];
164                 }
165                 
166                 public void ReportError (object currentObject, string msg)
167                 {
168                         throw new InvalidOperationException (msg + " (" + GetDescription (currentObject) + ")");
169                 }
170                 
171                 public void ReportRuleViolation (object currentObject, ConformanceRule rule)
172                 {
173                         BasicProfileViolation v = null;
174                         foreach (BasicProfileViolation bpv in violations) {
175                                 if (bpv.NormativeStatement == rule.NormativeStatement) {
176                                         v = bpv;
177                                         break;
178                                 }
179                         }
180                         
181                         if (v == null) {
182                                 v = new BasicProfileViolation (checker.Claims, rule);
183                                 violations.Add (v);
184                         }
185                         
186                         v.Elements.Add (GetDescription (currentObject));
187                 }
188                 
189                 string GetDescription (object obj)
190                 {
191                         if (obj is ServiceDescription) {
192                                 return "Service Description '" + ServiceDescription.TargetNamespace + "'";
193                         }
194                         else if (obj is Binding || obj is Message || obj is PortType || obj is Service) {
195                                 return GetNamedItemDescription (obj, ServiceDescription);
196                         }
197                         else if (obj is Import) {
198                                 return GetItemDescription (obj, ServiceDescription, ((Import)obj).Location);
199                         }
200                         else if (obj is MessageBinding) {
201                                 return GetNamedItemDescription (obj, ((MessageBinding)obj).OperationBinding);
202                         }
203                         else if (obj is MessagePart) {
204                                 return GetNamedItemDescription (obj, ((MessagePart)obj).Message);
205                         }
206                         else if (obj is Operation) {
207                                 return GetNamedItemDescription (obj, ((Operation)obj).PortType);
208                         }
209                         else if (obj is OperationBinding) {
210                                 return GetNamedItemDescription (obj, ((OperationBinding)obj).Binding);
211                         }
212                         else if (obj is OperationMessage) {
213                                 return GetNamedItemDescription (obj, ((OperationMessage)obj).Operation);
214                         }
215                         else if (obj is Port) {
216                                 return GetNamedItemDescription (obj, ((Port)obj).Service);
217                         }
218                         else if (obj is ServiceDescriptionFormatExtension) {
219                                 ServiceDescriptionFormatExtension ext = (ServiceDescriptionFormatExtension) obj;
220                                 return GetItemDescription (ext, ext.Parent, ext.GetType().Name);
221                         }
222                         else if (obj is XmlSchema) {
223                                 if (ServiceDescription == null) 
224                                         return "Schema '" + ((XmlSchema)obj).TargetNamespace + "'";
225                                 else
226                                         return "Schema '" + ((XmlSchema)obj).TargetNamespace + "', in " + GetDescription (ServiceDescription);
227                         }
228                         else if (obj is XmlSchemaObject) {
229                                 return obj.GetType().Name + " in Schema " + GetDescription (CurrentSchema);
230                         }
231                         return obj.GetType().Name;
232                 }
233                 
234                 string GetNamedItemDescription (object item, object parent)
235                 {
236                         return item.GetType().Name + " '" + ((NamedItem)item).Name + "', in " + GetDescription (parent);
237                 }
238                 
239                 string GetItemDescription (object item, object parent, string name)
240                 {
241                         return item.GetType().Name + " '" + name + "' in " + GetDescription (parent);
242                 }
243         }
244 }
245
246 #endif