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