(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / System.Web / System.Web.UI / StateBag.cs
blob2d994350e0a683d845c9db4e5a36b2c4fe783bc6
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining
4 // a copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to
8 // permit persons to whom the Software is furnished to do so, subject to
9 // the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be
12 // included in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 /**
23 * Namespace: System.Web.UI
24 * Class: StateBag
26 * Author: Gaurav Vaish
27 * Maintainer: gvaish@iitk.ac.in
28 * Implementation: yes
29 * Contact: <gvaish@iitk.ac.in>
30 * Status: 100%
32 * (C) Gaurav Vaish (2001)
35 using System;
36 using System.Web;
37 using System.Collections;
38 using System.Collections.Specialized;
40 namespace System.Web.UI
42 public sealed class StateBag : IStateManager, IDictionary, ICollection, IEnumerable
44 private bool ignoreCase;
45 private bool marked;
46 private HybridDictionary bag;
48 public StateBag (bool ignoreCase)
50 Initialize (ignoreCase);
53 public StateBag ()
55 Initialize (false);
58 private void Initialize (bool ignoreCase)
60 this.ignoreCase = ignoreCase;
61 marked = false;
62 bag = new HybridDictionary (ignoreCase);
65 public int Count {
66 get { return bag.Count; }
70 public object this [string key] {
71 get {
72 if (key == null || key.Length == 0)
73 throw new ArgumentException (HttpRuntime.FormatResourceString ("Key_Cannot_Be_Null"));
75 object val = bag [key];
77 if (val is StateItem)
78 return ((StateItem) val).Value;
80 return null; //
83 set { Add (key, value); }
86 object IDictionary.this [object key] {
87 get { return this [(string) key] as object; }
89 set { Add ((string) key, value); }
92 public ICollection Keys {
93 get { return bag.Keys; }
96 public ICollection Values {
97 get { return bag.Values; }
100 public StateItem Add (string key, object value)
102 if (key == null || key.Length == 0)
103 throw new ArgumentException (HttpRuntime.FormatResourceString ("Key_Cannot_Be_Null"));
105 StateItem val = bag [key] as StateItem; //don't throw exception when null
106 if(val == null) {
107 if(value != null || marked) {
108 val = new StateItem (value);
109 bag.Add (key, val);
112 else if (value == null && !marked)
113 bag.Remove (key);
114 else
115 val.Value = value;
117 if (val != null && marked) {
118 val.IsDirty = true;
121 return val;
124 public void Clear ()
126 bag.Clear ();
129 public IDictionaryEnumerator GetEnumerator ()
131 return bag.GetEnumerator ();
134 public bool IsItemDirty (string key)
136 object o = bag [key];
138 if (o is StateItem)
139 return ((StateItem) o).IsDirty;
141 return false;
144 public void Remove (string key)
146 bag.Remove (key);
149 /// <summary>
150 /// Undocumented
151 /// </summary>
152 public void SetItemDirty (string key, bool dirty)
154 if (bag [key] is StateItem)
155 ((StateItem) bag [key]).IsDirty = dirty;
158 internal bool IsTrackingViewState {
159 get { return marked; }
162 internal void LoadViewState (object state)
164 if(state!=null) {
165 Pair pair = (Pair) state;
166 ArrayList keyList = (ArrayList) (pair.First);
167 ArrayList valList = (ArrayList) (pair.Second);
169 int valCount = valList.Count;
170 for(int i = 0; i < keyList.Count; i++) {
171 if (i < valCount)
172 Add ((string) keyList [i], valList [i]);
173 else
174 Add ((string) keyList [i], null);
179 internal object SaveViewState ()
181 if(bag.Count > 0) {
182 ArrayList keyList = null, valList = null;
184 foreach (string key in bag.Keys) {
185 StateItem item = (StateItem) bag [key];
187 if (item.IsDirty) {
188 if (keyList == null) {
189 keyList = new ArrayList ();
190 valList = new ArrayList ();
193 keyList.Add (key);
194 valList.Add (item.Value);
198 if (keyList!=null)
199 return new Pair (keyList, valList);
201 return null;
204 internal void TrackViewState()
206 marked = true;
209 IEnumerator IEnumerable.GetEnumerator ()
211 return GetEnumerator ();
214 void IStateManager.LoadViewState (object savedState)
216 LoadViewState (savedState);
219 object IStateManager.SaveViewState ()
221 return SaveViewState ();
224 void IStateManager.TrackViewState ()
226 TrackViewState ();
229 bool IStateManager.IsTrackingViewState {
230 get { return IsTrackingViewState; }
233 void ICollection.CopyTo (Array array, int index)
235 Values.CopyTo (array, index);
238 bool ICollection.IsSynchronized {
239 get { return false; }
242 object ICollection.SyncRoot
244 get { return this; }
247 void IDictionary.Add (object key, object value)
249 Add ((string) key, value);
252 void IDictionary.Remove (object key)
254 Remove ((string) key);
257 bool IDictionary.Contains (object key)
259 return bag.Contains ((string) key);
262 bool IDictionary.IsFixedSize {
263 get { return false; }
266 bool IDictionary.IsReadOnly {
267 get { return false; }