NameValueCollection.cs 11 KB

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