StateBag.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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[string key]
  46. {
  47. get
  48. {
  49. if(key==null || key.Length==0)
  50. throw new ArgumentException(HttpRuntime.FormatResourceString("Key_Cannot_Be_Null"));
  51. object val = bag[key];
  52. if(val is StateItem)
  53. return val;
  54. return null;
  55. }
  56. set
  57. {
  58. Add(key, value);
  59. }
  60. }
  61. public object this [object key]
  62. {
  63. get {
  64. return this [(string) key] as object;
  65. }
  66. set {
  67. Add ((string) key, value);
  68. }
  69. }
  70. public ICollection Keys
  71. {
  72. get
  73. {
  74. return bag.Keys;
  75. }
  76. }
  77. public ICollection Values
  78. {
  79. get
  80. {
  81. return bag.Values;
  82. }
  83. }
  84. public StateItem Add(string key, object value)
  85. {
  86. if(key == null || key.Length == 0)
  87. {
  88. throw new ArgumentException(HttpRuntime.FormatResourceString("Key_Cannot_Be_Null"));
  89. }
  90. StateItem val = null;
  91. if(bag[key] is StateItem)
  92. val = (StateItem)(bag[key]);
  93. if(val==null)
  94. {
  95. if(value!=null || marked)
  96. {
  97. val = new StateItem(value);
  98. bag.Add(key, val);
  99. }
  100. } else
  101. {
  102. if(value!=null && !marked)
  103. bag.Remove(key);
  104. val.Value = value;
  105. }
  106. if(val!=null && marked)
  107. {
  108. val.IsDirty = true;
  109. }
  110. return val;
  111. }
  112. public void Clear()
  113. {
  114. bag.Clear();
  115. }
  116. public IDictionaryEnumerator GetEnumerator()
  117. {
  118. return bag.GetEnumerator();
  119. }
  120. public bool IsItemDirty(string key)
  121. {
  122. object o = bag[key];
  123. if(o is StateItem)
  124. return ((StateItem)o).IsDirty;
  125. return false;
  126. }
  127. public void Remove(string key)
  128. {
  129. bag.Remove(key);
  130. }
  131. /// <summary>
  132. /// Undocumented
  133. /// </summary>
  134. public void SetItemDirty(string key, bool dirty)
  135. {
  136. if(bag[key] is StateItem)
  137. ((StateItem)bag[key]).IsDirty = dirty;
  138. }
  139. internal bool IsTrackingViewState
  140. {
  141. get
  142. {
  143. return marked;
  144. }
  145. }
  146. internal void LoadViewState(object state)
  147. {
  148. if(state!=null)
  149. {
  150. Pair pair = (Pair)state;
  151. ArrayList keyList = (ArrayList)(pair.First);
  152. ArrayList valList = (ArrayList)(pair.Second);
  153. for(int i=0; i < keyList.Count; i++)
  154. Add((string)keyList[i], valList[i]);
  155. }
  156. }
  157. internal object SaveViewState()
  158. {
  159. if(bag.Count > 0)
  160. {
  161. ArrayList keyList = null, valList = null;
  162. foreach(IDictionaryEnumerator current in bag)
  163. {
  164. StateItem item = (StateItem)current.Value;
  165. if(item.IsDirty)
  166. {
  167. if(keyList==null)
  168. {
  169. keyList = new ArrayList();
  170. valList = new ArrayList();
  171. }
  172. keyList.Add(current.Key);
  173. valList.Add(current.Value);
  174. }
  175. }
  176. if(keyList!=null)
  177. return new Pair(keyList, valList);
  178. }
  179. return null;
  180. }
  181. internal void TrackViewState()
  182. {
  183. marked = true;
  184. }
  185. IEnumerator IEnumerable.GetEnumerator()
  186. {
  187. return GetEnumerator();
  188. }
  189. void IStateManager.LoadViewState(object savedState)
  190. {
  191. LoadViewState(savedState);
  192. }
  193. object IStateManager.SaveViewState()
  194. {
  195. return SaveViewState();
  196. }
  197. void IStateManager.TrackViewState()
  198. {
  199. TrackViewState();
  200. }
  201. bool IStateManager.IsTrackingViewState
  202. {
  203. get
  204. {
  205. return IsTrackingViewState;
  206. }
  207. }
  208. void ICollection.CopyTo(Array array, int index)
  209. {
  210. Values.CopyTo(array, index);
  211. }
  212. bool ICollection.IsSynchronized
  213. {
  214. get
  215. {
  216. return false;
  217. }
  218. }
  219. object ICollection.SyncRoot
  220. {
  221. get
  222. {
  223. return this;
  224. }
  225. }
  226. void IDictionary.Add(object key, object value)
  227. {
  228. Add((string)key, value);
  229. }
  230. void IDictionary.Remove(object key)
  231. {
  232. Remove((string)key);
  233. }
  234. bool IDictionary.Contains(object key)
  235. {
  236. return bag.Contains((string)key);
  237. }
  238. bool IDictionary.IsFixedSize
  239. {
  240. get
  241. {
  242. return false;
  243. }
  244. }
  245. bool IDictionary.IsReadOnly
  246. {
  247. get
  248. {
  249. return false;
  250. }
  251. }
  252. }
  253. }