SchemaImporterExtensionCollection.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // System.Xml.Serialization.SchemaImporterExtensionCollection.cs
  3. //
  4. // Author:
  5. // Lluis Sanchez Gual ([email protected])
  6. // Atsushi Enomoto ([email protected])
  7. //
  8. // Copyright (C) Novell, Inc., 2004, 2006
  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. using System;
  31. using System.Collections;
  32. using System.Collections.Generic;
  33. namespace System.Xml.Serialization.Advanced
  34. {
  35. public class SchemaImporterExtensionCollection : CollectionBase
  36. {
  37. Dictionary<string,SchemaImporterExtension> named_items =
  38. new Dictionary<string,SchemaImporterExtension> ();
  39. public SchemaImporterExtensionCollection ()
  40. {
  41. }
  42. public int Add (SchemaImporterExtension extension)
  43. {
  44. if (extension == null)
  45. throw new ArgumentNullException ("extension");
  46. return List.Add (extension);
  47. }
  48. public int Add (string name, Type type)
  49. {
  50. if (name == null)
  51. throw new ArgumentNullException ("name");
  52. if (type == null)
  53. throw new ArgumentNullException ("type");
  54. if (!type.IsSubclassOf (typeof (SchemaImporterExtension)))
  55. throw new ArgumentException ("The type argument must be subclass of SchemaImporterExtension.");
  56. SchemaImporterExtension e = (SchemaImporterExtension) Activator.CreateInstance (type);
  57. if (named_items.ContainsKey (name))
  58. throw new InvalidOperationException (String.Format ("A SchemaImporterExtension keyed by '{0}' already exists.", name));
  59. int ret = Add (e);
  60. named_items.Add (name, e);
  61. return ret;
  62. }
  63. public new void Clear ()
  64. {
  65. named_items.Clear ();
  66. List.Clear ();
  67. }
  68. public bool Contains (SchemaImporterExtension extension)
  69. {
  70. if (extension == null)
  71. throw new ArgumentNullException ("extension");
  72. foreach (SchemaImporterExtension e in List)
  73. if (extension.Equals (e))
  74. return true;
  75. return false;
  76. }
  77. public void CopyTo (SchemaImporterExtension [] array, int index)
  78. {
  79. List.CopyTo (array, index);
  80. }
  81. public int IndexOf (SchemaImporterExtension extension)
  82. {
  83. if (extension == null)
  84. throw new ArgumentNullException ("extension");
  85. int i = 0;
  86. foreach (SchemaImporterExtension e in List) {
  87. if (extension.Equals (e)) {
  88. return i;
  89. }
  90. i++;
  91. }
  92. return -1;
  93. }
  94. public void Insert (int index, SchemaImporterExtension extension)
  95. {
  96. if (extension == null)
  97. throw new ArgumentNullException ("extension");
  98. List.Insert (index, extension);
  99. }
  100. public void Remove (SchemaImporterExtension extension)
  101. {
  102. int idx = IndexOf (extension);
  103. if (idx >= 0)
  104. List.RemoveAt (idx);
  105. }
  106. public void Remove (string name)
  107. {
  108. if (name == null)
  109. throw new ArgumentNullException ("name");
  110. if (!named_items.ContainsKey (name))
  111. return;
  112. SchemaImporterExtension e = named_items [name];
  113. Remove (e);
  114. named_items.Remove (name);
  115. }
  116. public SchemaImporterExtension this [int index]
  117. {
  118. get { return (SchemaImporterExtension) List [index]; }
  119. set { List [index] = value; }
  120. }
  121. }
  122. }