public void Visit(ElementNode node, INode parentNode)
        {
            XmlName propertyName;

            if (TryGetPropertyName(node, parentNode, out propertyName) && propertyName == XmlName._CreateContent)
            {
                var s0 = Values[parentNode];
                if (s0 is ElementTemplate)
                {
                    SetTemplate(s0 as ElementTemplate, node);
                    return;
                }
            }

            var parentElement = parentNode as IElementNode;

            propertyName = XmlName.Empty;

            //Simplify ListNodes with single elements
            var pList = parentNode as ListNode;

            if (pList != null && pList.CollectionItems.Count == 1)
            {
                propertyName  = pList.XmlName;
                parentNode    = parentNode.Parent;
                parentElement = parentNode as IElementNode;
            }

            var value = Values[node];

            if (propertyName != XmlName.Empty || TryGetPropertyName(node, parentNode, out propertyName))
            {
                if (Skips.Contains(propertyName))
                {
                    return;
                }
                if (parentElement == null)
                {
                    return;
                }
                if (parentElement.SkipProperties.Contains(propertyName))
                {
                    return;
                }

                var source = Values[parentNode];
                ProvideValue(ref value, node, source, propertyName);
                SetPropertyValue(source, propertyName, value, Context.RootElement, node, Context, node);
            }
            else if (IsCollectionItem(node, parentNode) && parentNode is IElementNode)
            {
                var source = Values[parentNode];
                ProvideValue(ref value, node, source, XmlName.Empty);
                string    contentProperty;
                Exception xpe  = null;
                var       xKey = node.Properties.ContainsKey(XmlName.xKey) ? ((ValueNode)node.Properties[XmlName.xKey]).Value as string : null;

                //ResourceDictionary
                if (xpe == null && TryAddToResourceDictionary(source as ResourceDictionary, value, xKey, node, out xpe))
                {
                    return;
                }

                // Dictionary with string key
                if (xpe == null && xKey != null)
                {
                    var indexer = GetIndexer(source, typeof(string), value.GetType());
                    if (indexer != null)
                    {
                        indexer.SetValue(source, value, new[] { xKey });
                        return;
                    }
                }

                // Collection element, implicit content, or implicit collection element.
                if (xpe == null && typeof(IEnumerable).IsAssignableFrom(Context.Types[parentElement]) && Context.Types[parentElement].GetRuntimeMethods().Any(mi => mi.Name == "Add" && mi.GetParameters().Length == 1))
                {
                    var addMethod =
                        Context.Types[parentElement].GetRuntimeMethods().First(mi => mi.Name == "Add" && mi.GetParameters().Length == 1);

                    addMethod?.Invoke(source, new[] { value });
                    return;
                }
                if (xpe == null && Context.Types[parentElement].GetRuntimeMethods().Any(mi => mi.Name == "Add" && mi.GetParameters().Length == 1))
                {
                    //if there are similar parameters in the function, this will exist issue.
                    var addMethod = Context.Types[parentElement].GetRuntimeMethods().First(mi => mi.Name == "Add" && mi.GetParameters().Length == 1);
                    if (addMethod != null)
                    {
                        addMethod.Invoke(source, new[] { value });
                    }
                    return;
                }
                if (xpe == null && (contentProperty = GetContentPropertyName(Context.Types[parentElement].GetTypeInfo())) != null)
                {
                    var name = new XmlName(node.NamespaceURI, contentProperty);
                    if (Skips.Contains(name))
                    {
                        return;
                    }
                    if (parentElement.SkipProperties.Contains(propertyName))
                    {
                        return;
                    }

                    SetPropertyValue(source, name, value, Context.RootElement, node, Context, node);
                    return;
                }

                xpe = xpe ?? new XamlParseException($"Can not set the content of {((IElementNode)parentNode).XmlType.Name} as it doesn't have a ContentPropertyAttribute", node);
                if (Context.ExceptionHandler != null)
                {
                    Context.ExceptionHandler(xpe);
                }
                throw xpe;
            }
            else if (IsCollectionItem(node, parentNode) && parentNode is ListNode)
            {
                var source = Values[parentNode.Parent];
                ProvideValue(ref value, node, source, XmlName.Empty);
                var parentList = (ListNode)parentNode;
                if (Skips.Contains(parentList.XmlName))
                {
                    return;
                }
                Exception xpe  = null;
                var       xKey = node.Properties.ContainsKey(XmlName.xKey) ? ((ValueNode)node.Properties[XmlName.xKey]).Value as string : null;

                object _;
                var    collection = GetPropertyValue(source, parentList.XmlName, Context, parentList, out _) as IEnumerable;
                if (collection == null)
                {
                    xpe = new XamlParseException($"Property {parentList.XmlName.LocalName} is null or is not IEnumerable", node);
                }

                if (xpe == null && TryAddToResourceDictionary(collection as ResourceDictionary, value, xKey, node, out xpe))
                {
                    return;
                }

                MethodInfo addMethod;
                if (xpe == null && (addMethod = collection.GetType().GetRuntimeMethods().First(mi => mi.Name == "Add" && mi.GetParameters().Length == 1)) != null)
                {
                    addMethod.Invoke(collection, new[] { Values[node] });
                    return;
                }
                xpe = xpe ?? new XamlParseException($"Value of {parentList.XmlName.LocalName} does not have a Add() method", node);
                if (Context.ExceptionHandler != null)
                {
                    Context.ExceptionHandler(xpe);
                }
                else
                {
                    throw xpe;
                }
            }
        }
