NameValueCollection.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. [Serializable]
  14. public class NameValueCollection : NameObjectCollectionBase
  15. {
  16. string[] cachedAllKeys = null;
  17. string[] cachedAll = null;
  18. //--------------------- Constructors -----------------------------
  19. /// <summary> SDK: Initializes a new instance of the NameValueCollection class that is empty,
  20. /// has the default initial capacity and uses the default case-insensitive hash code provider and the default case-insensitive comparer.
  21. /// </summary>
  22. public NameValueCollection() : base()
  23. {
  24. }
  25. /// <summary> SDK: Initializes a new instance of the NameValueCollection class that is empty,
  26. /// has the specified initial capacity and uses the default case-insensitive hash code provider and the default case-insensitive comparer.
  27. ///</summary>
  28. public NameValueCollection( int capacity ) : base(capacity)
  29. {
  30. }
  31. /// <summary> SDK: Copies the entries from the specified NameValueCollection to a new
  32. /// NameValueCollection with the same initial capacity as the number of entries copied
  33. /// and using the same case-insensitive hash code provider and the same case-insensitive
  34. /// comparer as the source collection.
  35. /// </summary>
  36. /// TODO: uncomment constructor below after it will be possible to compile NameValueCollection and
  37. /// NameObjectCollectionBase to the same assembly
  38. public NameValueCollection( NameValueCollection col ) : base(col.HashCodeProvider,col.Comparer)
  39. {
  40. if (col==null)
  41. throw new ArgumentNullException("Null argument is not allowed");
  42. Add(col);
  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. {
  121. if (this.IsReadOnly)
  122. throw new NotSupportedException ("Collection is read-only");
  123. if (c == null)
  124. throw new ArgumentNullException ();
  125. InvalidateCachedArrays ();
  126. int max = c.Count;
  127. for (int i=0; i < max; i++){
  128. string key = c.GetKey (i);
  129. string [] values = c.GetValues (i);
  130. foreach (string value in values)
  131. Add (key, value);
  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. [MonoTODO]
  176. public void CopyTo( Array dest, int index )
  177. {
  178. if (dest==null)
  179. throw new ArgumentNullException("Null argument - dest");
  180. if (index<0)
  181. throw new ArgumentOutOfRangeException("index is less than 0");
  182. // throw new Exception("Not implemented yet");
  183. //TODO: add implementation here
  184. if (cachedAll==null)
  185. RefreshCachedAll();
  186. cachedAll.CopyTo(dest, index);
  187. }
  188. protected void RefreshCachedAll()
  189. {
  190. this.cachedAll=null;
  191. int max = this.Count;
  192. cachedAll = new string[max];
  193. for(int i=0;i<max;i++){
  194. cachedAll[i] = this.Get(i);
  195. }
  196. }
  197. /// <summary> SDK: Gets the values at the specified index of the NameValueCollection combined
  198. /// into one comma-separated list.</summary>
  199. public virtual string Get( int index )
  200. {
  201. ArrayList values = (ArrayList)BaseGet(index);
  202. // if index is out of range BaseGet throws an ArgumentOutOfRangeException
  203. return AsSingleString(values);
  204. }
  205. /**
  206. * SDK: Gets the values associated with the specified key from the NameValueCollection
  207. * combined into one comma-separated list.
  208. */
  209. public virtual string Get( string name )
  210. {
  211. ArrayList values = (ArrayList)BaseGet(name);
  212. /* if (values==null)
  213. Console.WriteLine("BaseGet returned null");*/
  214. return AsSingleString(values);
  215. // -------------------------------------------------------------
  216. }
  217. /// <summary></summary>
  218. [MonoTODO]
  219. private static string AsSingleString(ArrayList values)
  220. {
  221. const char separator = ',';
  222. if (values==null)
  223. return null;
  224. int max = values.Count;
  225. if (max==0)
  226. return null;
  227. //TODO: reimplement this
  228. StringBuilder sb = new StringBuilder((string)values[0]);
  229. for (int i=1; i<max; i++){
  230. sb.Append(separator);
  231. sb.Append(values[i]);
  232. }
  233. return sb.ToString();
  234. }
  235. /// <summary>SDK: Gets the key at the specified index of the NameValueCollection.</summary>
  236. public virtual string GetKey( int index )
  237. {
  238. return BaseGetKey(index);
  239. }
  240. /// <summary>SDK: Gets the values at the specified index of the NameValueCollection.</summary>
  241. public virtual string[] GetValues( int index )
  242. {
  243. ArrayList values = (ArrayList)BaseGet(index);
  244. return AsStringArray(values);
  245. }
  246. public virtual string[] GetValues( string name )
  247. {
  248. ArrayList values = (ArrayList)BaseGet(name);
  249. return AsStringArray(values);
  250. }
  251. private static string[] AsStringArray(ArrayList values)
  252. {
  253. if (values == null)
  254. return null;
  255. int max = values.Count;//get_Count();
  256. if (max==0)
  257. return null;
  258. string[] valArray =new string[max];
  259. values.CopyTo(valArray);
  260. return valArray;
  261. }
  262. /// <summary>
  263. /// SDK: Gets a value indicating whether the NameValueCollection contains keys that
  264. /// are not a null reference
  265. /// </summary>
  266. public bool HasKeys()
  267. {
  268. return BaseHasKeys();
  269. }
  270. public virtual void Remove( string name )
  271. {
  272. if (this.IsReadOnly)
  273. throw new NotSupportedException("Collection is read-only");
  274. InvalidateCachedArrays();
  275. BaseRemove(name);
  276. }
  277. /// <summary>
  278. /// Sets the value of an entry in the NameValueCollection.
  279. /// </summary>
  280. public virtual void Set( string name, string value )
  281. {
  282. if (this.IsReadOnly)
  283. throw new NotSupportedException("Collection is read-only");
  284. InvalidateCachedArrays();
  285. ArrayList values = new ArrayList();
  286. values.Add(value);
  287. BaseSet(name,values);
  288. }
  289. //---------------------- Protected Instance Methods ----------------------
  290. protected void InvalidateCachedArrays()
  291. {
  292. cachedAllKeys = null;
  293. cachedAll = null;
  294. }
  295. }
  296. }