* ConformanceChecker.cs, BasicProfileChecker.cs: New files that implement
[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
35 namespace System.Web.Services.Description 
36 {
37         internal abstract class ConformanceChecker
38         {
39                 public abstract WsiClaims Claims { get; }
40                 public virtual void Check (ConformanceCheckContext ctx, Binding value) { }
41                 public virtual void Check (ConformanceCheckContext ctx, MessageBinding value) { }
42                 public virtual void Check (ConformanceCheckContext ctx, Import value) { }
43                 public virtual void Check (ConformanceCheckContext ctx, Message value) { }
44                 public virtual void Check (ConformanceCheckContext ctx, MessagePart value) { }
45                 public virtual void Check (ConformanceCheckContext ctx, Operation value) { }
46                 public virtual void Check (ConformanceCheckContext ctx, OperationBinding value) { }
47                 public virtual void Check (ConformanceCheckContext ctx, OperationMessage value) { }
48                 public virtual void Check (ConformanceCheckContext ctx, Port value) { }
49                 public virtual void Check (ConformanceCheckContext ctx, PortType value) { }
50                 public virtual void Check (ConformanceCheckContext ctx, Service value) { }
51                 public virtual void Check (ConformanceCheckContext ctx, ServiceDescription value) { }
52                 public virtual void Check (ConformanceCheckContext ctx, Types value) { }
53                 public virtual void Check (ConformanceCheckContext ctx, ServiceDescriptionFormatExtension value) {}
54                 public virtual void Check (ConformanceCheckContext ctx, XmlSchemaObject value) {}
55         }
56         
57         internal class ConformanceRule
58         {
59                 public string NormativeStatement;
60                 public string Details;
61                 public string Recommendation;
62                 
63                 public ConformanceRule (string name, string desc, string rec)
64                 {
65                         NormativeStatement = name;
66                         Details = desc;
67                         Recommendation = rec;
68                 }
69         }
70         
71         internal class ConformanceCheckContext
72         {
73                 BasicProfileViolationCollection violations;
74                 ServiceDescriptionCollection collection;
75                 WebReference webReference;
76                 ConformanceChecker checker;             
77                 public ServiceDescription ServiceDescription;
78                 
79                 public ConformanceCheckContext (ServiceDescriptionCollection collection, BasicProfileViolationCollection violations)
80                 {
81                         this.collection = collection;
82                         this.violations = violations;
83                 }
84                 
85                 public ConformanceCheckContext (WebReference webReference, BasicProfileViolationCollection violations)
86                 {
87                         this.webReference = webReference;
88                         this.violations = violations;
89                 }
90                 
91                 public ConformanceChecker Checker {
92                         get { return checker; }
93                         set { checker = value; }
94                 }
95                 
96                 public BasicProfileViolationCollection Violations {
97                         get { return violations; }
98                 }
99                 
100                 public object GetDocument (string url)
101                 {
102                         if (collection != null)
103                                 return null;
104                         else
105                                 return webReference.Documents [url];
106                 }
107                 
108                 public void ReportError (object currentObject, string msg)
109                 {
110                         throw new InvalidOperationException (msg + " (" + GetDescription (currentObject) + ")");
111                 }
112                 
113                 public void ReportRuleViolation (object currentObject, ConformanceRule rule)
114                 {
115                         BasicProfileViolation v = null;
116                         foreach (BasicProfileViolation bpv in violations) {
117                                 if (bpv.NormativeStatement == rule.NormativeStatement) {
118                                         v = bpv;
119                                         break;
120                                 }
121                         }
122                         
123                         if (v == null) {
124                                 v = new BasicProfileViolation (checker.Claims, rule);
125                                 violations.Add (v);
126                         }
127                         
128                         v.Elements.Add (GetDescription (currentObject));
129                 }
130                 
131                 string GetDescription (object obj)
132                 {
133                         if (obj is ServiceDescription) {
134                                 return "Service Description '" + ServiceDescription.TargetNamespace + "'";
135                         }
136                         else if (obj is Binding || obj is Message || obj is PortType || obj is Service) {
137                                 return GetNamedItemDescription (obj, ServiceDescription);
138                         }
139                         else if (obj is Import) {
140                                 return GetItemDescription (obj, ServiceDescription, ((Import)obj).Location);
141                         }
142                         else if (obj is MessageBinding) {
143                                 return GetNamedItemDescription (obj, ((MessageBinding)obj).OperationBinding);
144                         }
145                         else if (obj is MessagePart) {
146                                 return GetNamedItemDescription (obj, ((MessagePart)obj).Message);
147                         }
148                         else if (obj is Operation) {
149                                 return GetNamedItemDescription (obj, ((Operation)obj).PortType);
150                         }
151                         else if (obj is OperationBinding) {
152                                 return GetNamedItemDescription (obj, ((OperationBinding)obj).Binding);
153                         }
154                         else if (obj is OperationMessage) {
155                                 return GetNamedItemDescription (obj, ((OperationMessage)obj).Operation);
156                         }
157                         else if (obj is Port) {
158                                 return GetNamedItemDescription (obj, ((Port)obj).Service);
159                         }
160                         else if (obj is ServiceDescriptionFormatExtension) {
161                                 ServiceDescriptionFormatExtension ext = (ServiceDescriptionFormatExtension) obj;
162                                 return GetItemDescription (ext, ext.Parent, ext.GetType().Name);
163                         }
164                         return obj.GetType().Name;
165                 }
166                 
167                 string GetNamedItemDescription (object item, object parent)
168                 {
169                         return item.GetType().Name + " '" + ((NamedItem)item).Name + "', in " + GetDescription (parent);
170                 }
171                 
172                 string GetItemDescription (object item, object parent, string name)
173                 {
174                         return item.GetType().Name + " '" + name + "' in " + GetDescription (parent);
175                 }
176         }
177 }
178
179 #endif