NameValueCollection.cs 10.0 KB

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