2 using System.Collections.Generic;
3 using System.Globalization;
5 using System.Reflection;
7 using Tizen.NUI.Binding.Internals;
8 using Tizen.NUI.Binding;
11 namespace Tizen.NUI.Xaml
13 internal class CreateValuesVisitor : IXamlNodeVisitor
15 public CreateValuesVisitor(HydrationContext context)
20 Dictionary<INode, object> Values
22 get { return Context.Values; }
25 HydrationContext Context { get; }
27 public TreeVisitingMode VisitingMode => TreeVisitingMode.BottomUp;
28 public bool StopOnDataTemplate => true;
29 public bool StopOnResourceDictionary => false;
30 public bool VisitNodeOnDataTemplate => false;
31 public bool SkipChildren(INode node, INode parentNode) => false;
33 public void Visit(ValueNode node, INode parentNode)
35 Values[node] = node.Value;
38 public void Visit(MarkupNode node, INode parentNode)
42 public void Visit(ElementNode node, INode parentNode)
46 XamlParseException xpe;
47 var type = XamlParser.GetElementType(node.XmlType, node, Context.RootElement?.GetType().GetTypeInfo().Assembly,
52 Context.Types[node] = type;
54 if (IsXaml2009LanguagePrimitive(node))
55 value = CreateLanguagePrimitive(type, node);
56 else if (node.Properties.ContainsKey(XmlName.xArguments) || node.Properties.ContainsKey(XmlName.xFactoryMethod))
57 value = CreateFromFactory(type, node);
60 .DeclaredConstructors.Any(
62 ci.IsPublic && ci.GetParameters().Length != 0 &&
63 ci.GetParameters().All(pi => pi.CustomAttributes.Any(attr => attr.AttributeType == typeof (ParameterAttribute)))) &&
64 ValidateCtorArguments(type, node, out ctorargname))
65 value = CreateFromParameterizedConstructor(type, node);
66 else if (!type.GetTypeInfo().DeclaredConstructors.Any(ci => ci.IsPublic && ci.GetParameters().Length == 0) &&
67 !ValidateCtorArguments(type, node, out ctorargname))
69 throw new XamlParseException($"The Property {ctorargname} is required to create a {type?.FullName} object.", node);
73 //this is a trick as the DataTemplate parameterless ctor is internal, and we can't CreateInstance(..., false) on WP7
76 if (type == typeof (DataTemplate))
77 value = new DataTemplate();
78 if (type == typeof (ControlTemplate))
79 value = new ControlTemplate();
80 if (value == null && node.CollectionItems.Any() && node.CollectionItems.First() is ValueNode)
82 var serviceProvider = new XamlServiceProvider(node, Context);
83 var converted = ((ValueNode)node.CollectionItems.First()).Value.ConvertTo(type, () => type.GetTypeInfo(),
85 if (converted != null && converted.GetType() == type)
90 value = Activator.CreateInstance(type);
91 if (value is BindableObject)
93 ((BindableObject)value).isCreateByXaml = true;
97 catch (TargetInvocationException e)
99 if (e.InnerException is XamlParseException || e.InnerException is XmlException)
100 throw e.InnerException;
105 Values[node] = value;
107 var markup = value as IMarkupExtension;
108 if (markup != null && (value is TypeExtension || value is StaticExtension || value is ArrayExtension))
110 var serviceProvider = new XamlServiceProvider(node, Context);
112 var visitor = new ApplyPropertiesVisitor(Context);
113 foreach (var cnode in node.Properties.Values.ToList())
114 cnode.Accept(visitor, node);
115 foreach (var cnode in node.CollectionItems)
116 cnode.Accept(visitor, node);
118 value = markup.ProvideValue(serviceProvider);
121 if (!node.Properties.TryGetValue(XmlName.xKey, out xKey))
124 node.Properties.Clear();
125 node.CollectionItems.Clear();
128 node.Properties.Add(XmlName.xKey, xKey);
130 Values[node] = value;
133 if (value is BindableObject)
134 NameScope.SetNameScope(value as BindableObject, node.Namescope);
137 public void Visit(RootNode node, INode parentNode)
139 var rnode = (XamlLoader.RuntimeRootNode)node;
140 Values[node] = rnode.Root;
141 Context.Types[node] = rnode.Root.GetType();
142 var bindableRoot = rnode.Root as BindableObject;
143 if (bindableRoot != null)
144 NameScope.SetNameScope(bindableRoot, node.Namescope);
147 public void Visit(ListNode node, INode parentNode)
149 //this is a gross hack to keep ListNode alive. ListNode must go in favor of Properties
151 if (ApplyPropertiesVisitor.TryGetPropertyName(node, parentNode, out name))
155 bool ValidateCtorArguments(Type nodeType, IElementNode node, out string missingArgName)
157 missingArgName = null;
159 nodeType.GetTypeInfo()
160 .DeclaredConstructors.FirstOrDefault(
162 ci.GetParameters().Length != 0 && ci.IsPublic &&
163 ci.GetParameters().All(pi => pi.CustomAttributes.Any(attr => attr.AttributeType == typeof (ParameterAttribute))));
164 if (ctorInfo == null)
166 foreach (var parameter in ctorInfo.GetParameters())
168 // Modify the namespace
170 parameter.CustomAttributes.First(ca => ca.AttributeType.FullName == "Tizen.NUI.Binding.ParameterAttribute")
171 .ConstructorArguments.First()
173 if (!node.Properties.ContainsKey(new XmlName("", propname)))
175 missingArgName = propname;
183 public object CreateFromParameterizedConstructor(Type nodeType, IElementNode node)
186 nodeType.GetTypeInfo()
187 .DeclaredConstructors.FirstOrDefault(
189 ci.GetParameters().Length != 0 && ci.IsPublic &&
190 ci.GetParameters().All(pi => pi.CustomAttributes.Any(attr => attr.AttributeType == typeof (ParameterAttribute))));
191 object[] arguments = CreateArgumentsArray(node, ctorInfo);
192 return ctorInfo.Invoke(arguments);
195 public object CreateFromFactory(Type nodeType, IElementNode node)
197 object[] arguments = CreateArgumentsArray(node);
199 if (!node.Properties.ContainsKey(XmlName.xFactoryMethod))
202 object ret = Activator.CreateInstance(nodeType, arguments);
203 if (ret is BindableObject)
205 ((BindableObject)ret).isCreateByXaml = true;
210 var factoryMethod = ((string)((ValueNode)node.Properties[XmlName.xFactoryMethod]).Value);
211 Type[] types = arguments == null ? new Type[0] : arguments.Select(a => a.GetType()).ToArray();
212 Func<MethodInfo, bool> isMatch = m => {
213 if (m.Name != factoryMethod)
215 var p = m.GetParameters();
216 if (p.Length != types.Length)
220 for (var i = 0; i < p.Length; i++) {
221 if ((p [i].ParameterType.IsAssignableFrom(types [i])))
223 var op_impl = p[i].ParameterType.GetImplicitConversionOperator(fromType: types[i], toType: p[i].ParameterType)
224 ?? types[i].GetImplicitConversionOperator(fromType: types[i], toType: p[i].ParameterType);
228 arguments [i] = op_impl.Invoke(null, new [] { arguments [i]});
232 var mi = nodeType.GetRuntimeMethods().FirstOrDefault(isMatch);
234 throw new MissingMemberException($"No static method found for {nodeType.FullName}::{factoryMethod} ({string.Join(", ", types.Select(t => t.FullName))})");
235 return mi.Invoke(null, arguments);
238 public object[] CreateArgumentsArray(IElementNode enode)
240 if (!enode.Properties.ContainsKey(XmlName.xArguments))
242 var node = enode.Properties[XmlName.xArguments];
243 var elementNode = node as ElementNode;
244 if (elementNode != null)
246 var array = new object[1];
247 array[0] = Values[elementNode];
251 var listnode = node as ListNode;
252 if (listnode != null)
254 var array = new object[listnode.CollectionItems.Count];
255 for (var i = 0; i < listnode.CollectionItems.Count; i++)
256 array[i] = Values[(ElementNode)listnode.CollectionItems[i]];
262 public object[] CreateArgumentsArray(IElementNode enode, ConstructorInfo ctorInfo)
264 var n = ctorInfo.GetParameters().Length;
265 var array = new object[n];
266 for (var i = 0; i < n; i++)
268 var parameter = ctorInfo.GetParameters()[i];
270 parameter.CustomAttributes.First(attr => attr.AttributeType == typeof (ParameterAttribute))
271 .ConstructorArguments.First()
273 var name = new XmlName("", propname);
275 if (!enode.Properties.TryGetValue(name, out node))
277 throw new XamlParseException(
278 String.Format("The Property {0} is required to create a {1} object.", propname, ctorInfo.DeclaringType.FullName),
279 enode as IXmlLineInfo);
281 if (!enode.SkipProperties.Contains(name))
282 enode.SkipProperties.Add(name);
283 var value = Context.Values[node];
284 var serviceProvider = new XamlServiceProvider(enode, Context);
285 var convertedValue = value.ConvertTo(parameter.ParameterType, () => parameter, serviceProvider);
286 array[i] = convertedValue;
292 static bool IsXaml2009LanguagePrimitive(IElementNode node)
294 return node.NamespaceURI == XamlParser.X2009Uri;
297 static object CreateLanguagePrimitive(Type nodeType, IElementNode node)
300 if (nodeType == typeof(string))
301 value = String.Empty;
302 else if (nodeType == typeof(Uri))
306 value = Activator.CreateInstance(nodeType);
307 if (value is BindableObject)
309 ((BindableObject)value).isCreateByXaml = true;
313 if (node.CollectionItems.Count == 1 && node.CollectionItems[0] is ValueNode &&
314 ((ValueNode)node.CollectionItems[0]).Value is string)
316 var valuestring = ((ValueNode)node.CollectionItems[0]).Value as string;
318 if (nodeType == typeof(SByte)) {
320 if (sbyte.TryParse(valuestring, NumberStyles.Number, CultureInfo.InvariantCulture, out retval))
323 if (nodeType == typeof(Int16)) {
325 if (short.TryParse(valuestring, NumberStyles.Number, CultureInfo.InvariantCulture, out retval))
328 if (nodeType == typeof(Int32)) {
330 if (int.TryParse(valuestring, NumberStyles.Number, CultureInfo.InvariantCulture, out retval))
333 if (nodeType == typeof(Int64)) {
335 if (long.TryParse(valuestring, NumberStyles.Number, CultureInfo.InvariantCulture, out retval))
338 if (nodeType == typeof(Byte)) {
340 if (byte.TryParse(valuestring, NumberStyles.Number, CultureInfo.InvariantCulture, out retval))
343 if (nodeType == typeof(UInt16)) {
345 if (ushort.TryParse(valuestring, NumberStyles.Number, CultureInfo.InvariantCulture, out retval))
348 if (nodeType == typeof(UInt32)) {
350 if (uint.TryParse(valuestring, NumberStyles.Number, CultureInfo.InvariantCulture, out retval))
353 if (nodeType == typeof(UInt64)) {
355 if (ulong.TryParse(valuestring, NumberStyles.Number, CultureInfo.InvariantCulture, out retval))
358 if (nodeType == typeof(Single)) {
360 if (float.TryParse(valuestring, NumberStyles.Number, CultureInfo.InvariantCulture, out retval))
363 if (nodeType == typeof(Double)) {
365 if (double.TryParse(valuestring, NumberStyles.Number, CultureInfo.InvariantCulture, out retval))
368 if (nodeType == typeof (Boolean))
371 if (bool.TryParse(valuestring, out outbool))
374 if (nodeType == typeof(TimeSpan)) {
376 if (TimeSpan.TryParse(valuestring, CultureInfo.InvariantCulture, out retval))
379 if (nodeType == typeof (char))
382 if (char.TryParse(valuestring, out retval))
385 if (nodeType == typeof (string))
387 if (nodeType == typeof (decimal))
390 if (decimal.TryParse(valuestring, NumberStyles.Number, CultureInfo.InvariantCulture, out retval))
394 else if (nodeType == typeof (Uri))
397 if (Uri.TryCreate(valuestring, UriKind.RelativeOrAbsolute, out retval))