Exemple #2
0
        public static Type GetElementType(XmlType xmlType, IXmlLineInfo xmlInfo, Assembly currentAssembly,
                                          out XamlParseException exception)
        {
            if (s_xmlnsDefinitions == null)
            {
                GatherXmlnsDefinitionAttributes();
            }

            var namespaceURI  = xmlType.NamespaceUri;
            var elementName   = xmlType.Name;
            var typeArguments = xmlType.TypeArguments;

            exception = null;

            var lookupAssemblies = new List <XmlnsDefinitionAttribute>();
            var lookupNames      = new List <string>();

            foreach (var xmlnsDef in s_xmlnsDefinitions)
            {
                if (xmlnsDef.XmlNamespace != namespaceURI)
                {
                    continue;
                }
                lookupAssemblies.Add(xmlnsDef);
            }

            if (lookupAssemblies.Count == 0)
            {
                string ns, asmstring, _;
                XmlnsHelper.ParseXmlns(namespaceURI, out _, out ns, out asmstring, out _);
                lookupAssemblies.Add(new XmlnsDefinitionAttribute(namespaceURI, ns)
                {
                    AssemblyName = asmstring ?? currentAssembly.FullName
                });
            }

            lookupNames.Add(elementName);
            lookupNames.Add(elementName + "Extension");

            for (var i = 0; i < lookupNames.Count; i++)
            {
                var name = lookupNames[i];
                if (name.Contains(":"))
                {
                    name = name.Substring(name.LastIndexOf(':') + 1);
                }
                if (typeArguments != null)
                {
                    name += "`" + typeArguments.Count;                     //this will return an open generic Type
                }
                lookupNames[i] = name;
            }

            Type type = null;

            foreach (var asm in lookupAssemblies)
            {
                foreach (var name in lookupNames)
                {
                    if ((type = Type.GetType($"{asm.ClrNamespace}.{name}, {asm.AssemblyName}")) != null)
                    {
                        break;
                    }
                }
                if (type != null)
                {
                    break;
                }
            }

            if (type == null)
            {
                List <Tuple <string, Assembly> > lookupAssemblies2 = new List <Tuple <string, Assembly> >();
                if (namespaceURI == NUI2018Uri)
                {
                    // Got the type of Tizen.NUI wiedget here, then CreateValueVisitor will create the instance of Tizen.NUI widget
                    lookupAssemblies2.Add(new Tuple <string, Assembly>("Tizen.NUI", typeof(Tizen.NUI.BaseComponents.View).GetTypeInfo().Assembly));
                    lookupAssemblies2.Add(new Tuple <string, Assembly>("Tizen.NUI.BaseComponents", typeof(Tizen.NUI.BaseComponents.View).GetTypeInfo().Assembly));
                    lookupAssemblies2.Add(new Tuple <string, Assembly>("Tizen.NUI.UIComponents", typeof(Tizen.NUI.BaseComponents.View).GetTypeInfo().Assembly));
                    lookupAssemblies2.Add(new Tuple <string, Assembly>("Tizen.NUI.Xaml", typeof(XamlLoader).GetTypeInfo().Assembly));
                    lookupAssemblies2.Add(new Tuple <string, Assembly>("Tizen.NUI.Binding", typeof(Tizen.NUI.BaseComponents.View).GetTypeInfo().Assembly));
                }
                else if (namespaceURI == X2009Uri || namespaceURI == X2006Uri)
                {
                    lookupAssemblies2.Add(new Tuple <string, Assembly>("Tizen.NUI.Xaml", typeof(XamlLoader).GetTypeInfo().Assembly));
                    lookupAssemblies2.Add(new Tuple <string, Assembly>("System", typeof(object).GetTypeInfo().Assembly));
                    lookupAssemblies2.Add(new Tuple <string, Assembly>("System", typeof(Uri).GetTypeInfo().Assembly)); //System.dll
                }
                else
                {
                    string   ns;
                    string   typename;
                    string   asmstring;
                    Assembly asm;
                    XmlnsHelper.ParseXmlns(namespaceURI, out typename, out ns, out asmstring, out _);
                    asm = asmstring == null ? currentAssembly : Assembly.Load(new AssemblyName(asmstring));
                    lookupAssemblies2.Add(new Tuple <string, Assembly>(ns, asm));
                }

                foreach (var asm in lookupAssemblies2)
                {
                    if (type != null)
                    {
                        break;
                    }
                    foreach (var name in lookupNames)
                    {
                        if (type != null)
                        {
                            break;
                        }
                        type = asm.Item2.GetType(asm.Item1 + "." + name);
                    }
                }
            }

            if (type != null && typeArguments != null)
            {
                XamlParseException innerexception = null;
                var args = typeArguments.Select(delegate(XmlType xmltype)
                {
                    XamlParseException xpe;
                    var t = GetElementType(xmltype, xmlInfo, currentAssembly, out xpe);
                    if (xpe != null)
                    {
                        innerexception = xpe;
                        return(null);
                    }
                    return(t);
                }).ToArray();
                if (innerexception != null)
                {
                    exception = innerexception;
                    return(null);
                }
                type = type.MakeGenericType(args);
            }

            if (type == null)
            {
                exception = new XamlParseException($"Type {elementName} not found in xmlns {namespaceURI}", xmlInfo);
            }

            return(type);
        }
