File: DesignerSerializationManager.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284,512 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (508 lines) | stat: -rw-r--r-- 15,208 bytes parent folder | download | duplicates (6)
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
//
// System.ComponentModel.Design.Serialization.DesignerSerializationManager
//
// Authors:	 
//	  Ivan N. Zlatev (contact i-nZ.net)
//
// (C) 2007 Ivan N. Zlatev

//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//


using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.ComponentModel;
using System.ComponentModel.Design;



namespace System.ComponentModel.Design.Serialization
{
	
	public class DesignerSerializationManager : IDesignerSerializationManager, IServiceProvider
	{
		
		private class Session : IDisposable
		{
			
			private DesignerSerializationManager _manager;
			
			public Session (DesignerSerializationManager manager)
			{
				_manager = manager;
			}
			
			public void Dispose ()
			{
				_manager.OnSessionDisposed (EventArgs.Empty);
			}
		}

		
		
		public DesignerSerializationManager () : this (null)
		{
		}
		
		// This constructor sets the PreserveNames and ValidateRecycledTypes properties to true.
		//
		public DesignerSerializationManager (IServiceProvider provider)
		{
			_serviceProvider = provider;
			_preserveNames = true;
			_validateRecycledTypes = true;
		}
		
		private IServiceProvider _serviceProvider;
		private bool _preserveNames = false;
		private bool _validateRecycledTypes = false;
		private bool _recycleInstances = false;
		private IContainer _designerContainer = null;
		private object _propertyProvider = null;
		private Session _session = null;
		private ArrayList _errors = null;
		private List <IDesignerSerializationProvider> _serializationProviders;
		private Dictionary <Type, object> _serializersCache = null; // componentType - serializer instance
		private Dictionary <string, object> _instancesByNameCache = null; // name - instance
		private Dictionary <object, string> _instancesByValueCache = null; // instance - name
		private ContextStack _contextStack = null;
		
		
		public bool RecycleInstances {
			get { return _recycleInstances; }
			set {
				VerifyNotInSession ();
				_recycleInstances = value; 
			}
		}
		
		public bool PreserveNames {
			get { return _preserveNames; }
			set {
				VerifyNotInSession ();
				_preserveNames = value; 
			}
		}
		
		public bool ValidateRecycledTypes {
			get { return _validateRecycledTypes; }
			set {
				VerifyNotInSession ();
				_validateRecycledTypes = value; 
			}
		}
		
		public IContainer Container { 
			get {
				if (_designerContainer == null) {
					_designerContainer = (this.GetService (typeof (IDesignerHost)) as IDesignerHost).Container;
				}
				return _designerContainer;
			}
			set{
				VerifyNotInSession ();
				_designerContainer = value;
			}
		}
		
		public object PropertyProvider {
			get { return _propertyProvider; }
			set { _propertyProvider = value; }
		}
		
		public IList Errors {
			get { return _errors; }
		}
		
		public event EventHandler SessionDisposed;
		public event EventHandler SessionCreated;
		
		protected virtual void OnSessionCreated (EventArgs e)
		{
			if (SessionCreated != null) {
				SessionCreated (this, e);
			}
		}
			
		// For behaviour description:
		//
		// http://msdn2.microsoft.com/en-us/library/system.componentmodel.design.serialization.designerserializationmanager.validaterecycledtypes.aspx
		// http://msdn2.microsoft.com/en-us/library/system.componentmodel.design.serialization.designerserializationmanager.preservenames.aspx
		//
		protected virtual object CreateInstance (Type type, ICollection arguments, string name, bool addToContainer)
		{
			VerifyInSession ();
			object instance = null;

			if (name != null && _recycleInstances) {
				_instancesByNameCache.TryGetValue (name, out instance);
				if (instance != null && _validateRecycledTypes) {
					if (instance.GetType () != type)
						instance = null;
				}
			}
			
			if (instance == null || !_recycleInstances)
				instance = this.CreateInstance (type, arguments);
		
			if (addToContainer && instance != null && this.Container != null && typeof (IComponent).IsAssignableFrom (type)) {
				if (_preserveNames) {
					this.Container.Add ((IComponent) instance, name);
				}
				else {
					if (name != null && this.Container.Components[name] != null) {
						this.Container.Add ((IComponent) instance);
					}
					else {
						this.Container.Add ((IComponent) instance, name);
					}
				}
				ISite site = ((IComponent)instance).Site; // get the name from the site in case a name has been generated.
				if (site != null)
					name = site.Name;
			}
			
			if (instance != null && name != null) {
				_instancesByNameCache[name] = instance;
				_instancesByValueCache[instance] = name;
			}
			
			return instance;
		}

