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

XamlParser.cs « Mono.Windows.Serialization « PresentationFramework « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 732a873527bcbde89dbc8b84530f723833ca80d1 (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
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
//
// XamlParser.cs
//
// Author:
//   Iain McCoy (iain@mccoy.id.au)
//
// (C) 2005 Iain McCoy
//
// 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.Diagnostics;
using System.IO;
using System.Xml;
using System.Reflection;
using System.Windows;
using System.Windows.Serialization;

namespace Mono.Windows.Serialization {
	public class XamlParser {
		public const string XAML_NAMESPACE = "http://schemas.microsoft.com/winfx/xaml/2005";
		private Mapper mapper = new Mapper(new string[] { });
		private XmlReader reader;
		private IXamlWriter writer;

		private enum CurrentType { Object, 
			Property, 
			PropertyObject,
			DependencyProperty,
	       		Code }

		private class ParserState {
			public object obj;
			public CurrentType type;
		}
	
		private bool begun = false;

		private ParserState currentState = null;
		private ArrayList oldStates = new ArrayList();
	
		public XamlParser(string filename, IXamlWriter writer) : this(
				new XmlTextReader(filename), writer)
		{
		}
		
		public XamlParser(TextReader reader, IXamlWriter writer) : this(
				new XmlTextReader(reader), writer)
		{
		}
		
		public XamlParser(XmlReader reader, IXamlWriter writer)
		{
			this.reader = reader;
			this.writer = writer;
		}
		
		public void Parse()
		{
			while (reader.Read()) {
				Debug.WriteLine("NOW PARSING: " + reader.NodeType + "; " + reader.Name + "; " + reader.Value);
				if (goneTooFar())
					throw new Exception("Too far: " + reader.NodeType + ", " + reader.Name);
				if (currentState != null && currentState.type == CurrentType.Code)
				{
					processElementInCodeState();
					continue;
				}
				switch (reader.NodeType) {
				case XmlNodeType.ProcessingInstruction:
					parsePI();
					break;
				case XmlNodeType.Element:
					parseElement();
					break;
				case XmlNodeType.EndElement:
					parseEndElement();
					break;
				case XmlNodeType.Text:
					parseText();
					break;
				case XmlNodeType.Whitespace:
				case XmlNodeType.Comment:
					// skip whitespace and comments
					break;
				default:
					Console.Out.WriteLine("Unknown element type " + reader.NodeType);
					break;
				}
			}
		}
		void processElementInCodeState()
		{
			if (reader.NodeType == XmlNodeType.EndElement &&
					reader.LocalName == "Code" && 
					reader.NamespaceURI == XAML_NAMESPACE) {
				parseEndElement();
			} else {
				currentState.obj = (string)currentState.obj + reader.Value;
			}
		}
		bool goneTooFar()
		{

			if (begun && 
					currentState == null && 
					reader.NodeType != XmlNodeType.Whitespace && 
					reader.NodeType != XmlNodeType.Comment)
				return true;
			else
				return false;
		}

		void parsePI()
		{
			if (reader.Name != "Mapping")
				throw new Exception("Unknown processing instruction");
			mapper.AddMappingProcessingInstruction(reader.Value);
		}

		void parseElement()
		{
			if (reader.NamespaceURI == "")
				throw new Exception("No xml namespace specified.");
			if (reader.LocalName == "Code" && reader.NamespaceURI == XAML_NAMESPACE) {
				parseCodeElement();
				return;
			}
			// This element must be an object if:
			//  - It's a direct child of a property element
			//  - It's a direct child of an IAddChild element
			//    and does not have a dot in its name
			//    
			//  We just check that it doesn't have a dot in it here
			//  since parseObjectElement will confirm that it is
			//  a direct child of an IAddChild.
			//
			//  If it's a dotted name, then it is a property.
			//  What it is a property of depends on the bit of the
			//  name before the dot.
			int dotPosition = reader.LocalName.IndexOf('.');
			if (dotPosition < 0 ||
					currentState.type == CurrentType.Property) {
				parseObjectElement();
				return;
			}
			string beforeDot = reader.LocalName.Substring(0, dotPosition);
			string afterDot = reader.LocalName.Substring(dotPosition + 1);
			// If we've got this far, then currentState.Type == Object
			if (isNameOfAncestorClass(beforeDot, (Type)currentState.obj))
				parseNormalPropertyElement(afterDot);
			else
				parseDependencyPropertyElement(beforeDot, afterDot);
		}

		// check if the given name is the name of an ancestor of 
		// the given type
		bool isNameOfAncestorClass(string name, Type t)
		{
			if (name == "object")
				return true;
			while (t.BaseType != null) {
				if (t.Name == name)
					return true;
				t = t.BaseType;
			}
			return false;
		}

		// handle an x:Code element. Most of the handling for this is
		// at the start of the main parsing loop, in the 
		// processElementInCodeState() function
		void parseCodeElement()
		{
			push(CurrentType.Code, "");
		}

		void parseText()
		{
			switch (currentState.type) {
			case CurrentType.Object:
			case CurrentType.PropertyObject:
				abortIfNotAddChild("text");
				writer.CreateObjectText(reader.Value);
				break;
			case CurrentType.DependencyProperty:
				DependencyProperty dp = (DependencyProperty)currentState.obj;
				writer.CreateDependencyPropertyText(reader.Value, dp.PropertyType);
				break;
			case CurrentType.Property:
				PropertyInfo prop = (PropertyInfo)currentState.obj;
				writer.CreatePropertyText(reader.Value, prop.PropertyType);
				break;
			default:
				throw new NotImplementedException();
			}
		}

		void abortIfNotAddChild(string thing)
		{
			if (!isAddChild((Type)currentState.obj))
				throw new Exception("Cannot add " + thing +
						" to instance of '" + 
						((Type)currentState.obj) + 
						"'.");
		}
		
		void parseNormalPropertyElement(string propertyName)
		{
			// preconditions: currentState.Type == Object
			Type currentType = (Type)currentState.obj;
			PropertyInfo prop = currentType.GetProperty(propertyName);

			if (prop == null) {
				throw new Exception("Property '" + propertyName + "' not found on '" + currentType.Name + "'.");
			}

			push(CurrentType.Property, prop);

			writer.CreateProperty(prop);

			if (reader.HasAttributes) {
				throw new Exception("Property node should not have attributes.");
			}
		}


		void parseDependencyPropertyElement(string attachedTo, string propertyName)
		{
			Type currentType = (Type)currentState.obj;
			ensureDependencyObject(currentType);
			Type typeAttachedTo = findTypeToAttachTo(attachedTo, propertyName);
			DependencyProperty dp = getDependencyProperty(typeAttachedTo, propertyName);
			
			push(CurrentType.DependencyProperty, dp);

			writer.CreateDependencyProperty(typeAttachedTo, propertyName, dp.PropertyType);
		}
		bool isAddChild(Type t)
		{
			return (t.GetInterface("System.Windows.Serialization.IAddChild") != null);
		}
		void parseObjectElement()
		{
			Type parent;
			bool isEmpty = reader.IsEmptyElement;
			
			parent = mapper.GetType(reader.NamespaceURI, reader.Name);
			if (parent == null)
				throw new Exception("Class '" + reader.Name + "' not found.");
		
			// whichever of these functions runs will push something
			if (currentState == null) {
				parseTopLevelObjectElement(parent);
			} else {
				parseChildObjectElement(parent);
			}
			
			processObjectAttributes();

			if (isEmpty) {
				closeEmptyObjectElement();
			}
		}
		void parseTopLevelObjectElement(Type parent)
		{
			if (reader.GetAttribute("Name", XAML_NAMESPACE) != null)
				throw new Exception("The XAML Name attribute can not be applied to top level elements\n"+
						"Do you mean the Class attribute?");
			begun = true;
			createTopLevel(parent.AssemblyQualifiedName, reader.GetAttribute("Class", XAML_NAMESPACE));
		}

		void parseChildObjectElement(Type parent)
		{
			if (reader.GetAttribute("Class", XAML_NAMESPACE) != null)
				throw new Exception("The XAML Class attribute can not be applied to child elements\n"+
						"Do you mean the Name attribute?");
			string name = reader.GetAttribute("Name", XAML_NAMESPACE);
			if (name == null)
				name = reader.GetAttribute("Name", reader.NamespaceURI);

			if (currentState.type == CurrentType.Object) {
				abortIfNotAddChild("object");
				addChild(parent, name);
			} else if (currentState.type == CurrentType.Property) {
				addPropertyChild(parent, name);
			} else {
				throw new NotImplementedException();
			}
		}
		void processObjectAttributes()
		{
			if (reader.MoveToFirstAttribute()) {
				do {
					if (reader.Name.StartsWith("xmlns"))
						continue;
					if (reader.NamespaceURI == XAML_NAMESPACE)
						continue;
					if (reader.LocalName.IndexOf(".") < 0)
						parseLocalPropertyAttribute();
					else
						parseDependencyPropertyAttribute();
				} while (reader.MoveToNextAttribute());
			}
		}

		void closeEmptyObjectElement()
		{
			if (currentState.type == CurrentType.Object) {
				writer.EndObject();
			} else if (currentState.type == CurrentType.PropertyObject) {
				ParserState state = (ParserState)oldStates[oldStates.Count - 1];
				writer.EndPropertyObject(((PropertyInfo)state.obj).PropertyType);
			}
			pop();
		}

		void createTopLevel(string parentName, string className)
		{
			Type t = Type.GetType(parentName);

			writer.CreateTopLevel(t, className);
			currentState = new ParserState();
			currentState.type = CurrentType.Object;
			currentState.obj = t;
		}

		void addChild(Type type, string objectName)
		{
			writer.CreateObject(type, objectName);
			push(CurrentType.Object, type);
		}
		
		void addPropertyChild(Type type, string objectName)
		{
			writer.CreatePropertyObject(type, objectName);

			push(CurrentType.PropertyObject, type);
		}


		
		void parseLocalPropertyAttribute()
		{
			string propertyName = reader.LocalName;
			Type currentType = (Type)currentState.obj;
			PropertyInfo prop = currentType.GetProperty(propertyName);
			if (parsedAsEventProperty(currentType, propertyName))
				return;
			if (prop == null)
				throw new Exception ("Property '" + propertyName + "' not found on '" + currentType.Name + "'.");

			writer.CreateProperty(prop);

			if (prop.PropertyType.IsSubclassOf(typeof(Delegate)))
				writer.CreatePropertyDelegate(reader.Value, prop.PropertyType);
			else
				writer.CreatePropertyText(reader.Value, prop.PropertyType);
			
			writer.EndProperty();
		}
		
		bool parsedAsEventProperty(Type currentType, string eventName)
		{
			EventInfo evt = currentType.GetEvent(eventName);
			if (evt == null)
				return false;
			writer.CreateEvent(evt);
			writer.CreateEventDelegate(reader.Value, evt.EventHandlerType);
			writer.EndEvent();
			return true;
		}

		void ensureDependencyObject(Type currentType)
		{
			if (!currentType.IsSubclassOf(typeof(System.Windows.DependencyObject)))
					throw new Exception("Dependency properties can only be set on "+
							"DependencyObjects (not " + currentType.Name + ")");
		}
		Type findTypeToAttachTo(string attachedTo, string propertyName)
		{
			Type typeAttachedTo = null;
			foreach (ParserState state in oldStates) {
				if ((state.type == CurrentType.Object || 
						state.type == CurrentType.PropertyObject) &&
						((Type)state.obj).Name == attachedTo) {
					typeAttachedTo = (Type)state.obj;
					break;
				}
			}
			if (typeAttachedTo == null)
				throw new Exception("Nothing to attach to: " + attachedTo + "." + propertyName);
			return typeAttachedTo;
		}

		DependencyProperty getDependencyProperty(Type typeAttachedTo, string propertyName)
		{
			FieldInfo propField = typeAttachedTo.GetField(propertyName + "Property");
			if (propField == null)
				throw new Exception("Property '" + propertyName + "' does not exist on '" + typeAttachedTo.Name + "'.");
			return (DependencyProperty)propField.GetValue(null);
		}

		void parseDependencyPropertyAttribute()
		{
			int index = reader.LocalName.LastIndexOf('.');
			string attachedTo = reader.LocalName.Substring(0, index);
			string propertyName = reader.LocalName.Substring(index + 1);
			
			Type currentType = (Type)currentState.obj;
			ensureDependencyObject(currentType);
			Type typeAttachedTo = findTypeToAttachTo(attachedTo, propertyName);
			DependencyProperty dp = getDependencyProperty(typeAttachedTo, propertyName);
			
			writer.CreateDependencyProperty(typeAttachedTo, propertyName, dp.PropertyType);
			writer.CreateDependencyPropertyText(reader.Value, dp.PropertyType);
			writer.EndDependencyProperty();
		}

		void parseEndElement()
		{
			Debug.WriteLine("IN ENDELEMENT, SWITCHING ON " + currentState.type);
			switch (currentState.type) {
			case CurrentType.Code:
				writer.CreateCode((string)currentState.obj);
				break;
			case CurrentType.Object:
				writer.EndObject();
				break;
			case CurrentType.PropertyObject:
				writer.EndPropertyObject((Type)currentState.obj);
				break;
			case CurrentType.Property:
				writer.EndProperty();
				break;
			case CurrentType.DependencyProperty:
				writer.EndDependencyProperty();
				break;
			}
			pop();
		}

		void pop()
		{
			Debug.WriteLine("POPPING: " + currentState.type);
			if (oldStates.Count == 0) {
				currentState = null;
				writer.Finish();
				return;
			}
			int lastIndex = oldStates.Count - 1;
			currentState = (ParserState)oldStates[lastIndex];
			oldStates.RemoveAt(lastIndex);
		}
		void push(CurrentType type, Object obj)
		{
			Debug.WriteLine("PUSHING: " + type);
			oldStates.Add(currentState);
			currentState = new ParserState();
			currentState.type = type;
			currentState.obj = obj;
		}
	}
}