Exemple #3
0
        public static Type GetElementType(XmlType xmlType, IXmlLineInfo xmlInfo, Assembly currentAssembly,
                                          out XamlParseException exception)
        {
            if (s_xmlnsDefinitions == null)
            {
                GatherXmlnsDefinitionAttributes();
            }

            var namespaceURI  = xmlType.NamespaceUri;
            var elementName   = xmlType.Name;
            var typeArguments = xmlType.TypeArguments;

            exception = null;

            var lookupAssemblies = new List <XmlnsDefinitionAttribute>();
            var lookupNames      = new List <string>();

            foreach (var xmlnsDef in s_xmlnsDefinitions)
            {
                if (xmlnsDef.XmlNamespace != namespaceURI)
                {
                    continue;
                }
                lookupAssemblies.Add(xmlnsDef);
            }

            if (lookupAssemblies.Count == 0)
            {
                string ns, asmstring, _;
                XmlnsHelper.ParseXmlns(namespaceURI, out _, out ns, out asmstring);
                lookupAssemblies.Add(new XmlnsDefinitionAttribute(namespaceURI, ns)
                {
                    AssemblyName = asmstring ?? currentAssembly.FullName
                });
            }

            lookupNames.Add(elementName);
            lookupNames.Add(elementName + "Extension");

            for (var i = 0; i < lookupNames.Count; i++)
            {
                var name = lookupNames[i];
                if (name.Contains(":"))
                {
                    name = name.Substring(name.LastIndexOf(':') + 1);
                }
                if (typeArguments != null)
                {
                    name += "`" + typeArguments.Count; //this will return an open generic Type
                }
                lookupNames[i] = name;
            }

            Type type = null;

            foreach (var asm in lookupAssemblies)
            {
                foreach (var name in lookupNames)
                {
                    if ((type = Type.GetType($"{asm.ClrNamespace}.{name}, {asm.AssemblyName}")) != null)
                    {
                        break;
                    }

                    if ('?' == name.Last())
                    {
                        string nameOfNotNull = name.Substring(0, name.Length - 1);
                        Type   typeofNotNull = Type.GetType($"{asm.ClrNamespace}.{nameOfNotNull}, {asm.AssemblyName}");

                        if (null != typeofNotNull)
                        {
                            type = typeof(Nullable <>).MakeGenericType(new Type[] { typeofNotNull });
                            break;
                        }
                    }
                }

                if (type != null)
                {
                    break;
                }
            }

            if (type != null && typeArguments != null)
            {
                XamlParseException innerexception = null;
                var args = typeArguments.Select(delegate(XmlType xmltype)
                {
                    XamlParseException xpe;
                    var t = GetElementType(xmltype, xmlInfo, currentAssembly, out xpe);
                    if (xpe != null)
                    {
                        innerexception = xpe;
                        return(null);
                    }
                    return(t);
                }).ToArray();
                if (innerexception != null)
                {
                    exception = innerexception;
                    return(null);
                }
                type = type.MakeGenericType(args);
            }

            if (type == null)
            {
                var message = $"Type {elementName} not found in xmlns {namespaceURI}\n";
                message  += "\n  - Make sure the all used assemblies (e.g. Tizen.NUI.Components) are included in the application project.";
                message  += "\n  - Make sure the type and namespace are correct.\n";
                exception = new XamlParseException($"message", xmlInfo);
            }

            return(type);
        }
