New test.
[mono.git] / mcs / class / System.Web.Mvc2 / System.Web.Mvc / NameValueCollectionValueProvider.cs
1 /* ****************************************************************************\r
2  *\r
3  * Copyright (c) Microsoft Corporation. All rights reserved.\r
4  *\r
5  * This software is subject to the Microsoft Public License (Ms-PL). \r
6  * A copy of the license can be found in the license.htm file included \r
7  * in this distribution.\r
8  *\r
9  * You must not remove this notice, or any other, from this software.\r
10  *\r
11  * ***************************************************************************/\r
12 \r
13 namespace System.Web.Mvc {\r
14     using System;\r
15     using System.Collections.Generic;\r
16     using System.Collections.Specialized;\r
17     using System.Globalization;\r
18     using System.Linq;\r
19 \r
20     public class NameValueCollectionValueProvider : IValueProvider {\r
21 \r
22         private readonly HashSet<string> _prefixes = new HashSet<string>(StringComparer.OrdinalIgnoreCase);\r
23         private readonly Dictionary<string, ValueProviderResult> _values = new Dictionary<string, ValueProviderResult>(StringComparer.OrdinalIgnoreCase);\r
24 \r
25         public NameValueCollectionValueProvider(NameValueCollection collection, CultureInfo culture) {\r
26             if (collection == null) {\r
27                 throw new ArgumentNullException("collection");\r
28             }\r
29 \r
30             AddValues(collection, culture);\r
31         }\r
32 \r
33         private void AddValues(NameValueCollection collection, CultureInfo culture) {\r
34             if (collection.Count > 0) {\r
35                 _prefixes.Add("");\r
36             }\r
37 \r
38             foreach (string key in collection) {\r
39                 if (key != null) {\r
40                     _prefixes.UnionWith(ValueProviderUtil.GetPrefixes(key));\r
41 \r
42                     string[] rawValue = collection.GetValues(key);\r
43                     string attemptedValue = collection[key];\r
44                     _values[key] = new ValueProviderResult(rawValue, attemptedValue, culture);\r
45                 }\r
46             }\r
47         }\r
48 \r
49         public virtual bool ContainsPrefix(string prefix) {\r
50             if (prefix == null) {\r
51                 throw new ArgumentNullException("prefix");\r
52             }\r
53 \r
54             return _prefixes.Contains(prefix);\r
55         }\r
56 \r
57         public virtual ValueProviderResult GetValue(string key) {\r
58             if (key == null) {\r
59                 throw new ArgumentNullException("key");\r
60             }\r
61 \r
62             ValueProviderResult vpResult;\r
63             _values.TryGetValue(key, out vpResult);\r
64             return vpResult;\r
65         }\r
66 \r
67     }\r
68 }\r