StateBag.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. //
  2. // Permission is hereby granted, free of charge, to any person obtaining
  3. // a copy of this software and associated documentation files (the
  4. // "Software"), to deal in the Software without restriction, including
  5. // without limitation the rights to use, copy, modify, merge, publish,
  6. // distribute, sublicense, and/or sell copies of the Software, and to
  7. // permit persons to whom the Software is furnished to do so, subject to
  8. // the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be
  11. // included in all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. //
  21. /**
  22. * Namespace: System.Web.UI
  23. * Class: StateBag
  24. *
  25. * Author: Gaurav Vaish
  26. * Maintainer: [email protected]
  27. * Implementation: yes
  28. * Contact: <[email protected]>
  29. * Status: 100%
  30. *
  31. * (C) Gaurav Vaish (2001)
  32. */
  33. using System;
  34. using System.Web;
  35. using System.Collections;
  36. using System.Collections.Specialized;
  37. namespace System.Web.UI
  38. {
  39. public sealed class StateBag : IStateManager, IDictionary, ICollection, IEnumerable
  40. {
  41. private bool ignoreCase;
  42. private bool marked;
  43. private HybridDictionary bag;
  44. public StateBag (bool ignoreCase)
  45. {
  46. Initialize (ignoreCase);
  47. }
  48. public StateBag ()
  49. {
  50. Initialize (false);
  51. }
  52. private void Initialize (bool ignoreCase)
  53. {
  54. this.ignoreCase = ignoreCase;
  55. marked = false;
  56. bag = new HybridDictionary (ignoreCase);
  57. }
  58. public int Count {
  59. get { return bag.Count; }
  60. }
  61. public object this [string key] {
  62. get {
  63. if (key == null || key.Length == 0)
  64. throw new ArgumentException (HttpRuntime.FormatResourceString ("Key_Cannot_Be_Null"));
  65. object val = bag [key];
  66. if (val is StateItem)
  67. return ((StateItem) val).Value;
  68. return null; //
  69. }
  70. set { Add (key, value); }
  71. }
  72. object IDictionary.this [object key] {
  73. get { return this [(string) key] as object; }
  74. set { Add ((string) key, value); }
  75. }
  76. public ICollection Keys {
  77. get { return bag.Keys; }
  78. }
  79. public ICollection Values {
  80. get { return bag.Values; }
  81. }
  82. public StateItem Add (string key, object value)
  83. {
  84. if (key == null || key.Length == 0)
  85. throw new ArgumentException (HttpRuntime.FormatResourceString ("Key_Cannot_Be_Null"));
  86. StateItem val = bag [key] as StateItem; //don't throw exception when null
  87. if(val == null) {
  88. if(value != null || marked) {
  89. val = new StateItem (value);
  90. bag.Add (key, val);
  91. }
  92. }
  93. else if (value == null && !marked)
  94. bag.Remove (key);
  95. else
  96. val.Value = value;
  97. if (val != null && marked) {
  98. val.IsDirty = true;
  99. }
  100. return val;
  101. }
  102. public void Clear ()
  103. {
  104. bag.Clear ();
  105. }
  106. public IDictionaryEnumerator GetEnumerator ()
  107. {
  108. return bag.GetEnumerator ();
  109. }
  110. public bool IsItemDirty (string key)
  111. {
  112. object o = bag [key];
  113. if (o is StateItem)
  114. return ((StateItem) o).IsDirty;
  115. return false;
  116. }
  117. public void Remove (string key)
  118. {
  119. bag.Remove (key);
  120. }
  121. /// <summary>
  122. /// Undocumented
  123. /// </summary>
  124. public void SetItemDirty (string key, bool dirty)
  125. {
  126. if (bag [key] is StateItem)
  127. ((StateItem) bag [key]).IsDirty = dirty;
  128. }
  129. internal bool IsTrackingViewState {
  130. get { return marked; }
  131. }
  132. internal void LoadViewState (object state)
  133. {
  134. if(state!=null) {
  135. Pair pair = (Pair) state;
  136. ArrayList keyList = (ArrayList) (pair.First);
  137. ArrayList valList = (ArrayList) (pair.Second);
  138. int valCount = valList.Count;
  139. for(int i = 0; i < keyList.Count; i++) {
  140. if (i < valCount)
  141. Add ((string) keyList [i], valList [i]);
  142. else
  143. Add ((string) keyList [i], null);
  144. }
  145. }
  146. }
  147. internal object SaveViewState ()
  148. {
  149. if(bag.Count > 0) {
  150. ArrayList keyList = null, valList = null;
  151. foreach (string key in bag.Keys) {
  152. StateItem item = (StateItem) bag [key];
  153. if (item.IsDirty) {
  154. if (keyList == null) {
  155. keyList = new ArrayList ();
  156. valList = new ArrayList ();
  157. }
  158. keyList.Add (key);
  159. valList.Add (item.Value);
  160. }
  161. }
  162. if (keyList!=null)
  163. return new Pair (keyList, valList);
  164. }
  165. return null;
  166. }
  167. internal void TrackViewState()
  168. {
  169. marked = true;
  170. }
  171. IEnumerator IEnumerable.GetEnumerator ()
  172. {
  173. return GetEnumerator ();
  174. }
  175. void IStateManager.LoadViewState (object savedState)
  176. {
  177. LoadViewState (savedState);
  178. }
  179. object IStateManager.SaveViewState ()
  180. {
  181. return SaveViewState ();
  182. }
  183. void IStateManager.TrackViewState ()
  184. {
  185. TrackViewState ();
  186. }
  187. bool IStateManager.IsTrackingViewState {
  188. get { return IsTrackingViewState; }
  189. }
  190. void ICollection.CopyTo (Array array, int index)
  191. {
  192. Values.CopyTo (array, index);
  193. }
  194. bool ICollection.IsSynchronized {
  195. get { return false; }
  196. }
  197. object ICollection.SyncRoot
  198. {
  199. get { return this; }
  200. }
  201. void IDictionary.Add (object key, object value)
  202. {
  203. Add ((string) key, value);
  204. }
  205. void IDictionary.Remove (object key)
  206. {
  207. Remove ((string) key);
  208. }
  209. bool IDictionary.Contains (object key)
  210. {
  211. return bag.Contains ((string) key);
  212. }
  213. bool IDictionary.IsFixedSize {
  214. get { return false; }
  215. }
  216. bool IDictionary.IsReadOnly {
  217. get { return false; }
  218. }
  219. }
  220. }