NameValueCollection.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /**
  2. * System.Collections.Specialized.NamaValueCollection class implementation
  3. *
  4. * Author: Gleb Novodran
  5. */
  6. // created on 7/21/2001 at 5:15 PM
  7. using System;
  8. using System.Collections;
  9. using System.Collections.Specialized;
  10. using System.Runtime.Serialization;
  11. using System.Text;
  12. namespace System.Collections.Specialized{
  13. public class NameValueCollection : NameObjectCollectionBase
  14. {
  15. string[] cachedAllKeys = null;
  16. string[] cachedAll = null;
  17. //--------------------- Constructors -----------------------------
  18. /// <summary> SDK: Initializes a new instance of the NameValueCollection class that is empty,
  19. /// has the default initial capacity and uses the default case-insensitive hash code provider and the default case-insensitive comparer.
  20. /// </summary>
  21. public NameValueCollection() : base()
  22. {
  23. }
  24. /// <summary> SDK: Initializes a new instance of the NameValueCollection class that is empty,
  25. /// has the specified initial capacity and uses the default case-insensitive hash code provider and the default case-insensitive comparer.
  26. ///</summary>
  27. public NameValueCollection( int capacity ) : base(capacity)
  28. {
  29. }
  30. /// <summary> SDK: Copies the entries from the specified NameValueCollection to a new
  31. /// NameValueCollection with the same initial capacity as the number of entries copied
  32. /// and using the same case-insensitive hash code provider and the same case-insensitive
  33. /// comparer as the source collection.
  34. /// </summary>
  35. /// TODO: uncomment constructor below after it will be possible to compile NameValueCollection and
  36. /// NameObjectCollectionBase to the same assembly
  37. /* public NameValueCollection( NameValueCollection col ) : base(col.HashCodeProvider,col.Comparer)
  38. {
  39. if (col==null)
  40. throw new ArgumentNullException("Null argument is not allowed");
  41. Add(col);
  42. }
  43. */
  44. ///<summary>SDK: Initializes a new instance of the NameValueCollection class that is empty,
  45. ///has the default initial capacity and uses the specified hash code provider and
  46. ///the specified comparer.</summary>
  47. public NameValueCollection( IHashCodeProvider hashProvider, IComparer comparer )
  48. : base(hashProvider, comparer)
  49. {
  50. }
  51. /// <summary>
  52. /// SDK: Copies the entries from the specified NameValueCollection to a new NameValueCollection
  53. /// with the specified initial capacity or the same initial capacity as the number of entries
  54. /// copied, whichever is greater, and using the default case-insensitive hash code provider and
  55. /// the default case-insensitive comparer.
  56. /// </summary>
  57. /// TODO: uncomment constructor below after it will be possible to compile NameValueCollection and
  58. /// NameObjectCollectionBase to the same assembly
  59. /* public NameValueCollection( int capacity, NameValueCollection col )
  60. : base(capacity, col.get_HashCodeProvider(),col.Comparer)
  61. {
  62. if (col==null)
  63. throw new ArgumentNullException("Null argument is not allowed");
  64. Add(col);
  65. }
  66. */
  67. /// <summary>
  68. /// SDK: Initializes a new instance of the NameValueCollection class that is serializable
  69. /// and uses the specified System.Runtime.Serialization.SerializationInfo and
  70. /// System.Runtime.Serialization.StreamingContext.
  71. /// </summary>
  72. protected NameValueCollection( SerializationInfo info, StreamingContext context )
  73. :base(info, context)
  74. {
  75. }
  76. /// <summary>
  77. /// SDK: Initializes a new instance of the NameValueCollection class that is empty,
  78. /// has the specified initial capacity and uses the specified hash code provider and
  79. /// the specified comparer.
  80. /// </summary>
  81. public NameValueCollection( int capacity, IHashCodeProvider hashProvider, IComparer comparer )
  82. :base(capacity, hashProvider, comparer)
  83. {
  84. }
  85. //----------------------- Public Instance Properties -------------------------------
  86. ///<summary> SDK:
  87. /// Gets all the keys in the NameValueCollection.
  88. /// The arrays returned by AllKeys are cached for better performance and are
  89. /// automatically refreshed when the collection changes. A derived class can
  90. /// invalidate the cached version by calling InvalidateCachedArrays, thereby
  91. /// forcing the arrays to be recreated.
  92. /// </summary>
  93. public virtual string[] AllKeys
  94. {
  95. get {
  96. if (cachedAllKeys==null)
  97. cachedAllKeys = BaseGetAllKeys();
  98. return this.cachedAllKeys;
  99. }
  100. }
  101. public string this[ int index ]
  102. {
  103. get{
  104. return this.Get(index);
  105. }
  106. }
  107. public string this[ string name ] {
  108. get{
  109. return this.Get(name);
  110. }
  111. set{
  112. this.Set(name,value);
  113. }
  114. }
  115. /////////////////////////////// Public Instance Methods //////////////////////////////
  116. /// <summary> SDK: Copies the entries in the specified NameValueCollection
  117. /// to the current NameValueCollection.</summary>
  118. /// LAMESPEC: see description that comes this Add(string, string)
  119. public void Add( NameValueCollection c ){
  120. if (this.IsReadOnly)
  121. throw new NotSupportedException("Collection is read-only");
  122. if (c==null)
  123. throw new ArgumentNullException();
  124. InvalidateCachedArrays();
  125. ArrayList values = null;
  126. int max = c.Count;
  127. for(int i=0; i<max; i++){
  128. values=(ArrayList)BaseGet(c.GetKey(i));
  129. if (values==null)
  130. values = new ArrayList();
  131. values.AddRange((ArrayList)c.BaseGet(i));
  132. }
  133. }
  134. /// <summary> SDK: Adds an entry with the specified name and value to the
  135. /// NameValueCollection. </summary>
  136. ///
  137. /// LAMESPEC:
  138. /// in SDK doc: If the same value already exists under the same key in the collection,
  139. /// the new value overwrites the old value.
  140. /// however the Microsoft implemenatation in this case just adds one more value
  141. /// in other words after
  142. /// <code>
  143. /// NameValueCollection nvc;
  144. /// nvc.Add("LAZY","BASTARD")
  145. /// nvc.Add("LAZY","BASTARD")
  146. /// </code>
  147. /// nvc.Get("LAZY") will be "BASTARD,BASTARD" instead of "BASTARD"
  148. public virtual void Add( string name, string val )
  149. {
  150. if (this.IsReadOnly)
  151. throw new NotSupportedException("Collection is read-only");
  152. InvalidateCachedArrays();
  153. ArrayList values = (ArrayList)BaseGet(name);
  154. if (values==null){
  155. values = new ArrayList();
  156. if (val!=null)
  157. values.Add(val);
  158. BaseAdd(name,values);
  159. }
  160. else {
  161. if (val!=null)
  162. values.Add(val);
  163. }
  164. }
  165. /// <summary> SDK: Invalidates the cached arrays and removes all entries from
  166. /// the NameValueCollection.</summary>
  167. public void Clear(){
  168. if (this.IsReadOnly)
  169. throw new NotSupportedException("Collection is read-only");
  170. InvalidateCachedArrays();
  171. BaseClear();
  172. }
  173. /// <summary> SDK: Copies the entire NameValueCollection to a compatible one-dimensional Array,
  174. /// starting at the specified index of the target array.</summary>
  175. override public void CopyTo( Array dest, int index )
  176. {
  177. if (dest==null)
  178. throw new ArgumentNullException("Null argument - dest");
  179. if (index<0)
  180. throw new ArgumentOutOfRangeException("index is less than 0");
  181. // throw new Exception("Not implemented yet");
  182. //TODO: add implementation here
  183. if (cachedAll==null)
  184. RefreshCachedAll();
  185. cachedAll.CopyTo(dest, index);
  186. }
  187. protected void RefreshCachedAll()
  188. {
  189. this.cachedAll=null;
  190. int max = this.Count;
  191. cachedAll = new string[max];
  192. for(int i=0;i<max;i++){
  193. cachedAll[i] = this.Get(i);
  194. }
  195. }
  196. /// <summary> SDK: Gets the values at the specified index of the NameValueCollection combined
  197. /// into one comma-separated list.</summary>
  198. public virtual string Get( int index )
  199. {
  200. ArrayList values = (ArrayList)BaseGet(index);
  201. // if index is out of range BaseGet throws an ArgumentOutOfRangeException
  202. return AsSingleString(values);
  203. }
  204. /**
  205. * SDK: Gets the values associated with the specified key from the NameValueCollection
  206. * combined into one comma-separated list.
  207. */
  208. public virtual string Get( string name )
  209. {
  210. ArrayList values = (ArrayList)BaseGet(name);
  211. /* if (values==null)
  212. Console.WriteLine("BaseGet returned null");*/
  213. return AsSingleString(values);
  214. // -------------------------------------------------------------
  215. }
  216. /// <summary></summary>
  217. private static string AsSingleString(ArrayList values)
  218. {
  219. const char separator = ',';
  220. if (values==null)
  221. return null;
  222. int max = values.Count;
  223. if (max==0)
  224. return null;
  225. //TODO: reimplement this
  226. StringBuilder sb = new StringBuilder((string)values[0]);
  227. for (int i=1; i<max; i++){
  228. sb.Append(separator);
  229. sb.Append(values[i]);
  230. }
  231. return sb.ToString();
  232. }
  233. /// <summary>SDK: Gets the key at the specified index of the NameValueCollection.</summary>
  234. public virtual string GetKey( int index )
  235. {
  236. return BaseGetKey(index);
  237. }
  238. /// <summary>SDK: Gets the values at the specified index of the NameValueCollection.</summary>
  239. public virtual string[] GetValues( int index )
  240. {
  241. ArrayList values = (ArrayList)BaseGet(index);
  242. return AsStringArray(values);
  243. }
  244. public virtual string[] GetValues( string name )
  245. {
  246. ArrayList values = (ArrayList)BaseGet(name);
  247. return AsStringArray(values);
  248. }
  249. private static string[] AsStringArray(ArrayList values){
  250. int max = values.Count;//get_Count();
  251. if (max==0)
  252. return null;
  253. string[] valArray =new string[max];
  254. values.CopyTo(valArray);
  255. return valArray;
  256. }
  257. /// <summary>
  258. /// SDK: Gets a value indicating whether the NameValueCollection contains keys that
  259. /// are not a null reference
  260. /// </summary>
  261. public bool HasKeys()
  262. {
  263. return BaseHasKeys();
  264. }
  265. public virtual void Remove( string name )
  266. {
  267. if (this.IsReadOnly)
  268. throw new NotSupportedException("Collection is read-only");
  269. InvalidateCachedArrays();
  270. BaseRemove(name);
  271. }
  272. /// <summary>
  273. /// Sets the value of an entry in the NameValueCollection.
  274. /// </summary>
  275. public virtual void Set( string name, string value )
  276. {
  277. if (this.IsReadOnly)
  278. throw new NotSupportedException("Collection is read-only");
  279. InvalidateCachedArrays();
  280. ArrayList values = new ArrayList();
  281. values.Add(value);
  282. BaseSet(name,values);
  283. }
  284. //---------------------- Protected Instance Methods ----------------------
  285. protected void InvalidateCachedArrays()
  286. {
  287. cachedAllKeys = null;
  288. cachedAll = null;
  289. }
  290. }
  291. }