StateBag.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /**
  2. * Namespace: System.Web.UI
  3. * Class: StateBag
  4. *
  5. * Author: Gaurav Vaish
  6. * Maintainer: [email protected]
  7. * Implementation: yes
  8. * Contact: <[email protected]>
  9. * Status: 100%
  10. *
  11. * (C) Gaurav Vaish (2001)
  12. */
  13. using System;
  14. using System.Web;
  15. using System.Collections;
  16. using System.Collections.Specialized;
  17. namespace System.Web.UI
  18. {
  19. public sealed class StateBag : IStateManager, IDictionary, ICollection, IEnumerable
  20. {
  21. private bool ignoreCase;
  22. private bool marked;
  23. private IDictionary bag;
  24. public StateBag(bool ignoreCase)
  25. {
  26. Initialize(ignoreCase);
  27. }
  28. public StateBag()
  29. {
  30. Initialize(false);
  31. }
  32. private void Initialize(bool ignoreCase)
  33. {
  34. this.ignoreCase = ignoreCase;
  35. marked = false;
  36. bag = new HybridDictionary(ignoreCase);
  37. }
  38. public int Count
  39. {
  40. get
  41. {
  42. return bag.Count;
  43. }
  44. }
  45. public object this[object key]
  46. {
  47. get
  48. {
  49. string sKey = (string)key;
  50. if(sKey==null || sKey.Length==0)
  51. throw new ArgumentException(HttpRuntime.FormatResourceString("Key_Cannot_Be_Null"));
  52. object val = bag[sKey];
  53. if(val is StateItem)
  54. return val;
  55. return null;
  56. }
  57. set
  58. {
  59. Add((string)key, value);
  60. }
  61. }
  62. public ICollection Keys
  63. {
  64. get
  65. {
  66. return bag.Keys;
  67. }
  68. }
  69. public ICollection Values
  70. {
  71. get
  72. {
  73. return bag.Values;
  74. }
  75. }
  76. public StateItem Add(string key, object value)
  77. {
  78. if(key == null || key.Length == 0)
  79. {
  80. throw new ArgumentException(HttpRuntime.FormatResourceString("Key_Cannot_Be_Null"));
  81. }
  82. StateItem val = null;
  83. if(bag[key] is StateItem)
  84. val = (StateItem)(bag[key]);
  85. if(val==null)
  86. {
  87. if(value!=null || marked)
  88. {
  89. val = new StateItem(value);
  90. bag.Add(key, val);
  91. }
  92. } else
  93. {
  94. if(value!=null && !marked)
  95. bag.Remove(key);
  96. val.Value = value;
  97. }
  98. if(val!=null && marked)
  99. {
  100. val.IsDirty = true;
  101. }
  102. return val;
  103. }
  104. public void Clear()
  105. {
  106. bag.Clear();
  107. }
  108. public IDictionaryEnumerator GetEnumerator()
  109. {
  110. return bag.GetEnumerator();
  111. }
  112. public bool IsItemDirty(string key)
  113. {
  114. object o = bag[key];
  115. if(o is StateItem)
  116. return ((StateItem)o).IsDirty;
  117. return false;
  118. }
  119. public void Remove(string key)
  120. {
  121. bag.Remove(key);
  122. }
  123. /// <summary>
  124. /// Undocumented
  125. /// </summary>
  126. public void SetItemDirty(string key, bool dirty)
  127. {
  128. if(bag[key] is StateItem)
  129. ((StateItem)bag[key]).IsDirty = dirty;
  130. }
  131. internal bool IsTrackingViewState
  132. {
  133. get
  134. {
  135. return marked;
  136. }
  137. }
  138. internal void LoadViewState(object state)
  139. {
  140. if(state!=null)
  141. {
  142. Pair pair = (Pair)state;
  143. ArrayList keyList = (ArrayList)(pair.First);
  144. ArrayList valList = (ArrayList)(pair.Second);
  145. for(int i=0; i < keyList.Count; i++)
  146. Add((string)keyList[i], valList[i]);
  147. }
  148. }
  149. internal object SaveViewState()
  150. {
  151. if(bag.Count > 0)
  152. {
  153. ArrayList keyList = null, valList = null;
  154. foreach(IDictionaryEnumerator current in bag)
  155. {
  156. StateItem item = (StateItem)current.Value;
  157. if(item.IsDirty)
  158. {
  159. if(keyList==null)
  160. {
  161. keyList = new ArrayList();
  162. valList = new ArrayList();
  163. }
  164. keyList.Add(current.Key);
  165. valList.Add(current.Value);
  166. }
  167. }
  168. if(keyList!=null)
  169. return new Pair(keyList, valList);
  170. }
  171. return null;
  172. }
  173. internal void TrackViewState()
  174. {
  175. marked = true;
  176. }
  177. IEnumerator IEnumerable.GetEnumerator()
  178. {
  179. return GetEnumerator();
  180. }
  181. void IStateManager.LoadViewState(object savedState)
  182. {
  183. LoadViewState(savedState);
  184. }
  185. object IStateManager.SaveViewState()
  186. {
  187. return SaveViewState();
  188. }
  189. void IStateManager.TrackViewState()
  190. {
  191. TrackViewState();
  192. }
  193. bool IStateManager.IsTrackingViewState
  194. {
  195. get
  196. {
  197. return IsTrackingViewState;
  198. }
  199. }
  200. void ICollection.CopyTo(Array array, int index)
  201. {
  202. Values.CopyTo(array, index);
  203. }
  204. bool ICollection.IsSynchronized
  205. {
  206. get
  207. {
  208. return false;
  209. }
  210. }
  211. object ICollection.SyncRoot
  212. {
  213. get
  214. {
  215. return this;
  216. }
  217. }
  218. void IDictionary.Add(object key, object value)
  219. {
  220. Add((string)key, value);
  221. }
  222. void IDictionary.Remove(object key)
  223. {
  224. Remove((string)key);
  225. }
  226. bool IDictionary.Contains(object key)
  227. {
  228. return bag.Contains((string)key);
  229. }
  230. bool IDictionary.IsFixedSize
  231. {
  232. get
  233. {
  234. return false;
  235. }
  236. }
  237. bool IDictionary.IsReadOnly
  238. {
  239. get
  240. {
  241. return false;
  242. }
  243. }
  244. }
  245. }