2 using System.Collections.Generic;
3 using System.Globalization;
5 using System.Reflection;
7 using Tizen.NUI.Internals;
8 using Tizen.NUI.Binding;
11 namespace Tizen.NUI.Xaml
13 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)
89 value = Activator.CreateInstance(type);
91 catch (TargetInvocationException e)
93 if (e.InnerException is XamlParseException || e.InnerException is XmlException)
94 throw e.InnerException;
101 var markup = value as IMarkupExtension;
102 if (markup != null && (value is TypeExtension || value is StaticExtension || value is ArrayExtension))
104 var serviceProvider = new XamlServiceProvider(node, Context);
106 var visitor = new ApplyPropertiesVisitor(Context);
107 foreach (var cnode in node.Properties.Values.ToList())
108 cnode.Accept(visitor, node);
109 foreach (var cnode in node.CollectionItems)
110 cnode.Accept(visitor, node);
112 value = markup.ProvideValue(serviceProvider);
115 if (!node.Properties.TryGetValue(XmlName.xKey, out xKey))
118 node.Properties.Clear();
119 node.CollectionItems.Clear();
122 node.Properties.Add(XmlName.xKey, xKey);
124 Values[node] = value;
127 if (value is BindableObject)
128 NameScope.SetNameScope(value as BindableObject, node.Namescope);
131 public void Visit(RootNode node, INode parentNode)
133 var rnode = (XamlLoader.RuntimeRootNode)node;
134 Values[node] = rnode.Root;
135 Context.Types[node] = rnode.Root.GetType();
136 var bindableRoot = rnode.Root as BindableObject;
137 if (bindableRoot != null)
138 NameScope.SetNameScope(bindableRoot, node.Namescope);
141 public void Visit(ListNode node, INode parentNode)
143 //this is a gross hack to keep ListNode alive. ListNode must go in favor of Properties
145 if (ApplyPropertiesVisitor.TryGetPropertyName(node, parentNode, out name))
149 bool ValidateCtorArguments(Type nodeType, IElementNode node, out string missingArgName)
151 missingArgName = null;
153 nodeType.GetTypeInfo()
154 .DeclaredConstructors.FirstOrDefault(
156 ci.GetParameters().Length != 0 && ci.IsPublic &&
157 ci.GetParameters().All(pi => pi.CustomAttributes.Any(attr => attr.AttributeType == typeof (ParameterAttribute))));
158 if (ctorInfo == null)
160 foreach (var parameter in ctorInfo.GetParameters())
162 // Modify the namespace
164 parameter.CustomAttributes.First(ca => ca.AttributeType.FullName == "Tizen.NUI.Binding.ParameterAttribute")
165 .ConstructorArguments.First()
167 if (!node.Properties.ContainsKey(new XmlName("", propname)))
169 missingArgName = propname;
177 public object CreateFromParameterizedConstructor(Type nodeType, IElementNode node)
180 nodeType.GetTypeInfo()
181 .DeclaredConstructors.FirstOrDefault(
183 ci.GetParameters().Length != 0 && ci.IsPublic &&
184 ci.GetParameters().All(pi => pi.CustomAttributes.Any(attr => attr.AttributeType == typeof (ParameterAttribute))));
185 object[] arguments = CreateArgumentsArray(node, ctorInfo);
186 return ctorInfo.Invoke(arguments);
189 public object CreateFromFactory(Type nodeType, IElementNode node)
191 object[] arguments = CreateArgumentsArray(node);
193 if (!node.Properties.ContainsKey(XmlName.xFactoryMethod))
196 return Activator.CreateInstance(nodeType, arguments);
199 var factoryMethod = ((string)((ValueNode)node.Properties[XmlName.xFactoryMethod]).Value);
200 Type[] types = arguments == null ? new Type[0] : arguments.Select(a => a.GetType()).ToArray();
201 Func<MethodInfo, bool> isMatch = m => {
202 if (m.Name != factoryMethod)
204 var p = m.GetParameters();
205 if (p.Length != types.Length)
209 for (var i = 0; i < p.Length; i++) {
210 if ((p [i].ParameterType.IsAssignableFrom(types [i])))
212 var op_impl = p[i].ParameterType.GetImplicitConversionOperator(fromType: types[i], toType: p[i].ParameterType)
213 ?? types[i].GetImplicitConversionOperator(fromType: types[i], toType: p[i].ParameterType);
217 arguments [i] = op_impl.Invoke(null, new [] { arguments [i]});
221 var mi = nodeType.GetRuntimeMethods().FirstOrDefault(isMatch);
223 throw new MissingMemberException($"No static method found for {nodeType.FullName}::{factoryMethod} ({string.Join(", ", types.Select(t => t.FullName))})");
224 return mi.Invoke(null, arguments);
227 public object[] CreateArgumentsArray(IElementNode enode)
229 if (!enode.Properties.ContainsKey(XmlName.xArguments))
231 var node = enode.Properties[XmlName.xArguments];
232 var elementNode = node as ElementNode;
233 if (elementNode != null)
235 var array = new object[1];
236 array[0] = Values[elementNode];
240 var listnode = node as ListNode;
241 if (listnode != null)
243 var array = new object[listnode.CollectionItems.Count];
244 for (var i = 0; i < listnode.CollectionItems.Count; i++)
245 array[i] = Values[(ElementNode)listnode.CollectionItems[i]];
251 public object[] CreateArgumentsArray(IElementNode enode, ConstructorInfo ctorInfo)
253 var n = ctorInfo.GetParameters().Length;
254 var array = new object[n];
255 for (var i = 0; i < n; i++)
257 var parameter = ctorInfo.GetParameters()[i];
259 parameter.CustomAttributes.First(attr => attr.AttributeType == typeof (ParameterAttribute))
260 .ConstructorArguments.First()
262 var name = new XmlName("", propname);
264 if (!enode.Properties.TryGetValue(name, out node))
266 throw new XamlParseException(
267 String.Format("The Property {0} is required to create a {1} object.", propname, ctorInfo.DeclaringType.FullName),
268 enode as IXmlLineInfo);
270 if (!enode.SkipProperties.Contains(name))
271 enode.SkipProperties.Add(name);
272 var value = Context.Values[node];
273 var serviceProvider = new XamlServiceProvider(enode, Context);
274 var convertedValue = value.ConvertTo(parameter.ParameterType, () => parameter, serviceProvider);
275 array[i] = convertedValue;
281 static bool IsXaml2009LanguagePrimitive(IElementNode node)
283 return node.NamespaceURI == XamlParser.X2009Uri;
286 static object CreateLanguagePrimitive(Type nodeType, IElementNode node)
289 if (nodeType == typeof (string))
290 value = String.Empty;
291 else if (nodeType == typeof (Uri))
294 value = Activator.CreateInstance(nodeType);
296 if (node.CollectionItems.Count == 1 && node.CollectionItems[0] is ValueNode &&
297 ((ValueNode)node.CollectionItems[0]).Value is string)
299 var valuestring = ((ValueNode)node.CollectionItems[0]).Value as string;
301 if (nodeType == typeof(SByte)) {
303 if (sbyte.TryParse(valuestring, NumberStyles.Number, CultureInfo.InvariantCulture, out retval))
306 if (nodeType == typeof(Int16)) {
308 if (short.TryParse(valuestring, NumberStyles.Number, CultureInfo.InvariantCulture, out retval))
311 if (nodeType == typeof(Int32)) {
313 if (int.TryParse(valuestring, NumberStyles.Number, CultureInfo.InvariantCulture, out retval))
316 if (nodeType == typeof(Int64)) {
318 if (long.TryParse(valuestring, NumberStyles.Number, CultureInfo.InvariantCulture, out retval))
321 if (nodeType == typeof(Byte)) {
323 if (byte.TryParse(valuestring, NumberStyles.Number, CultureInfo.InvariantCulture, out retval))
326 if (nodeType == typeof(UInt16)) {
328 if (ushort.TryParse(valuestring, NumberStyles.Number, CultureInfo.InvariantCulture, out retval))
331 if (nodeType == typeof(UInt32)) {
333 if (uint.TryParse(valuestring, NumberStyles.Number, CultureInfo.InvariantCulture, out retval))
336 if (nodeType == typeof(UInt64)) {
338 if (ulong.TryParse(valuestring, NumberStyles.Number, CultureInfo.InvariantCulture, out retval))
341 if (nodeType == typeof(Single)) {
343 if (float.TryParse(valuestring, NumberStyles.Number, CultureInfo.InvariantCulture, out retval))
346 if (nodeType == typeof(Double)) {
348 if (double.TryParse(valuestring, NumberStyles.Number, CultureInfo.InvariantCulture, out retval))
351 if (nodeType == typeof (Boolean))
354 if (bool.TryParse(valuestring, out outbool))
357 if (nodeType == typeof(TimeSpan)) {
359 if (TimeSpan.TryParse(valuestring, CultureInfo.InvariantCulture, out retval))
362 if (nodeType == typeof (char))
365 if (char.TryParse(valuestring, out retval))
368 if (nodeType == typeof (string))
370 if (nodeType == typeof (decimal))
373 if (decimal.TryParse(valuestring, NumberStyles.Number, CultureInfo.InvariantCulture, out retval))
377 else if (nodeType == typeof (Uri))
380 if (Uri.TryCreate(valuestring, UriKind.RelativeOrAbsolute, out retval))