PageRenderTime 51ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/System.Web.Mvc2/System.Web.Mvc/ValueProviderDictionary.cs

https://github.com/iainlane/mono
C# | 208 lines | 159 code | 34 blank | 15 comment | 14 complexity | 6788e77fbd989665f0216c43b0a6703a MD5 | raw file
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation. All rights reserved.
  4. *
  5. * This software is subject to the Microsoft Public License (Ms-PL).
  6. * A copy of the license can be found in the license.htm file included
  7. * in this distribution.
  8. *
  9. * You must not remove this notice, or any other, from this software.
  10. *
  11. * ***************************************************************************/
  12. namespace System.Web.Mvc {
  13. using System;
  14. using System.Collections;
  15. using System.Collections.Generic;
  16. using System.Collections.Specialized;
  17. using System.Diagnostics.CodeAnalysis;
  18. using System.Globalization;
  19. using System.Web.Routing;
  20. [Obsolete("The recommended alternative is to use one of the specific ValueProvider types, such as FormValueProvider.")]
  21. public class ValueProviderDictionary : IDictionary<string, ValueProviderResult>, IValueProvider {
  22. private readonly Dictionary<string, ValueProviderResult> _dictionary = new Dictionary<string, ValueProviderResult>(StringComparer.OrdinalIgnoreCase);
  23. public ValueProviderDictionary(ControllerContext controllerContext) {
  24. ControllerContext = controllerContext;
  25. if (controllerContext != null) {
  26. PopulateDictionary();
  27. }
  28. }
  29. public ControllerContext ControllerContext {
  30. get;
  31. private set;
  32. }
  33. public int Count {
  34. get {
  35. return ((ICollection<KeyValuePair<string, ValueProviderResult>>)Dictionary).Count;
  36. }
  37. }
  38. internal Dictionary<string, ValueProviderResult> Dictionary {
  39. get {
  40. return _dictionary;
  41. }
  42. }
  43. public bool IsReadOnly {
  44. get {
  45. return ((ICollection<KeyValuePair<string, ValueProviderResult>>)Dictionary).IsReadOnly;
  46. }
  47. }
  48. public ICollection<string> Keys {
  49. get {
  50. return Dictionary.Keys;
  51. }
  52. }
  53. public ValueProviderResult this[string key] {
  54. get {
  55. ValueProviderResult result;
  56. Dictionary.TryGetValue(key, out result);
  57. return result;
  58. }
  59. set {
  60. Dictionary[key] = value;
  61. }
  62. }
  63. public ICollection<ValueProviderResult> Values {
  64. get {
  65. return Dictionary.Values;
  66. }
  67. }
  68. public void Add(KeyValuePair<string, ValueProviderResult> item) {
  69. ((ICollection<KeyValuePair<string, ValueProviderResult>>)Dictionary).Add(item);
  70. }
  71. public void Add(string key, object value) {
  72. string attemptedValue = Convert.ToString(value, CultureInfo.InvariantCulture);
  73. ValueProviderResult vpResult = new ValueProviderResult(value, attemptedValue, CultureInfo.InvariantCulture);
  74. Add(key, vpResult);
  75. }
  76. public void Add(string key, ValueProviderResult value) {
  77. Dictionary.Add(key, value);
  78. }
  79. private void AddToDictionaryIfNotPresent(string key, ValueProviderResult result) {
  80. if (!String.IsNullOrEmpty(key)) {
  81. if (!Dictionary.ContainsKey(key)) {
  82. Dictionary.Add(key, result);
  83. }
  84. }
  85. }
  86. public void Clear() {
  87. ((ICollection<KeyValuePair<string, ValueProviderResult>>)Dictionary).Clear();
  88. }
  89. public bool Contains(KeyValuePair<string, ValueProviderResult> item) {
  90. return ((ICollection<KeyValuePair<string, ValueProviderResult>>)Dictionary).Contains(item);
  91. }
  92. public bool ContainsKey(string key) {
  93. return Dictionary.ContainsKey(key);
  94. }
  95. public void CopyTo(KeyValuePair<string, ValueProviderResult>[] array, int arrayIndex) {
  96. ((ICollection<KeyValuePair<string, ValueProviderResult>>)Dictionary).CopyTo(array, arrayIndex);
  97. }
  98. public IEnumerator<KeyValuePair<string, ValueProviderResult>> GetEnumerator() {
  99. return ((IEnumerable<KeyValuePair<string, ValueProviderResult>>)Dictionary).GetEnumerator();
  100. }
  101. private void PopulateDictionary() {
  102. CultureInfo currentCulture = CultureInfo.CurrentCulture;
  103. CultureInfo invariantCulture = CultureInfo.InvariantCulture;
  104. // We use this order of precedence to populate the dictionary:
  105. // 1. Request form submission (should be culture-aware)
  106. // 2. Values from the RouteData (could be from the typed-in URL or from the route's default values)
  107. // 3. URI query string
  108. NameValueCollection form = ControllerContext.HttpContext.Request.Form;
  109. if (form != null) {
  110. string[] keys = form.AllKeys;
  111. foreach (string key in keys) {
  112. string[] rawValue = form.GetValues(key);
  113. string attemptedValue = form[key];
  114. ValueProviderResult result = new ValueProviderResult(rawValue, attemptedValue, currentCulture);
  115. AddToDictionaryIfNotPresent(key, result);
  116. }
  117. }
  118. RouteValueDictionary routeValues = ControllerContext.RouteData.Values;
  119. if (routeValues != null) {
  120. foreach (var kvp in routeValues) {
  121. string key = kvp.Key;
  122. object rawValue = kvp.Value;
  123. string attemptedValue = Convert.ToString(rawValue, invariantCulture);
  124. ValueProviderResult result = new ValueProviderResult(rawValue, attemptedValue, invariantCulture);
  125. AddToDictionaryIfNotPresent(key, result);
  126. }
  127. }
  128. NameValueCollection queryString = ControllerContext.HttpContext.Request.QueryString;
  129. if (queryString != null) {
  130. string[] keys = queryString.AllKeys;
  131. foreach (string key in keys) {
  132. string[] rawValue = queryString.GetValues(key);
  133. string attemptedValue = queryString[key];
  134. ValueProviderResult result = new ValueProviderResult(rawValue, attemptedValue, invariantCulture);
  135. AddToDictionaryIfNotPresent(key, result);
  136. }
  137. }
  138. }
  139. public bool Remove(KeyValuePair<string, ValueProviderResult> item) {
  140. return ((ICollection<KeyValuePair<string, ValueProviderResult>>)Dictionary).Remove(item);
  141. }
  142. public bool Remove(string key) {
  143. return Dictionary.Remove(key);
  144. }
  145. public bool TryGetValue(string key, out ValueProviderResult value) {
  146. return Dictionary.TryGetValue(key, out value);
  147. }
  148. #region IEnumerable Members
  149. IEnumerator IEnumerable.GetEnumerator() {
  150. return ((IEnumerable)Dictionary).GetEnumerator();
  151. }
  152. #endregion
  153. #region IValueProvider Members
  154. [SuppressMessage("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes",
  155. Justification = "The declaring type is obsolete, so there is little benefit to exposing this as a virtual method.")]
  156. bool IValueProvider.ContainsPrefix(string prefix) {
  157. if (prefix == null) {
  158. throw new ArgumentNullException("prefix");
  159. }
  160. return ValueProviderUtil.CollectionContainsPrefix(Keys, prefix);
  161. }
  162. [SuppressMessage("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes",
  163. Justification = "The declaring type is obsolete, so there is little benefit to exposing this as a virtual method.")]
  164. ValueProviderResult IValueProvider.GetValue(string key) {
  165. if (key == null) {
  166. throw new ArgumentNullException("key");
  167. }
  168. ValueProviderResult vpResult;
  169. TryGetValue(key, out vpResult);
  170. return vpResult;
  171. }
  172. #endregion
  173. }
  174. }