summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Xaml/FillResourceDictionariesVisitor.cs
blob: 509d54e8bf53a16abe3bddec7b305f241b973348 (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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using Xamarin.Forms.Xaml.Internals;

namespace Xamarin.Forms.Xaml
{
	internal class FillResourceDictionariesVisitor : IXamlNodeVisitor
	{
		public FillResourceDictionariesVisitor(HydratationContext context)
		{
			Context = context;
		}

		HydratationContext Context { get; }

		Dictionary<INode, object> Values
		{
			get { return Context.Values; }
		}

		public TreeVisitingMode VisitingMode => TreeVisitingMode.TopDown;
		public bool StopOnDataTemplate => true;
		public bool StopOnResourceDictionary => false;
		public bool VisitNodeOnDataTemplate => false;

		public void Visit(ValueNode node, INode parentNode)
		{
			var parentElement = parentNode as IElementNode;
			var value = Values [node];
			var source = Values [parentNode];

			XmlName propertyName;
			if (ApplyPropertiesVisitor.TryGetPropertyName(node, parentNode, out propertyName)) {
				if (parentElement.SkipProperties.Contains(propertyName))
					return;
				if (parentElement.SkipPrefix(node.NamespaceResolver.LookupPrefix(propertyName.NamespaceURI)))
					return;
				if (propertyName.NamespaceURI == "http://schemas.openxmlformats.org/markup-compatibility/2006" &&
					propertyName.LocalName == "Ignorable") 
					return;
				if (propertyName.LocalName != "MergedWith")
					return;
				ApplyPropertiesVisitor.SetPropertyValue(source, propertyName, value, Context.RootElement, node, Context, node);
			}

		}

		public void Visit(MarkupNode node, INode parentNode)
		{
		}

		public void Visit(ElementNode node, INode parentNode)
		{
			var value = Values[node];
			var parentElement = parentNode as IElementNode;
			var markupExtension = value as IMarkupExtension;
			var valueProvider = value as IValueProvider;

			//Set Resources in ResourcesDictionaries
			if (IsCollectionItem(node, parentNode) && parentNode is IElementNode)
			{
				if (typeof (IEnumerable).IsAssignableFrom(Context.Types[parentElement]))
				{
					var source = Values[parentNode];
					if (typeof (ResourceDictionary).IsAssignableFrom(Context.Types[parentElement]) && value is Style &&
					    !node.Properties.ContainsKey(XmlName.xKey))
					{
						node.Accept(new ApplyPropertiesVisitor(Context), parentNode);
						if (markupExtension != null)
						{
							var serviceProvider = new XamlServiceProvider(node, Context);
							value = markupExtension.ProvideValue(serviceProvider);
						}
						if (valueProvider != null)
						{
							var serviceProvider = new XamlServiceProvider(node, Context);
							value = valueProvider.ProvideValue(serviceProvider);
						}
						((ResourceDictionary)source).Add(value as Style);
					}
					else if (typeof (ResourceDictionary).IsAssignableFrom(Context.Types[parentElement]) && !node.Properties.ContainsKey(XmlName.xKey))
						throw new XamlParseException("resources in ResourceDictionary require a x:Key attribute", node);
					else if (typeof (ResourceDictionary).IsAssignableFrom(Context.Types[parentElement]) && node.Properties.ContainsKey(XmlName.xKey))
					{
						node.Accept(new ApplyPropertiesVisitor(Context), parentNode);
						if (markupExtension != null)
						{
							var serviceProvider = new XamlServiceProvider(node, Context);
							value = markupExtension.ProvideValue(serviceProvider);
						}
						if (valueProvider != null)
						{
							var serviceProvider = new XamlServiceProvider(node, Context);
							value = valueProvider.ProvideValue(serviceProvider);
						}
						((ResourceDictionary)source).Add((string)(((ValueNode)node.Properties[XmlName.xKey]).Value), value);
					}
				}
			}

			//Set RD to VE
			XmlName propertyName;
			if (ApplyPropertiesVisitor.TryGetPropertyName(node, parentNode, out propertyName))
			{
				if ((propertyName.LocalName == "Resources" ||
				     propertyName.LocalName.EndsWith(".Resources", StringComparison.Ordinal)) && value is ResourceDictionary)
				{
					var source = Values[parentNode];
					ApplyPropertiesVisitor.SetPropertyValue(source, propertyName, value, Context.RootElement, node, Context, node);
				}
			}
		}

		public void Visit(RootNode node, INode parentNode)
		{
		}

		public void Visit(ListNode node, INode parentNode)
		{
		}

		static bool IsCollectionItem(INode node, INode parentNode)
		{
			var parentList = parentNode as IListNode;
			if (parentList == null)
				return false;
			return parentList.CollectionItems.Contains(node);
		}
	}
}