PolicyAssertionCollection.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Description
  5. {
  6. using System.ServiceModel;
  7. using System.ComponentModel;
  8. using System.Collections.Generic;
  9. using System.Collections.ObjectModel;
  10. using System.Runtime.Serialization;
  11. using System.Xml;
  12. public class PolicyAssertionCollection : Collection<XmlElement>
  13. {
  14. public PolicyAssertionCollection()
  15. {
  16. }
  17. public PolicyAssertionCollection(IEnumerable<XmlElement> elements)
  18. {
  19. if (elements == null)
  20. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("elements");
  21. AddRange(elements);
  22. }
  23. internal void AddRange(IEnumerable<XmlElement> elements)
  24. {
  25. foreach (XmlElement element in elements)
  26. {
  27. base.Add(element);
  28. }
  29. }
  30. public bool Contains(string localName, string namespaceUri)
  31. {
  32. if (localName == null)
  33. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("localName");
  34. if (namespaceUri == null)
  35. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("namespaceUri");
  36. for (int i = 0; i < this.Count; i++)
  37. {
  38. XmlElement item = this[i];
  39. if (item.LocalName == localName && item.NamespaceURI == namespaceUri)
  40. return true;
  41. }
  42. return false;
  43. }
  44. public XmlElement Find(string localName, string namespaceUri)
  45. {
  46. return Find(localName, namespaceUri, false);
  47. }
  48. public XmlElement Remove(string localName, string namespaceUri)
  49. {
  50. return Find(localName, namespaceUri, true);
  51. }
  52. XmlElement Find(string localName, string namespaceUri, bool remove)
  53. {
  54. if (localName == null)
  55. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("localName");
  56. if (namespaceUri == null)
  57. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("namespaceUri");
  58. for (int index = 0; index < this.Count; index++)
  59. {
  60. XmlElement item = this[index];
  61. if (item.LocalName == localName && item.NamespaceURI == namespaceUri)
  62. {
  63. if (remove)
  64. {
  65. RemoveAt(index);
  66. }
  67. return item;
  68. }
  69. }
  70. return null;
  71. }
  72. public Collection<XmlElement> FindAll(string localName, string namespaceUri)
  73. {
  74. return FindAll(localName, namespaceUri, false);
  75. }
  76. public Collection<XmlElement> RemoveAll(string localName, string namespaceUri)
  77. {
  78. return FindAll(localName, namespaceUri, true);
  79. }
  80. Collection<XmlElement> FindAll(string localName, string namespaceUri, bool remove)
  81. {
  82. if (localName == null)
  83. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("localName");
  84. if (namespaceUri == null)
  85. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("namespaceUri");
  86. Collection<XmlElement> collection = new Collection<XmlElement>();
  87. for (int index = 0; index < this.Count; index++)
  88. {
  89. XmlElement item = this[index];
  90. if (item.LocalName == localName && item.NamespaceURI == namespaceUri)
  91. {
  92. if (remove)
  93. {
  94. RemoveAt(index);
  95. // back up the index so we inspect the new item at this location
  96. index--;
  97. }
  98. collection.Add(item);
  99. }
  100. }
  101. return collection;
  102. }
  103. protected override void InsertItem(int index, XmlElement item)
  104. {
  105. if (item == null)
  106. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("item");
  107. base.InsertItem(index, item);
  108. }
  109. protected override void SetItem(int index, XmlElement item)
  110. {
  111. if (item == null)
  112. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("item");
  113. base.SetItem(index, item);
  114. }
  115. }
  116. }