UnrecognizedPolicyAssertionElement.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //------------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------------------------
  4. namespace System.ServiceModel.Configuration
  5. {
  6. using System.Collections.Generic;
  7. using System.Configuration;
  8. using System.Globalization;
  9. using System.Runtime;
  10. using System.ServiceModel.Channels;
  11. using System.ServiceModel.Description;
  12. using System.Text;
  13. using System.Xml;
  14. sealed partial class UnrecognizedPolicyAssertionElement : BindingElementExtensionElement
  15. {
  16. XmlQualifiedName wsdlBinding;
  17. ICollection<XmlElement> bindingAsserions;
  18. IDictionary<OperationDescription, ICollection<XmlElement>> operationAssertions;
  19. IDictionary<MessageDescription, ICollection<XmlElement>> messageAssertions;
  20. public override Type BindingElementType
  21. {
  22. get { return typeof(UnrecognizedAssertionsBindingElement); }
  23. }
  24. public override void CopyFrom(ServiceModelExtensionElement from)
  25. {
  26. base.CopyFrom(from);
  27. UnrecognizedPolicyAssertionElement source = (UnrecognizedPolicyAssertionElement)from;
  28. #pragma warning suppress 56506 //markg; base.CopyFrom() checks for 'from' being null
  29. this.wsdlBinding = source.wsdlBinding;
  30. this.bindingAsserions = source.bindingAsserions;
  31. this.operationAssertions = source.operationAssertions;
  32. this.messageAssertions = source.messageAssertions;
  33. }
  34. protected internal override BindingElement CreateBindingElement()
  35. {
  36. return new UnrecognizedAssertionsBindingElement(XmlQualifiedName.Empty, null);
  37. }
  38. protected internal override void InitializeFrom(BindingElement bindingElement)
  39. {
  40. base.InitializeFrom(bindingElement);
  41. UnrecognizedAssertionsBindingElement binding = (UnrecognizedAssertionsBindingElement)bindingElement;
  42. this.wsdlBinding = binding.WsdlBinding;
  43. this.bindingAsserions = binding.BindingAsserions;
  44. this.operationAssertions = binding.OperationAssertions;
  45. this.messageAssertions = binding.MessageAssertions;
  46. }
  47. protected override bool SerializeToXmlElement(XmlWriter writer, String elementName)
  48. {
  49. XmlDocument document = new XmlDocument();
  50. if (writer != null && this.bindingAsserions != null && this.bindingAsserions.Count > 0)
  51. {
  52. int indent = 1;
  53. XmlWriterSettings settings = WriterSettings(writer);
  54. Fx.Assert(this.wsdlBinding != null, "");
  55. WriteComment(SR.GetString(SR.UnrecognizedBindingAssertions1, this.wsdlBinding.Namespace), indent, writer, settings);
  56. WriteComment(String.Format(CultureInfo.InvariantCulture, "<wsdl:binding name='{0}'>", this.wsdlBinding.Name), indent, writer, settings);
  57. indent++;
  58. foreach (XmlElement assertion in this.bindingAsserions)
  59. {
  60. WriteComment(ToString(assertion, document), indent, writer, settings);
  61. }
  62. if (this.operationAssertions == null || this.operationAssertions.Count == 0)
  63. return true;
  64. foreach (OperationDescription operation in this.operationAssertions.Keys)
  65. {
  66. WriteComment(String.Format(CultureInfo.InvariantCulture, "<wsdl:operation name='{0}'>", operation.Name), indent, writer, settings);
  67. indent++;
  68. foreach (XmlElement assertion in this.operationAssertions[operation])
  69. {
  70. WriteComment(ToString(assertion, document), indent, writer, settings);
  71. }
  72. if (this.messageAssertions == null || this.messageAssertions.Count == 0)
  73. return true;
  74. foreach (MessageDescription message in operation.Messages)
  75. {
  76. ICollection<XmlElement> assertions;
  77. if (this.messageAssertions.TryGetValue(message, out assertions))
  78. {
  79. if (message.Direction == MessageDirection.Input)
  80. WriteComment("<wsdl:input>", indent, writer, settings);
  81. else if (message.Direction == MessageDirection.Output)
  82. WriteComment("<wsdl:output>", indent, writer, settings);
  83. foreach (XmlElement assertion in assertions)
  84. {
  85. WriteComment(ToString(assertion, document), indent + 1, writer, settings);
  86. }
  87. }
  88. }
  89. }
  90. return true;
  91. }
  92. return false;
  93. }
  94. protected override void Unmerge(ConfigurationElement sourceElement, ConfigurationElement parentElement, ConfigurationSaveMode saveMode)
  95. {
  96. if (sourceElement is UnrecognizedPolicyAssertionElement)
  97. {
  98. this.wsdlBinding = ((UnrecognizedPolicyAssertionElement)sourceElement).wsdlBinding;
  99. this.bindingAsserions = ((UnrecognizedPolicyAssertionElement)sourceElement).bindingAsserions;
  100. this.operationAssertions = ((UnrecognizedPolicyAssertionElement)sourceElement).operationAssertions;
  101. this.messageAssertions = ((UnrecognizedPolicyAssertionElement)sourceElement).messageAssertions;
  102. }
  103. base.Unmerge(sourceElement, parentElement, saveMode);
  104. }
  105. string ToString(XmlElement e, XmlDocument document)
  106. {
  107. XmlElement top = document.CreateElement(e.Prefix, e.LocalName, e.NamespaceURI);
  108. top.InsertBefore(document.CreateTextNode(".."), null);
  109. return top.OuterXml;
  110. }
  111. void WriteComment(string text, int indent, XmlWriter writer, XmlWriterSettings settings)
  112. {
  113. if (settings.Indent)
  114. {
  115. // indent is always > 0
  116. StringBuilder sb = new StringBuilder();
  117. for (int i = 0; i < indent; i++)
  118. {
  119. sb.Append(settings.IndentChars);
  120. }
  121. sb.Append(text);
  122. sb.Append(settings.IndentChars);
  123. text = sb.ToString();
  124. }
  125. writer.WriteComment(text);
  126. }
  127. XmlWriterSettings WriterSettings(XmlWriter writer)
  128. {
  129. if (writer.Settings == null)
  130. {
  131. // V1 writers
  132. XmlWriterSettings settings = new XmlWriterSettings();
  133. XmlTextWriter xmlTextWriter = writer as XmlTextWriter;
  134. if (xmlTextWriter != null)
  135. {
  136. settings.Indent = xmlTextWriter.Formatting == Formatting.Indented;
  137. if (settings.Indent && xmlTextWriter.Indentation > 0)
  138. {
  139. StringBuilder sb = new StringBuilder(xmlTextWriter.Indentation);
  140. for (int i = 0; i < xmlTextWriter.Indentation; i++)
  141. sb.Append(xmlTextWriter.IndentChar);
  142. settings.IndentChars = sb.ToString();
  143. }
  144. }
  145. return settings;
  146. }
  147. return writer.Settings;
  148. }
  149. }
  150. }