NameValueCollection.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. string [] values = c.GetValues (i);
  152. foreach (string value in values)
  153. Add (key, value);
  154. }
  155. }
  156. /// <summary> SDK: Adds an entry with the specified name and value to the
  157. /// NameValueCollection. </summary>
  158. ///
  159. /// in SDK doc: If the same value already exists under the same key in the collection,
  160. /// it just adds one more value in other words after
  161. /// <code>
  162. /// NameValueCollection nvc;
  163. /// nvc.Add("LAZY","BASTARD")
  164. /// nvc.Add("LAZY","BASTARD")
  165. /// </code>
  166. /// nvc.Get("LAZY") will be "BASTARD,BASTARD" instead of "BASTARD"
  167. public virtual void Add( string name, string val )
  168. {
  169. if (this.IsReadOnly)
  170. throw new NotSupportedException("Collection is read-only");
  171. InvalidateCachedArrays();
  172. ArrayList values = (ArrayList)BaseGet(name);
  173. if (values==null){
  174. values = new ArrayList();
  175. if (val!=null)
  176. values.Add(val);
  177. BaseAdd(name,values);
  178. }
  179. else {
  180. if (val!=null)
  181. values.Add(val);
  182. }
  183. }
  184. /// <summary> SDK: Invalidates the cached arrays and removes all entries from
  185. /// the NameValueCollection.</summary>
  186. public void Clear(){
  187. if (this.IsReadOnly)
  188. throw new NotSupportedException("Collection is read-only");
  189. InvalidateCachedArrays();
  190. BaseClear();
  191. }
  192. /// <summary> SDK: Copies the entire NameValueCollection to a compatible one-dimensional Array,
  193. /// starting at the specified index of the target array.</summary>
  194. public void CopyTo( Array dest, int index )
  195. {
  196. if (dest==null)
  197. throw new ArgumentNullException("dest", "Null argument - dest");
  198. if (index<0)
  199. throw new ArgumentOutOfRangeException("index", "index is less than 0");
  200. if (dest.Rank > 1)
  201. throw new ArgumentException ("dest", "multidim");
  202. if (cachedAll==null)
  203. RefreshCachedAll();
  204. cachedAll.CopyTo(dest, index);
  205. }
  206. private void RefreshCachedAll()
  207. {
  208. this.cachedAll=null;
  209. int max = this.Count;
  210. cachedAll = new string[max];
  211. for(int i=0;i<max;i++){
  212. cachedAll[i] = this.Get(i);
  213. }
  214. }
  215. /// <summary> SDK: Gets the values at the specified index of the NameValueCollection combined
  216. /// into one comma-separated list.</summary>
  217. public virtual string Get( int index )
  218. {
  219. ArrayList values = (ArrayList)BaseGet(index);
  220. // if index is out of range BaseGet throws an ArgumentOutOfRangeException
  221. return AsSingleString(values);
  222. }
  223. /**
  224. * SDK: Gets the values associated with the specified key from the NameValueCollection
  225. * combined into one comma-separated list.
  226. */
  227. public virtual string Get( string name )
  228. {
  229. ArrayList values = (ArrayList)BaseGet(name);
  230. /* if (values==null)
  231. Console.WriteLine("BaseGet returned null");*/
  232. return AsSingleString(values);
  233. // -------------------------------------------------------------
  234. }
  235. /// <summary></summary>
  236. [MonoTODO]
  237. private static string AsSingleString(ArrayList values)
  238. {
  239. const char separator = ',';
  240. if (values==null)
  241. return null;
  242. int max = values.Count;
  243. if (max==0)
  244. return null;
  245. //TODO: reimplement this
  246. StringBuilder sb = new StringBuilder((string)values[0]);
  247. for (int i=1; i<max; i++){
  248. sb.Append(separator);
  249. sb.Append(values[i]);
  250. }
  251. return sb.ToString();
  252. }
  253. /// <summary>SDK: Gets the key at the specified index of the NameValueCollection.</summary>
  254. public virtual string GetKey( int index )
  255. {
  256. return BaseGetKey(index);
  257. }
  258. /// <summary>SDK: Gets the values at the specified index of the NameValueCollection.</summary>
  259. public virtual string[] GetValues( int index )
  260. {
  261. ArrayList values = (ArrayList)BaseGet(index);
  262. return AsStringArray(values);
  263. }
  264. public virtual string[] GetValues( string name )
  265. {
  266. ArrayList values = (ArrayList)BaseGet(name);
  267. return AsStringArray(values);
  268. }
  269. private static string[] AsStringArray(ArrayList values)
  270. {
  271. if (values == null)
  272. return null;
  273. int max = values.Count;//get_Count();
  274. if (max==0)
  275. return null;
  276. string[] valArray =new string[max];
  277. values.CopyTo(valArray);
  278. return valArray;
  279. }
  280. /// <summary>
  281. /// SDK: Gets a value indicating whether the NameValueCollection contains keys that
  282. /// are not a null reference
  283. /// </summary>
  284. public bool HasKeys()
  285. {
  286. return BaseHasKeys();
  287. }
  288. public virtual void Remove( string name )
  289. {
  290. if (this.IsReadOnly)
  291. throw new NotSupportedException("Collection is read-only");
  292. InvalidateCachedArrays();
  293. BaseRemove(name);
  294. }
  295. /// <summary>
  296. /// Sets the value of an entry in the NameValueCollection.
  297. /// </summary>
  298. public virtual void Set (string name, string value)
  299. {
  300. if (this.IsReadOnly)
  301. throw new NotSupportedException ("Collection is read-only");
  302. InvalidateCachedArrays ();
  303. ArrayList values = new ArrayList ();
  304. if (value != null) {
  305. values.Add (value);
  306. BaseSet (name,values);
  307. }
  308. else {
  309. // remove all entries
  310. BaseSet (name, null);
  311. }
  312. }
  313. //---------------------- Protected Instance Methods ----------------------
  314. protected void InvalidateCachedArrays()
  315. {
  316. cachedAllKeys = null;
  317. cachedAll = null;
  318. }
  319. }
  320. }