		// Invokes the constructor that matches the arguments
		//
		private object CreateInstance (Type type, ICollection argsCollection)
		{
			object instance = null;
			object[] arguments = null;
			Type[] types = new Type[0];

			if (argsCollection != null) {
				arguments = new object[argsCollection.Count];
				types = new Type[argsCollection.Count];
				argsCollection.CopyTo (arguments, 0);

				for (int i=0; i < arguments.Length; i++) {
					if (arguments[i] == null)
						types[i] = null;
					else
						types[i] = arguments[i].GetType ();
				}
			}

			ConstructorInfo ctor = type.GetConstructor (types);
			if (ctor != null) {
				instance = ctor.Invoke (arguments);
			}

			return instance;
		}

		public object GetSerializer (Type objectType, Type serializerType)
		{
			VerifyInSession ();
			
			if (serializerType == null)
				throw new ArgumentNullException ("serializerType");
				
			object serializer = null;

			if (objectType != null) {
				// try 1: from cache
				//
				_serializersCache.TryGetValue (objectType, out serializer);

				// check for provider attribute and add it to the list of providers
				//
				if (serializer != null && !serializerType.IsAssignableFrom (serializer.GetType ()))
					serializer = null;
				
				AttributeCollection attributes = TypeDescriptor.GetAttributes (objectType);
				DefaultSerializationProviderAttribute providerAttribute = attributes[typeof (DefaultSerializationProviderAttribute)] 
																			   as DefaultSerializationProviderAttribute;
				if (providerAttribute != null && this.GetType (providerAttribute.ProviderTypeName) == serializerType) {
					object provider = Activator.CreateInstance (this.GetType (providerAttribute.ProviderTypeName), 
																 BindingFlags.CreateInstance | BindingFlags.Public | BindingFlags.NonPublic, 
																 null, null, null);
					((IDesignerSerializationManager)this).AddSerializationProvider ((IDesignerSerializationProvider) provider);
				}
			}

			// try 2: DesignerSerializerAttribute
			//
			if (serializer == null && objectType != null) {
				AttributeCollection attributes = TypeDescriptor.GetAttributes (objectType);
				DesignerSerializerAttribute serializerAttribute = attributes[typeof (DesignerSerializerAttribute)] as DesignerSerializerAttribute;
				if (serializerAttribute != null && 
					this.GetType (serializerAttribute.SerializerBaseTypeName) == serializerType) {
					try {
						serializer = Activator.CreateInstance (this.GetType (serializerAttribute.SerializerTypeName), 
										       BindingFlags.CreateInstance | BindingFlags.Instance | 
										       BindingFlags.Public | BindingFlags.NonPublic, 
										       null, null, null);
					} catch {}
				}
				
				if (serializer != null)
					_serializersCache[objectType] = serializer;
			}

			// try 3: from provider
			//
			if (serializer == null && _serializationProviders != null) {
				foreach (IDesignerSerializationProvider provider in _serializationProviders) {
					serializer = provider.GetSerializer (this, null, objectType, serializerType);
					if (serializer != null)
						break;
				}
			}

			return serializer;
		}

		private void VerifyInSession ()
		{
			if (_session == null)
				throw new InvalidOperationException ("Not in session.");
		}
		
		private void VerifyNotInSession ()
		{
			if (_session != null)
				throw new InvalidOperationException ("In session.");
		}
		
		public IDisposable CreateSession ()
		{
			VerifyNotInSession ();
			_errors = new ArrayList ();
			_session = new Session (this);
			_serializersCache = new Dictionary<System.Type,object> ();
			_instancesByNameCache = new Dictionary<string,object> ();
			_instancesByValueCache = new Dictionary<object, string> ();
			_contextStack = new ContextStack ();
			
			this.OnSessionCreated (EventArgs.Empty);

			return _session;
		}
		
		protected virtual void OnSessionDisposed (EventArgs e)
		{
			_errors.Clear ();
			_errors = null;
			_serializersCache.Clear ();
			_serializersCache = null;
			_instancesByNameCache.Clear ();
			_instancesByNameCache = null;
			_instancesByValueCache.Clear ();
			_instancesByValueCache = null;
			_session = null;
			_contextStack = null;
			_resolveNameHandler = null;
			_serializationCompleteHandler = null;
			
			if (SessionDisposed != null) {
				SessionDisposed (this, e);
			}

			if (_serializationCompleteHandler != null)
				_serializationCompleteHandler (this, EventArgs.Empty);
		}
				
		protected virtual Type GetType (string typeName)
		{
			if (typeName == null)
				throw new ArgumentNullException ("typeName");
			
			this.VerifyInSession ();
			
			Type result = null;
			ITypeResolutionService typeResSvc = this.GetService (typeof (ITypeResolutionService)) as ITypeResolutionService;
			if (typeResSvc != null)
				result = typeResSvc.GetType (typeName);
			if (result == null)
				result = Type.GetType (typeName);
			
			return result;
		}
										
#region IDesignerSerializationManager implementation
		
