2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Reflection;
6 using Tizen.NUI.Binding;
7 using Tizen.NUI.Binding.Internals;
9 namespace Tizen.NUI.Xaml
11 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
12 [EditorBrowsable(EditorBrowsableState.Never)]
13 public class XamlServiceProvider : IServiceProvider
15 readonly Dictionary<Type, object> services = new Dictionary<Type, object>();
17 internal XamlServiceProvider(INode node, HydrationContext context)
20 if (node != null && node.Parent != null && context.Values.TryGetValue(node.Parent, out targetObject))
21 IProvideValueTarget = new XamlValueTargetProvider(targetObject, node, context, null);
23 IRootObjectProvider = new XamlRootObjectProvider(context.RootElement);
26 IXamlTypeResolver = new XamlTypeResolver(node.NamespaceResolver, XamlParser.GetElementType,
27 context?.RootElement?.GetType().GetTypeInfo().Assembly);
29 Add(typeof(IReferenceProvider), new ReferenceProvider(node));
32 var xmlLineInfo = node as IXmlLineInfo;
33 if (xmlLineInfo != null)
34 IXmlLineInfoProvider = new XmlLineInfoProvider(xmlLineInfo);
36 IValueConverterProvider = new ValueConverterProvider();
39 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
40 [EditorBrowsable(EditorBrowsableState.Never)]
41 public XamlServiceProvider()
43 IValueConverterProvider = new ValueConverterProvider();
46 internal IProvideValueTarget IProvideValueTarget
48 get { return (IProvideValueTarget)GetService(typeof(IProvideValueTarget)); }
49 set { services[typeof(IProvideValueTarget)] = value; }
52 internal IXamlTypeResolver IXamlTypeResolver
54 get { return (IXamlTypeResolver)GetService(typeof(IXamlTypeResolver)); }
55 set { services[typeof(IXamlTypeResolver)] = value; }
58 internal IRootObjectProvider IRootObjectProvider
60 get { return (IRootObjectProvider)GetService(typeof(IRootObjectProvider)); }
61 set { services[typeof(IRootObjectProvider)] = value; }
64 internal IXmlLineInfoProvider IXmlLineInfoProvider
66 get { return (IXmlLineInfoProvider)GetService(typeof(IXmlLineInfoProvider)); }
67 set { services[typeof(IXmlLineInfoProvider)] = value; }
70 internal INameScopeProvider INameScopeProvider
72 get { return (INameScopeProvider)GetService(typeof(INameScopeProvider)); }
73 set { services[typeof(INameScopeProvider)] = value; }
76 internal IValueConverterProvider IValueConverterProvider
78 get { return (IValueConverterProvider)GetService(typeof(IValueConverterProvider)); }
79 set { services[typeof(IValueConverterProvider)] = value; }
82 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
83 [EditorBrowsable(EditorBrowsableState.Never)]
84 public object GetService(Type serviceType)
87 return services.TryGetValue(serviceType, out service) ? service : null;
90 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
91 [EditorBrowsable(EditorBrowsableState.Never)]
92 public void Add(Type type, object service)
94 services.Add(type, service);
98 internal class XamlValueTargetProvider : IProvideParentValues, IProvideValueTarget
100 public XamlValueTargetProvider(object targetObject, INode node, HydrationContext context, object targetProperty)
104 TargetObject = targetObject;
105 TargetProperty = targetProperty;
110 HydrationContext Context { get; }
111 public object TargetObject { get; }
112 public object TargetProperty { get; internal set; } = null;
114 IEnumerable<object> IProvideParentValues.ParentObjects
118 if (Node == null || Context == null)
122 var context = Context;
123 while (n.Parent != null && context != null)
125 if (n.Parent is IElementNode)
127 if (context.Values.TryGetValue(n.Parent, out obj))
131 context = context.ParentContext;
141 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
142 [EditorBrowsable(EditorBrowsableState.Never)]
143 public class SimpleValueTargetProvider : IProvideParentValues, IProvideValueTarget, IReferenceProvider
145 readonly object[] objectAndParents;
146 readonly object targetProperty;
148 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
149 [EditorBrowsable(EditorBrowsableState.Never)]
150 [Obsolete("SimpleValueTargetProvider(object[] objectAndParents) is obsolete as of version 2.3.4. Please use SimpleValueTargetProvider(object[] objectAndParents, object targetProperty) instead.")]
151 public SimpleValueTargetProvider(object[] objectAndParents) : this(objectAndParents, null)
155 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
156 [EditorBrowsable(EditorBrowsableState.Never)]
157 public SimpleValueTargetProvider(object[] objectAndParents, object targetProperty)
159 if (objectAndParents == null)
160 throw new ArgumentNullException(nameof(objectAndParents));
161 if (objectAndParents.Length == 0)
162 throw new ArgumentException(nameof(objectAndParents.Length));
164 this.objectAndParents = objectAndParents;
165 this.targetProperty = targetProperty;
168 IEnumerable<object> IProvideParentValues.ParentObjects
171 object IProvideValueTarget.TargetObject
172 => objectAndParents[0];
174 object IProvideValueTarget.TargetProperty
177 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
178 [EditorBrowsable(EditorBrowsableState.Never)]
179 public object FindByName(string name)
181 for (var i = 0; i < objectAndParents.Length; i++)
183 var bo = objectAndParents[i] as BindableObject;
184 if (bo == null) continue;
185 var ns = NameScope.GetNameScope(bo) as INameScope;
186 if (ns == null) continue;
187 var value = ns.FindByName(name);
195 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
196 [EditorBrowsable(EditorBrowsableState.Never)]
197 public class XamlTypeResolver : IXamlTypeResolver
199 readonly Assembly currentAssembly;
200 readonly GetTypeFromXmlName getTypeFromXmlName;
201 readonly IXmlNamespaceResolver namespaceResolver;
203 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
204 [EditorBrowsable(EditorBrowsableState.Never)]
205 public XamlTypeResolver(IXmlNamespaceResolver namespaceResolver, Assembly currentAssembly)
206 : this(namespaceResolver, XamlParser.GetElementType, currentAssembly)
210 internal XamlTypeResolver(IXmlNamespaceResolver namespaceResolver, GetTypeFromXmlName getTypeFromXmlName,
211 Assembly currentAssembly)
213 this.currentAssembly = currentAssembly;
214 if (namespaceResolver == null)
215 throw new ArgumentNullException(nameof(namespaceResolver));
216 if (getTypeFromXmlName == null)
217 throw new ArgumentNullException(nameof(getTypeFromXmlName));
219 this.namespaceResolver = namespaceResolver;
220 this.getTypeFromXmlName = getTypeFromXmlName;
223 Type IXamlTypeResolver.Resolve(string qualifiedTypeName, IServiceProvider serviceProvider)
225 XamlParseException e;
226 var type = Resolve(qualifiedTypeName, serviceProvider, out e);
232 bool IXamlTypeResolver.TryResolve(string qualifiedTypeName, out Type type)
234 XamlParseException exception;
235 type = Resolve(qualifiedTypeName, null, out exception);
236 return exception == null;
239 Type Resolve(string qualifiedTypeName, IServiceProvider serviceProvider, out XamlParseException exception)
242 var split = qualifiedTypeName.Split(':');
243 if (split.Length > 2)
247 if (split.Length == 2)
258 IXmlLineInfo xmlLineInfo = null;
259 if (serviceProvider != null)
261 var lineInfoProvider = serviceProvider.GetService(typeof(IXmlLineInfoProvider)) as IXmlLineInfoProvider;
262 if (lineInfoProvider != null)
263 xmlLineInfo = lineInfoProvider.XmlLineInfo;
266 var namespaceuri = namespaceResolver.LookupNamespace(prefix);
267 if (namespaceuri == null)
269 exception = new XamlParseException(string.Format("No xmlns declaration for prefix \"{0}\"", prefix), xmlLineInfo);
273 return getTypeFromXmlName(new XmlType(namespaceuri, name, null), xmlLineInfo, currentAssembly, out exception);
276 internal delegate Type GetTypeFromXmlName(
277 XmlType xmlType, IXmlLineInfo xmlInfo, Assembly currentAssembly, out XamlParseException exception);
280 internal class XamlRootObjectProvider : IRootObjectProvider
282 public XamlRootObjectProvider(object rootObject)
284 RootObject = rootObject;
287 public object RootObject { get; }
290 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
291 [EditorBrowsable(EditorBrowsableState.Never)]
292 public class XmlLineInfoProvider : IXmlLineInfoProvider
294 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
295 [EditorBrowsable(EditorBrowsableState.Never)]
296 public XmlLineInfoProvider(IXmlLineInfo xmlLineInfo)
298 XmlLineInfo = xmlLineInfo;
301 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
302 [EditorBrowsable(EditorBrowsableState.Never)]
303 public IXmlLineInfo XmlLineInfo { get; }
306 class ReferenceProvider : IReferenceProvider
308 readonly INode _node;
309 internal ReferenceProvider(INode node)
312 public object FindByName(string name)
318 if ((value = (n as IElementNode)?.Namescope?.FindByName(name)) != null)
326 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
327 [EditorBrowsable(EditorBrowsableState.Never)]
329 public class NameScopeProvider : INameScopeProvider
331 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
332 [EditorBrowsable(EditorBrowsableState.Never)]
333 public INameScope NameScope { get; set; }
336 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
337 [EditorBrowsable(EditorBrowsableState.Never)]
338 public class XmlNamespaceResolver : IXmlNamespaceResolver
340 readonly Dictionary<string, string> namespaces = new Dictionary<string, string>();
342 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
343 [EditorBrowsable(EditorBrowsableState.Never)]
344 public IDictionary<string, string> GetNamespacesInScope(XmlNamespaceScope scope)
346 throw new NotImplementedException();
349 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
350 [EditorBrowsable(EditorBrowsableState.Never)]
351 public string LookupNamespace(string prefix)
354 if (namespaces.TryGetValue(prefix, out result))
359 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
360 [EditorBrowsable(EditorBrowsableState.Never)]
361 public string LookupPrefix(string namespaceName)
363 throw new NotImplementedException();
366 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
367 [EditorBrowsable(EditorBrowsableState.Never)]
368 public void Add(string prefix, string ns)
370 namespaces.Add(prefix, ns);