SchemaImporterExtensionCollection.cs 3.9 KB

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