2 // System.Collections.Specialized.NameValueCollection.cs
7 // (C) Ximian, Inc. http://www.ximian.com
8 // Copyright (C) 2004-2005 Novell (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Runtime.Serialization;
35 namespace System.Collections.Specialized{
38 public class NameValueCollection : NameObjectCollectionBase
40 string[] cachedAllKeys = null;
41 string[] cachedAll = null;
43 //--------------------- Constructors -----------------------------
45 /// <summary> SDK: Initializes a new instance of the NameValueCollection class that is empty,
46 /// has the default initial capacity and uses the default case-insensitive hash code provider and the default case-insensitive comparer.
48 public NameValueCollection() : base()
52 /// <summary> SDK: Initializes a new instance of the NameValueCollection class that is empty,
53 /// has the specified initial capacity and uses the default case-insensitive hash code provider and the default case-insensitive comparer.
55 public NameValueCollection( int capacity ) : base(capacity)
59 /// <summary> SDK: Copies the entries from the specified NameValueCollection to a new
60 /// NameValueCollection with the same initial capacity as the number of entries copied
61 /// and using the same case-insensitive hash code provider and the same case-insensitive
62 /// comparer as the source collection.
64 /// TODO: uncomment constructor below after it will be possible to compile NameValueCollection and
65 /// NameObjectCollectionBase to the same assembly
67 public NameValueCollection( NameValueCollection col ) : base(col.HashCodeProvider,col.Comparer)
70 throw new ArgumentNullException ("col");
74 ///<summary>SDK: Initializes a new instance of the NameValueCollection class that is empty,
75 ///has the default initial capacity and uses the specified hash code provider and
76 ///the specified comparer.</summary>
78 [Obsolete ("Use NameValueCollection(IEqualityComparer)")]
80 public NameValueCollection( IHashCodeProvider hashProvider, IComparer comparer )
81 : base(hashProvider, comparer)
87 /// SDK: Copies the entries from the specified NameValueCollection to a new NameValueCollection
88 /// with the specified initial capacity or the same initial capacity as the number of entries
89 /// copied, whichever is greater, and using the default case-insensitive hash code provider and
90 /// the default case-insensitive comparer.
92 /// TODO: uncomment constructor below after it will be possible to compile NameValueCollection and
93 /// NameObjectCollectionBase to the same assembly
95 public NameValueCollection( int capacity, NameValueCollection col )
96 : base(capacity, col.HashCodeProvider, col.Comparer)
102 /// SDK: Initializes a new instance of the NameValueCollection class that is serializable
103 /// and uses the specified System.Runtime.Serialization.SerializationInfo and
104 /// System.Runtime.Serialization.StreamingContext.
106 protected NameValueCollection( SerializationInfo info, StreamingContext context )
113 /// SDK: Initializes a new instance of the NameValueCollection class that is empty,
114 /// has the specified initial capacity and uses the specified hash code provider and
115 /// the specified comparer.
118 [Obsolete ("Use NameValueCollection(IEqualityComparer)")]
120 public NameValueCollection( int capacity, IHashCodeProvider hashProvider, IComparer comparer )
121 :base(capacity, hashProvider, comparer)
127 public NameValueCollection (IEqualityComparer equalityComparer)
128 : base (equalityComparer)
132 public NameValueCollection (int capacity, IEqualityComparer equalityComparer)
133 : base (capacity, equalityComparer)
138 //----------------------- Public Instance Properties -------------------------------
142 /// Gets all the keys in the NameValueCollection.
143 /// The arrays returned by AllKeys are cached for better performance and are
144 /// automatically refreshed when the collection changes. A derived class can
145 /// invalidate the cached version by calling InvalidateCachedArrays, thereby
146 /// forcing the arrays to be recreated.
148 public virtual string[] AllKeys
151 if (cachedAllKeys==null)
152 cachedAllKeys = BaseGetAllKeys();
153 return this.cachedAllKeys;
157 public string this[ int index ]
160 return this.Get(index);
164 public string this[ string name ] {
166 return this.Get(name);
169 this.Set(name,value);
173 /////////////////////////////// Public Instance Methods //////////////////////////////
175 /// <summary> SDK: Copies the entries in the specified NameValueCollection
176 /// to the current NameValueCollection.</summary>
177 /// LAMESPEC: see description that comes this Add(string, string)
179 public void Add (NameValueCollection c)
182 throw new NotSupportedException ("Collection is read-only");
185 throw new ArgumentNullException ("c");
187 // make sense - but it's not the exception thrown
188 // throw new ArgumentNullException ();
190 InvalidateCachedArrays ();
192 for (int i=0; i < max; i++){
193 string key = c.GetKey (i);
194 ArrayList new_values = (ArrayList) c.BaseGet (i);
195 ArrayList values = (ArrayList) BaseGet (key);
196 if (values != null && new_values != null)
197 values.AddRange (new_values);
198 else if (new_values != null)
199 values = new ArrayList (new_values);
200 BaseSet (key, values);
205 /// <summary> SDK: Adds an entry with the specified name and value to the
206 /// NameValueCollection. </summary>
208 /// in SDK doc: If the same value already exists under the same key in the collection,
209 /// it just adds one more value in other words after
211 /// NameValueCollection nvc;
212 /// nvc.Add("LAZY","BASTARD")
213 /// nvc.Add("LAZY","BASTARD")
215 /// nvc.Get("LAZY") will be "BASTARD,BASTARD" instead of "BASTARD"
217 public virtual void Add( string name, string val )
221 throw new NotSupportedException("Collection is read-only");
223 InvalidateCachedArrays();
224 ArrayList values = (ArrayList)BaseGet(name);
226 values = new ArrayList();
229 BaseAdd(name,values);
238 /// <summary> SDK: Invalidates the cached arrays and removes all entries from
239 /// the NameValueCollection.</summary>
241 public virtual void Clear ()
247 throw new NotSupportedException("Collection is read-only");
248 InvalidateCachedArrays();
252 /// <summary> SDK: Copies the entire NameValueCollection to a compatible one-dimensional Array,
253 /// starting at the specified index of the target array.</summary>
255 public void CopyTo( Array dest, int index )
258 throw new ArgumentNullException("dest", "Null argument - dest");
260 throw new ArgumentOutOfRangeException("index", "index is less than 0");
262 throw new ArgumentException ("dest", "multidim");
266 cachedAll.CopyTo(dest, index);
269 private void RefreshCachedAll()
272 int max = this.Count;
273 cachedAll = new string[max];
274 for(int i=0;i<max;i++){
275 cachedAll[i] = this.Get(i);
280 /// <summary> SDK: Gets the values at the specified index of the NameValueCollection combined
281 /// into one comma-separated list.</summary>
283 public virtual string Get( int index )
285 ArrayList values = (ArrayList)BaseGet(index);
286 // if index is out of range BaseGet throws an ArgumentOutOfRangeException
288 return AsSingleString(values);
293 * SDK: Gets the values associated with the specified key from the NameValueCollection
294 * combined into one comma-separated list.
296 public virtual string Get( string name )
298 ArrayList values = (ArrayList)BaseGet(name);
300 Console.WriteLine("BaseGet returned null");*/
301 return AsSingleString(values);
302 // -------------------------------------------------------------
305 /// <summary></summary>
307 private static string AsSingleString(ArrayList values)
309 const char separator = ',';
313 int max = values.Count;
317 //TODO: reimplement this
318 StringBuilder sb = new StringBuilder((string)values[0]);
319 for (int i=1; i<max; i++){
320 sb.Append(separator);
321 sb.Append(values[i]);
324 return sb.ToString();
328 /// <summary>SDK: Gets the key at the specified index of the NameValueCollection.</summary>
329 public virtual string GetKey( int index )
331 return BaseGetKey(index);
335 /// <summary>SDK: Gets the values at the specified index of the NameValueCollection.</summary>
337 public virtual string[] GetValues( int index )
339 ArrayList values = (ArrayList)BaseGet(index);
341 return AsStringArray(values);
345 public virtual string[] GetValues( string name )
347 ArrayList values = (ArrayList)BaseGet(name);
349 return AsStringArray(values);
352 private static string[] AsStringArray(ArrayList values)
356 int max = values.Count;//get_Count();
360 string[] valArray =new string[max];
361 values.CopyTo(valArray);
367 /// SDK: Gets a value indicating whether the NameValueCollection contains keys that
368 /// are not a null reference
371 public bool HasKeys()
373 return BaseHasKeys();
376 public virtual void Remove( string name )
379 throw new NotSupportedException("Collection is read-only");
380 InvalidateCachedArrays();
386 /// Sets the value of an entry in the NameValueCollection.
388 public virtual void Set (string name, string value)
391 throw new NotSupportedException ("Collection is read-only");
393 InvalidateCachedArrays ();
395 ArrayList values = new ArrayList ();
398 BaseSet (name,values);
401 // remove all entries
402 BaseSet (name, null);
407 //---------------------- Protected Instance Methods ----------------------
409 protected void InvalidateCachedArrays()
411 cachedAllKeys = null;