		protected virtual void OnResolveName (ResolveNameEventArgs e)
		{
			if (_resolveNameHandler != null) {
				_resolveNameHandler (this, e);
			}
		}	
		
		void IDesignerSerializationManager.AddSerializationProvider (IDesignerSerializationProvider provider)
		{
			if (_serializationProviders == null)
				_serializationProviders = new List <IDesignerSerializationProvider> ();
			
			if (!_serializationProviders.Contains (provider))
				_serializationProviders.Add (provider);
		}
		
		void IDesignerSerializationManager.RemoveSerializationProvider (IDesignerSerializationProvider provider)
		{
			if (_serializationProviders != null)
				_serializationProviders.Remove (provider);
		}
		
		object IDesignerSerializationManager.CreateInstance (Type type, ICollection arguments, string name, bool addToContainer)
		{
			return this.CreateInstance (type, arguments, name, addToContainer);
		}
		
		object IDesignerSerializationManager.GetInstance (string name)
		{
			if (name == null)
				throw new ArgumentNullException ("name");
			this.VerifyInSession ();
			
			object instance = null;
			_instancesByNameCache.TryGetValue (name, out instance);

			if (instance == null && this.Container != null)
				instance = this.Container.Components[name];

			if (instance == null)
				instance = this.RequestInstance (name);

			return instance;
		}
		
		private object RequestInstance (string name)
		{
			ResolveNameEventArgs args = new ResolveNameEventArgs (name);
			this.OnResolveName (args);
			return args.Value;
		}
		
		Type IDesignerSerializationManager.GetType (string name)
		{
			return this.GetType (name);
		}
		
		object IDesignerSerializationManager.GetSerializer (Type type, Type serializerType)
		{
			return this.GetSerializer (type, serializerType);
		}
		
		string IDesignerSerializationManager.GetName (object instance)
		{
			if (instance == null)
				throw new ArgumentNullException ("instance");
			this.VerifyInSession ();
			
			string name = null;
			if (instance is IComponent) {
				ISite site = ((IComponent)instance).Site;
				if (site != null && site is INestedSite)
					name = ((INestedSite)site).FullName;
				else if (site != null)
					name = site.Name;
			}
			if (name == null)
				_instancesByValueCache.TryGetValue (instance, out name);
			return name;
		}
		
		void IDesignerSerializationManager.SetName (object instance, string name)
		{
			if (instance == null)
				throw new ArgumentNullException ("instance");
			if (name == null)
				throw new ArgumentNullException ("name");
			
			if (_instancesByNameCache.ContainsKey (name))
				throw new ArgumentException ("The object specified by instance already has a name, or name is already used by another named object.");
			
			_instancesByNameCache.Add (name, instance);
			_instancesByValueCache.Add (instance, name);
		}
		
		void IDesignerSerializationManager.ReportError (object error)
		{
		   this.VerifyInSession ();
		   _errors.Add (error);
		}
		
		ContextStack IDesignerSerializationManager.Context {
			get { return _contextStack; }
		}
		
		PropertyDescriptorCollection IDesignerSerializationManager.Properties {
			get {
				PropertyDescriptorCollection properties = new PropertyDescriptorCollection (new PropertyDescriptor[0]);
				object component = this.PropertyProvider;
				if (component != null)
				   properties = TypeDescriptor.GetProperties (component);
				
				return properties;
			}
		}
		
		private EventHandler _serializationCompleteHandler;
		private ResolveNameEventHandler _resolveNameHandler;
		
		event EventHandler IDesignerSerializationManager.SerializationComplete {
			add {
				this.VerifyInSession ();
				_serializationCompleteHandler = (EventHandler) Delegate.Combine (_serializationCompleteHandler, value);
			}
			remove {
				_serializationCompleteHandler = (EventHandler) Delegate.Remove (_serializationCompleteHandler, value);
			}
		}
				
		event ResolveNameEventHandler IDesignerSerializationManager.ResolveName {
			add {
				this.VerifyInSession ();
				_resolveNameHandler = (ResolveNameEventHandler) Delegate.Combine (_resolveNameHandler, value);
			}
			remove {
				_resolveNameHandler = (ResolveNameEventHandler) Delegate.Remove (_resolveNameHandler, value);
			}
		}	  
#endregion
		
		object IServiceProvider.GetService (Type serviceType)
		{
			return this.GetService (serviceType);
		}
		
		protected virtual object GetService (Type serviceType)
		{
			object result = null;
			if (_serviceProvider != null)
				result = _serviceProvider.GetService (serviceType);
			
			return result;
		}
	}
}