summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Xaml/XamlServiceProvider.cs
blob: 77ed6345c7a7d6d6bdc73f6162bf64d5dc6cf996 (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
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Xml;
using Xamarin.Forms.Internals;

namespace Xamarin.Forms.Xaml.Internals
{
	public class XamlServiceProvider : IServiceProvider
	{
		readonly Dictionary<Type, object> services = new Dictionary<Type, object>();

		internal XamlServiceProvider(INode node, HydratationContext context)
		{
			object targetObject;
			if (node != null && node.Parent != null && context.Values.TryGetValue(node.Parent, out targetObject))
				IProvideValueTarget = new XamlValueTargetProvider(targetObject, node, context, null);
			if (context != null)
				IRootObjectProvider = new XamlRootObjectProvider(context.RootElement);
			if (node != null)
			{
				IXamlTypeResolver = new XamlTypeResolver(node.NamespaceResolver, XamlParser.GetElementType,
					context.RootElement.GetType().GetTypeInfo().Assembly);

				var enode = node;
				while (enode != null && !(enode is IElementNode))
					enode = enode.Parent;
				if (enode != null)
					INameScopeProvider = new NameScopeProvider { NameScope = (enode as IElementNode).Namescope };
			}

			var xmlLineInfo = node as IXmlLineInfo;
			if (xmlLineInfo != null)
				IXmlLineInfoProvider = new XmlLineInfoProvider(xmlLineInfo);

			IValueConverterProvider = new ValueConverterProvider();
		}

		public XamlServiceProvider()
		{
			IValueConverterProvider = new ValueConverterProvider();
		}

		internal IProvideValueTarget IProvideValueTarget
		{
			get { return (IProvideValueTarget)GetService(typeof (IProvideValueTarget)); }
			set { services[typeof (IProvideValueTarget)] = value; }
		}

		internal IXamlTypeResolver IXamlTypeResolver
		{
			get { return (IXamlTypeResolver)GetService(typeof (IXamlTypeResolver)); }
			set { services[typeof (IXamlTypeResolver)] = value; }
		}

		internal IRootObjectProvider IRootObjectProvider
		{
			get { return (IRootObjectProvider)GetService(typeof (IRootObjectProvider)); }
			set { services[typeof (IRootObjectProvider)] = value; }
		}

		internal IXmlLineInfoProvider IXmlLineInfoProvider
		{
			get { return (IXmlLineInfoProvider)GetService(typeof (IXmlLineInfoProvider)); }
			set { services[typeof (IXmlLineInfoProvider)] = value; }
		}

		internal INameScopeProvider INameScopeProvider
		{
			get { return (INameScopeProvider)GetService(typeof (INameScopeProvider)); }
			set { services[typeof (INameScopeProvider)] = value; }
		}

		internal IValueConverterProvider IValueConverterProvider
		{
			get { return (IValueConverterProvider)GetService(typeof (IValueConverterProvider)); }
			set { services[typeof (IValueConverterProvider)] = value; }
		}

		public object GetService(Type serviceType)
		{
			object service;
			return services.TryGetValue(serviceType, out service) ? service : null;
		}

		public void Add(Type type, object service)
		{
			services.Add(type, service);
		}
	}

	internal class XamlValueTargetProvider : IProvideParentValues, IProvideValueTarget
	{
		public XamlValueTargetProvider(object targetObject, INode node, HydratationContext context, object targetProperty)
		{
			Context = context;
			Node = node;
			TargetObject = targetObject;
			TargetProperty = targetProperty;
		}

		INode Node { get; }

		HydratationContext Context { get; }

		public object TargetObject { get; }

		public object TargetProperty
		{
			get { throw new NotImplementedException(); }
			private set { }
		}

		IEnumerable<object> IProvideParentValues.ParentObjects
		{
			get
			{
				if (Node == null || Context == null)
					yield break;
				var n = Node;
				object obj = null;
				var context = Context;
				while (n.Parent != null && context != null)
				{
					if (n.Parent is IElementNode)
					{
						if (context.Values.TryGetValue(n.Parent, out obj))
							yield return obj;
						else
						{
							context = context.ParentContext;
							continue;
						}
					}
					n = n.Parent;
				}
			}
		}
	}

	public class SimpleValueTargetProvider : IProvideParentValues, IProvideValueTarget
	{
		readonly object[] objectAndParents;

		public SimpleValueTargetProvider(object[] objectAndParents)
		{
			if (objectAndParents == null)
				throw new ArgumentNullException("objectAndParents");
			if (objectAndParents.Length == 0)
				throw new ArgumentException();

			this.objectAndParents = objectAndParents;
		}

		IEnumerable<object> IProvideParentValues.ParentObjects
		{
			get { return objectAndParents; }
		}

		object IProvideValueTarget.TargetObject
		{
			get { return objectAndParents[0]; }
		}

		object IProvideValueTarget.TargetProperty
		{
			get { throw new NotImplementedException(); }
		}
	}

	public class XamlTypeResolver : IXamlTypeResolver
	{
		readonly Assembly currentAssembly;
		readonly GetTypeFromXmlName getTypeFromXmlName;
		readonly IXmlNamespaceResolver namespaceResolver;

		public XamlTypeResolver(IXmlNamespaceResolver namespaceResolver, Assembly currentAssembly)
			: this(namespaceResolver, XamlParser.GetElementType, currentAssembly)
		{
		}

		internal XamlTypeResolver(IXmlNamespaceResolver namespaceResolver, GetTypeFromXmlName getTypeFromXmlName,
			Assembly currentAssembly)
		{
			this.currentAssembly = currentAssembly;
			if (namespaceResolver == null)
				throw new ArgumentNullException();
			if (getTypeFromXmlName == null)
				throw new ArgumentNullException();

			this.namespaceResolver = namespaceResolver;
			this.getTypeFromXmlName = getTypeFromXmlName;
		}

		Type IXamlTypeResolver.Resolve(string qualifiedTypeName, IServiceProvider serviceProvider)
		{
			XamlParseException e;
			var type = Resolve(qualifiedTypeName, serviceProvider, out e);
			if (e != null)
				throw e;
			return type;
		}

		bool IXamlTypeResolver.TryResolve(string qualifiedTypeName, out Type type)
		{
			XamlParseException exception;
			type = Resolve(qualifiedTypeName, null, out exception);
			return exception == null;
		}

		Type Resolve(string qualifiedTypeName, IServiceProvider serviceProvider, out XamlParseException exception)
		{
			exception = null;
			var split = qualifiedTypeName.Split(':');
			if (split.Length > 2)
				return null;

			string prefix, name;
			if (split.Length == 2)
			{
				prefix = split[0];
				name = split[1];
			}
			else
			{
				prefix = "";
				name = split[0];
			}

			IXmlLineInfo xmlLineInfo = null;
			if (serviceProvider != null)
			{
				var lineInfoProvider = serviceProvider.GetService(typeof (IXmlLineInfoProvider)) as IXmlLineInfoProvider;
				if (lineInfoProvider != null)
					xmlLineInfo = lineInfoProvider.XmlLineInfo;
			}

			var namespaceuri = prefix == null ? "" : namespaceResolver.LookupNamespace(prefix);
			if (namespaceuri == null)
			{
				exception = new XamlParseException(string.Format("No xmlns declaration for prefix \"{0}\"", prefix), xmlLineInfo);
				return null;
			}

			return getTypeFromXmlName(new XmlType(namespaceuri, name, null), xmlLineInfo, currentAssembly, out exception);
		}

		internal delegate Type GetTypeFromXmlName(
			XmlType xmlType, IXmlLineInfo xmlInfo, Assembly currentAssembly, out XamlParseException exception);
	}

	internal class XamlRootObjectProvider : IRootObjectProvider
	{
		public XamlRootObjectProvider(object rootObject)
		{
			RootObject = rootObject;
		}

		public object RootObject { get; }
	}

	public class XmlLineInfoProvider : IXmlLineInfoProvider
	{
		public XmlLineInfoProvider(IXmlLineInfo xmlLineInfo)
		{
			XmlLineInfo = xmlLineInfo;
		}

		public IXmlLineInfo XmlLineInfo { get; }
	}

	internal interface INameScopeProvider
	{
		INameScope NameScope { get; }
	}

	public class NameScopeProvider : INameScopeProvider
	{
		public INameScope NameScope { get; set; }
	}

	public class XmlNamespaceResolver : IXmlNamespaceResolver
	{
		readonly Dictionary<string, string> namespaces = new Dictionary<string, string>();

		public IDictionary<string, string> GetNamespacesInScope(XmlNamespaceScope scope)
		{
			throw new NotImplementedException();
		}

		public string LookupNamespace(string prefix)
		{
			string result;
			if (namespaces.TryGetValue(prefix, out result))
				return result;
			return null;
		}

		public string LookupPrefix(string namespaceName)
		{
			throw new NotImplementedException();
		}

		public void Add(string prefix, string ns)
		{
			namespaces.Add(prefix, ns);
		}
	}
}