Exemple #4
0
        public static Type GetElementType(XmlType xmlType, IXmlLineInfo xmlInfo, Assembly currentAssembly,
                                          out XamlParseException exception)
        {
            if (s_xmlnsDefinitions == null)
            {
                GatherXmlnsDefinitionAttributes();
            }

            var namespaceURI  = xmlType.NamespaceUri;
            var elementName   = xmlType.Name;
            var typeArguments = xmlType.TypeArguments;

            exception = null;

            var lookupAssemblies = new List <XmlnsDefinitionAttribute>();
            var lookupNames      = new List <string>();

            foreach (var xmlnsDef in s_xmlnsDefinitions)
            {
                if (xmlnsDef.XmlNamespace != namespaceURI)
                {
                    continue;
                }
                lookupAssemblies.Add(xmlnsDef);
            }

            if (lookupAssemblies.Count == 0)
            {
                string ns, asmstring, _;
                XmlnsHelper.ParseXmlns(namespaceURI, out _, out ns, out asmstring, out _);
                lookupAssemblies.Add(new XmlnsDefinitionAttribute(namespaceURI, ns)
                {
                    AssemblyName = asmstring ?? currentAssembly.FullName
                });
            }

            lookupNames.Add(elementName);
            lookupNames.Add(elementName + "Extension");

            for (var i = 0; i < lookupNames.Count; i++)
            {
                var name = lookupNames[i];
                if (name.Contains(":"))
                {
                    name = name.Substring(name.LastIndexOf(':') + 1);
                }
                if (typeArguments != null)
                {
                    name += "`" + typeArguments.Count; //this will return an open generic Type
                }
                lookupNames[i] = name;
            }

            Type type = null;

            foreach (var asm in lookupAssemblies)
            {
                foreach (var name in lookupNames)
                {
                    if ((type = Type.GetType($"{asm.ClrNamespace}.{name}, {asm.AssemblyName}")) != null)
                    {
                        break;
                    }
                }
                if (type != null)
                {
                    break;
                }
            }

            if (type != null && typeArguments != null)
            {
                XamlParseException innerexception = null;
                var args = typeArguments.Select(delegate(XmlType xmltype)
                {
                    XamlParseException xpe;
                    var t = GetElementType(xmltype, xmlInfo, currentAssembly, out xpe);
                    if (xpe != null)
                    {
                        innerexception = xpe;
                        return(null);
                    }
                    return(t);
                }).ToArray();
                if (innerexception != null)
                {
                    exception = innerexception;
                    return(null);
                }
                type = type.MakeGenericType(args);
            }

            if (type == null)
            {
                exception = new XamlParseException($"Type {elementName} not found in xmlns {namespaceURI}", xmlInfo);
            }

            return(type);
        }