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 public NameValueCollection () : base ()
49 public NameValueCollection (int capacity) : base (capacity)
53 public NameValueCollection (NameValueCollection col) : base (( col == null ) ? null : col.EqualityComparer ,
54 (col == null) ? null : col.Comparer,
55 (col == null) ? null : col.HashCodeProvider)
58 throw new ArgumentNullException ("col");
62 public NameValueCollection (NameValueCollection col) : base ((col == null) ? null : col.HashCodeProvider ,
63 (col == null) ? null : col.Comparer)
66 throw new NullReferenceException ();
72 [Obsolete ("Use NameValueCollection (IEqualityComparer)")]
74 public NameValueCollection (IHashCodeProvider hashProvider, IComparer comparer)
75 : base (hashProvider, comparer)
80 public NameValueCollection (int capacity, NameValueCollection col)
81 : base (capacity, (col == null) ? null : col.HashCodeProvider,
82 (col == null) ? null : col.Comparer)
87 protected NameValueCollection (SerializationInfo info, StreamingContext context)
94 [Obsolete ("Use NameValueCollection (IEqualityComparer)")]
96 public NameValueCollection (int capacity, IHashCodeProvider hashProvider, IComparer comparer)
97 :base (capacity, hashProvider, comparer)
103 public NameValueCollection (IEqualityComparer equalityComparer)
104 : base (equalityComparer)
108 public NameValueCollection (int capacity, IEqualityComparer equalityComparer)
109 : base (capacity, equalityComparer)
114 public virtual string[] AllKeys
117 if (cachedAllKeys == null)
118 cachedAllKeys = BaseGetAllKeys ();
119 return this.cachedAllKeys;
123 public string this [int index]
126 return this.Get (index);
130 public string this [string name] {
132 return this.Get (name);
135 this.Set (name,value);
139 public void Add (NameValueCollection c)
142 throw new NotSupportedException ("Collection is read-only");
145 throw new ArgumentNullException ("c");
147 // make sense - but it's not the exception thrown
148 // throw new ArgumentNullException ();
150 InvalidateCachedArrays ();
152 for (int i=0; i < max; i++){
153 string key = c.GetKey (i);
154 ArrayList new_values = (ArrayList) c.BaseGet (i);
155 ArrayList values = (ArrayList) BaseGet (key);
156 if (values != null && new_values != null)
157 values.AddRange (new_values);
158 else if (new_values != null)
159 values = new ArrayList (new_values);
160 BaseSet (key, values);
164 /// in SDK doc: If the same value already exists under the same key in the collection,
165 /// it just adds one more value in other words after
167 /// NameValueCollection nvc;
168 /// nvc.Add ("LAZY","BASTARD")
169 /// nvc.Add ("LAZY","BASTARD")
171 /// nvc.Get ("LAZY") will be "BASTARD,BASTARD" instead of "BASTARD"
173 public virtual void Add (string name, string val)
176 throw new NotSupportedException ("Collection is read-only");
178 InvalidateCachedArrays ();
179 ArrayList values = (ArrayList)BaseGet (name);
181 values = new ArrayList ();
184 BaseAdd (name, values);
194 public virtual void Clear ()
200 throw new NotSupportedException ("Collection is read-only");
201 InvalidateCachedArrays ();
205 public void CopyTo (Array dest, int index)
208 throw new ArgumentNullException ("dest", "Null argument - dest");
210 throw new ArgumentOutOfRangeException ("index", "index is less than 0");
212 throw new ArgumentException ("dest", "multidim");
214 if (cachedAll == null)
219 cachedAll.CopyTo (dest, index);
222 catch (ArrayTypeMismatchException)
224 throw new InvalidCastException();
229 private void RefreshCachedAll ()
231 this.cachedAll = null;
232 int max = this.Count;
233 cachedAll = new string [max];
234 for (int i = 0; i < max; i++)
235 cachedAll [i] = this.Get (i);
238 public virtual string Get (int index)
240 ArrayList values = (ArrayList)BaseGet (index);
241 // if index is out of range BaseGet throws an ArgumentOutOfRangeException
243 return AsSingleString (values);
246 public virtual string Get (string name)
248 ArrayList values = (ArrayList)BaseGet (name);
249 return AsSingleString (values);
252 private static string AsSingleString (ArrayList values)
254 const char separator = ',';
258 int max = values.Count;
262 //TODO: reimplement this
263 StringBuilder sb = new StringBuilder ((string)values [0]);
264 for (int i = 1; i < max; i++){
265 sb.Append (separator);
266 sb.Append (values [i]);
269 return sb.ToString ();
273 public virtual string GetKey (int index)
275 return BaseGetKey (index);
279 public virtual string[] GetValues (int index)
281 ArrayList values = (ArrayList)BaseGet (index);
283 return AsStringArray (values);
287 public virtual string[] GetValues (string name)
289 ArrayList values = (ArrayList)BaseGet (name);
291 return AsStringArray (values);
294 private static string[] AsStringArray (ArrayList values)
298 int max = values.Count;//get_Count ();
302 string[] valArray = new string[max];
303 values.CopyTo (valArray);
307 public bool HasKeys ()
309 return BaseHasKeys ();
312 public virtual void Remove (string name)
315 throw new NotSupportedException ("Collection is read-only");
316 InvalidateCachedArrays ();
321 public virtual void Set (string name, string value)
324 throw new NotSupportedException ("Collection is read-only");
326 InvalidateCachedArrays ();
328 ArrayList values = new ArrayList ();
331 BaseSet (name,values);
334 // remove all entries
335 BaseSet (name, null);
339 protected void InvalidateCachedArrays ()
341 cachedAllKeys = null;