ConformanceChecker.cs 6.1 KB

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