Welcome to mirror list, hosted at ThFree Co, Russian Federation.

StateBag.cs « System.Web.UI « System.Web « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 44b4a09e060997213db765616c3d6083f83a2319 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/**
 * Namespace: System.Web.UI
 * Class:     StateBag
 * 
 * Author:  Gaurav Vaish
 * Maintainer: gvaish@iitk.ac.in
 * Implementation: yes
 * Contact: <gvaish@iitk.ac.in>
 * Status:  100%
 * 
 * (C) Gaurav Vaish (2001)
 */

using System;
using System.Web;
using System.Collections;
using System.Collections.Specialized;

namespace System.Web.UI
{
	public sealed class StateBag : IStateManager, IDictionary, ICollection, IEnumerable
	{
		private bool ignoreCase;
		private bool marked;
		private IDictionary bag;
		
		public StateBag (bool ignoreCase)
		{
			Initialize (ignoreCase);
		}
		
		public StateBag ()
		{
			Initialize (false);
		}

		private void Initialize (bool ignoreCase)
		{
			this.ignoreCase = ignoreCase;
			marked = false;
			bag = new HybridDictionary (ignoreCase);
		}
		
		public int Count {
			get { return bag.Count; }
		}

		
		public object this [string key] {
			get {
				if (key == null || key.Length == 0)
					throw new ArgumentException (HttpRuntime.FormatResourceString ("Key_Cannot_Be_Null"));

				object val = bag [key];

				if (val is StateItem)
					return ((StateItem) val).Value;

				return null; // 
			}

			set { Add (key, value); }
		}

		object IDictionary.this [object key] {
			get { return this [(string) key] as object; }

			set { Add ((string) key, value); }
		}
		
		public ICollection Keys {
			get { return bag.Keys; }
		}
		
		public ICollection Values {
			get { return bag.Values; }
		}
		
		public StateItem Add (string key, object value)
		{
			if (key == null || key.Length == 0)
				throw new ArgumentException (HttpRuntime.FormatResourceString ("Key_Cannot_Be_Null"));

			StateItem val = bag [key] as StateItem; //don't throw exception when null
			if(val == null) {
				if(value != null || marked) {
					val = new StateItem (value);
					bag.Add (key, val);
				}
			}
			else if (value == null && !marked)
				bag.Remove (key);
			else
				val.Value = value;

			if (val != null && marked)
				val.IsDirty = true;

			return val;
		}
		
		public void Clear ()
		{
			bag.Clear ();
		}
		
		public IDictionaryEnumerator GetEnumerator ()
		{
			return bag.GetEnumerator ();
		}
		
		public bool IsItemDirty (string key)
		{
			object o = bag [key];

			if (o is StateItem)
				return ((StateItem) o).IsDirty;
			
			return false;
		}
		
		public void Remove (string key)
		{
			bag.Remove (key);
		}
		
		/// <summary>
		/// Undocumented
		/// </summary>
		public void SetItemDirty (string key, bool dirty)
		{
			if (bag [key] is StateItem)
				((StateItem) bag [key]).IsDirty = dirty;
		}
		
		internal bool IsTrackingViewState {
			get { return marked; }
		}
		
		internal void LoadViewState (object state)
		{
			if(state!=null) {
				Pair pair = (Pair) state;
				ArrayList keyList = (ArrayList) (pair.First);
				ArrayList valList = (ArrayList) (pair.Second);
				for(int i = 0; i < keyList.Count; i++)
					Add ((string) keyList [i], valList [i]);
			}
		}
		
		internal object SaveViewState ()
		{
			if(bag.Count > 0) {
				ArrayList keyList = null, valList = null;

				foreach(IDictionaryEnumerator current in bag) {
					StateItem item = (StateItem)current.Value;

					if (item.IsDirty) {
						if (keyList == null) {
							keyList = new ArrayList ();
							valList = new ArrayList ();
						}
						
						keyList.Add (current.Key);
						valList.Add (current.Value);
					}
				}

				if (keyList!=null)
					return new Pair (keyList, valList);
			}
			return null;
		}
		
		internal void TrackViewState()
		{
			marked = true;
		}
		
		IEnumerator IEnumerable.GetEnumerator ()
		{
			return GetEnumerator ();
		}
		
		void IStateManager.LoadViewState (object savedState)
		{
			LoadViewState (savedState);
		}
		
		object IStateManager.SaveViewState ()
		{
			return SaveViewState ();
		}
		
		void IStateManager.TrackViewState ()
		{
			TrackViewState ();
		}
		
		bool IStateManager.IsTrackingViewState {
			get { return IsTrackingViewState; }
		}
		
		void ICollection.CopyTo (Array array, int index)
		{
			Values.CopyTo (array, index);
		}
		
		bool ICollection.IsSynchronized {
			get { return false; }
		}
		
		object ICollection.SyncRoot
		{
			get { return this; }
		}
		
		void IDictionary.Add (object key, object value)
		{
			Add ((string) key, value);
		}
		
		void IDictionary.Remove (object key)
		{
			Remove ((string) key);
		}
		
		bool IDictionary.Contains (object key)
		{
			return bag.Contains ((string) key);
		}
		
		bool IDictionary.IsFixedSize {
			get { return false; }
		}
		
		bool IDictionary.IsReadOnly {
			get { return false; }
		}
	}
}