ConformanceChecker.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. //
  2. // System.Web.Services.Description.ConformanceChecker.cs
  3. //
  4. // Author:
  5. // Lluis Sanchez ([email protected])
  6. //
  7. // Copyright (C) Novell, Inc., 2004
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0
  30. using System.Xml.Schema;
  31. using System.Xml.Serialization;
  32. namespace System.Web.Services.Description
  33. {
  34. internal abstract class ConformanceChecker
  35. {
  36. public abstract WsiClaims Claims { get; }
  37. public virtual void Check (ConformanceCheckContext ctx, Binding value) { }
  38. public virtual void Check (ConformanceCheckContext ctx, MessageBinding value) { }
  39. public virtual void Check (ConformanceCheckContext ctx, Import value) { }
  40. public virtual void Check (ConformanceCheckContext ctx, Message value) { }
  41. public virtual void Check (ConformanceCheckContext ctx, MessagePart value) { }
  42. public virtual void Check (ConformanceCheckContext ctx, Operation value) { }
  43. public virtual void Check (ConformanceCheckContext ctx, OperationBinding value) { }
  44. public virtual void Check (ConformanceCheckContext ctx, OperationMessage value) { }
  45. public virtual void Check (ConformanceCheckContext ctx, Port value) { }
  46. public virtual void Check (ConformanceCheckContext ctx, PortType value) { }
  47. public virtual void Check (ConformanceCheckContext ctx, Service value) { }
  48. public virtual void Check (ConformanceCheckContext ctx, ServiceDescription value) { }
  49. public virtual void Check (ConformanceCheckContext ctx, Types value) { }
  50. public virtual void Check (ConformanceCheckContext ctx, ServiceDescriptionFormatExtension value) {}
  51. public virtual void Check (ConformanceCheckContext ctx, XmlSchemaObject value) {}
  52. public virtual void Check (ConformanceCheckContext ctx, XmlSchema s) {}
  53. public virtual void Check (ConformanceCheckContext ctx, XmlSchemaImport value) {}
  54. public virtual void Check (ConformanceCheckContext ctx, XmlSchemaAll value) {}
  55. public virtual void Check (ConformanceCheckContext ctx, XmlSchemaAnnotation value) {}
  56. public virtual void Check (ConformanceCheckContext ctx, XmlSchemaAttribute value) {}
  57. public virtual void Check (ConformanceCheckContext ctx, XmlSchemaAttributeGroup value) {}
  58. public virtual void Check (ConformanceCheckContext ctx, XmlSchemaAttributeGroupRef value) {}
  59. public virtual void Check (ConformanceCheckContext ctx, XmlSchemaComplexContentExtension value) {}
  60. public virtual void Check (ConformanceCheckContext ctx, XmlSchemaChoice value) {}
  61. public virtual void Check (ConformanceCheckContext ctx, XmlSchemaComplexContent value) {}
  62. public virtual void Check (ConformanceCheckContext ctx, XmlSchemaComplexContentRestriction value) {}
  63. public virtual void Check (ConformanceCheckContext ctx, XmlSchemaComplexType value) {}
  64. public virtual void Check (ConformanceCheckContext ctx, XmlSchemaElement value) {}
  65. public virtual void Check (ConformanceCheckContext ctx, XmlSchemaGroup value) {}
  66. public virtual void Check (ConformanceCheckContext ctx, XmlSchemaGroupRef value) {}
  67. public virtual void Check (ConformanceCheckContext ctx, XmlSchemaIdentityConstraint value) {}
  68. public virtual void Check (ConformanceCheckContext ctx, XmlSchemaKeyref value) {}
  69. public virtual void Check (ConformanceCheckContext ctx, XmlSchemaRedefine value) {}
  70. public virtual void Check (ConformanceCheckContext ctx, XmlSchemaSequence value) {}
  71. public virtual void Check (ConformanceCheckContext ctx, XmlSchemaSimpleContent value) {}
  72. public virtual void Check (ConformanceCheckContext ctx, XmlSchemaSimpleContentExtension value) {}
  73. public virtual void Check (ConformanceCheckContext ctx, XmlSchemaSimpleContentRestriction value) {}
  74. public virtual void Check (ConformanceCheckContext ctx, XmlSchemaSimpleType value) {}
  75. public virtual void Check (ConformanceCheckContext ctx, XmlSchemaSimpleTypeList value) {}
  76. public virtual void Check (ConformanceCheckContext ctx, XmlSchemaSimpleTypeRestriction value) {}
  77. public virtual void Check (ConformanceCheckContext ctx, XmlSchemaSimpleTypeUnion value) {}
  78. }
  79. internal class ConformanceRule
  80. {
  81. public string NormativeStatement;
  82. public string Details;
  83. public string Recommendation;
  84. public ConformanceRule (string name, string desc, string rec)
  85. {
  86. NormativeStatement = name;
  87. Details = desc;
  88. Recommendation = rec;
  89. }
  90. }
  91. internal class ConformanceCheckContext
  92. {
  93. BasicProfileViolationCollection violations;
  94. ServiceDescriptionCollection collection;
  95. WebReference webReference;
  96. ConformanceChecker checker;
  97. public ServiceDescription ServiceDescription;
  98. public XmlSchema CurrentSchema;
  99. XmlSchemas schemas = new XmlSchemas ();
  100. ServiceDescriptionCollection services;
  101. public ConformanceCheckContext (ServiceDescriptionCollection collection, BasicProfileViolationCollection violations)
  102. {
  103. this.collection = collection;
  104. this.violations = violations;
  105. foreach (ServiceDescription sd in collection) {
  106. if (sd.Types != null && sd.Types.Schemas != null)
  107. schemas.Add (sd.Types.Schemas);
  108. }
  109. services = collection;
  110. }
  111. public ConformanceCheckContext (WebReference webReference, BasicProfileViolationCollection violations)
  112. {
  113. this.webReference = webReference;
  114. this.violations = violations;
  115. services = new ServiceDescriptionCollection ();
  116. foreach (object doc in webReference.Documents.Values)
  117. {
  118. if (doc is XmlSchema)
  119. schemas.Add ((XmlSchema)doc);
  120. else if (doc is ServiceDescription) {
  121. ServiceDescription sd = (ServiceDescription) doc;
  122. services.Add (sd);
  123. if (sd.Types != null && sd.Types.Schemas != null)
  124. schemas.Add (sd.Types.Schemas);
  125. }
  126. }
  127. }
  128. public ConformanceChecker Checker {
  129. get { return checker; }
  130. set { checker = value; }
  131. }
  132. public BasicProfileViolationCollection Violations {
  133. get { return violations; }
  134. }
  135. public XmlSchemas Schemas {
  136. get { return schemas; }
  137. }
  138. public ServiceDescriptionCollection Services {
  139. get { return services; }
  140. }
  141. public object GetDocument (string url)
  142. {
  143. if (collection != null)
  144. return null;
  145. else
  146. return webReference.Documents [url];
  147. }
  148. public void ReportError (object currentObject, string msg)
  149. {
  150. throw new InvalidOperationException (msg + " (" + GetDescription (currentObject) + ")");
  151. }
  152. public void ReportRuleViolation (object currentObject, ConformanceRule rule)
  153. {
  154. BasicProfileViolation v = null;
  155. foreach (BasicProfileViolation bpv in violations) {
  156. if (bpv.NormativeStatement == rule.NormativeStatement) {
  157. v = bpv;
  158. break;
  159. }
  160. }
  161. if (v == null) {
  162. v = new BasicProfileViolation (checker.Claims, rule);
  163. violations.Add (v);
  164. }
  165. v.Elements.Add (GetDescription (currentObject));
  166. }
  167. string GetDescription (object obj)
  168. {
  169. if (obj is ServiceDescription) {
  170. return "Service Description '" + ServiceDescription.TargetNamespace + "'";
  171. }
  172. else if (obj is Binding || obj is Message || obj is PortType || obj is Service) {
  173. return GetNamedItemDescription (obj, ServiceDescription);
  174. }
  175. else if (obj is Import) {
  176. return GetItemDescription (obj, ServiceDescription, ((Import)obj).Location);
  177. }
  178. else if (obj is MessageBinding) {
  179. return GetNamedItemDescription (obj, ((MessageBinding)obj).OperationBinding);
  180. }
  181. else if (obj is MessagePart) {
  182. return GetNamedItemDescription (obj, ((MessagePart)obj).Message);
  183. }
  184. else if (obj is Operation) {
  185. return GetNamedItemDescription (obj, ((Operation)obj).PortType);
  186. }
  187. else if (obj is OperationBinding) {
  188. return GetNamedItemDescription (obj, ((OperationBinding)obj).Binding);
  189. }
  190. else if (obj is OperationMessage) {
  191. return GetNamedItemDescription (obj, ((OperationMessage)obj).Operation);
  192. }
  193. else if (obj is Port) {
  194. return GetNamedItemDescription (obj, ((Port)obj).Service);
  195. }
  196. else if (obj is ServiceDescriptionFormatExtension) {
  197. ServiceDescriptionFormatExtension ext = (ServiceDescriptionFormatExtension) obj;
  198. return GetItemDescription (ext, ext.Parent, ext.GetType().Name);
  199. }
  200. else if (obj is XmlSchema) {
  201. if (ServiceDescription == null)
  202. return "Schema '" + ((XmlSchema)obj).TargetNamespace + "'";
  203. else
  204. return "Schema '" + ((XmlSchema)obj).TargetNamespace + "', in " + GetDescription (ServiceDescription);
  205. }
  206. else if (obj is XmlSchemaObject) {
  207. return obj.GetType().Name + " in Schema " + GetDescription (CurrentSchema);
  208. }
  209. return obj.GetType().Name;
  210. }
  211. string GetNamedItemDescription (object item, object parent)
  212. {
  213. return item.GetType().Name + " '" + ((NamedItem)item).Name + "', in " + GetDescription (parent);
  214. }
  215. string GetItemDescription (object item, object parent, string name)
  216. {
  217. return item.GetType().Name + " '" + name + "' in " + GetDescription (parent);
  218. }
  219. }
  220. }
  221. #endif