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