1 using System.Collections.Generic;
5 using Mono.Cecil.Rocks;
6 using Tizen.NUI.Binding;
9 namespace Tizen.NUI.Xaml.Build.Tasks
11 static class XmlTypeExtensions
13 static internal IList<XmlnsDefinitionAttribute> s_xmlnsDefinitions;
15 static void GatherXmlnsDefinitionAttributes()
17 //this could be extended to look for [XmlnsDefinition] in all assemblies
18 s_xmlnsDefinitions = XamlTask.s_xmlnsDefinitions.OrderByDescending(a => a.Level).ToList(); ;
21 public static TypeReference GetTypeReference(string xmlType, ModuleDefinition module, BaseNode node)
23 var split = xmlType.Split(':');
25 throw new XamlParseException($"Type \"{xmlType}\" is invalid", node as IXmlLineInfo);
28 if (split.Length == 2) {
35 var namespaceuri = node.NamespaceResolver.LookupNamespace(prefix) ?? "";
36 return GetTypeReference(new XmlType(namespaceuri, name, null), module, node as IXmlLineInfo);
39 public static TypeReference GetTypeReference(string namespaceURI, string typename, ModuleDefinition module, IXmlLineInfo xmlInfo)
41 return new XmlType(namespaceURI, typename, null).GetTypeReference(module, xmlInfo);
44 public static TypeReference GetTypeReference(this XmlType xmlType, ModuleDefinition module, IXmlLineInfo xmlInfo, bool fromAllAssembly = false)
46 if (s_xmlnsDefinitions == null)
47 GatherXmlnsDefinitionAttributes();
49 var namespaceURI = xmlType.NamespaceUri;
50 var elementName = xmlType.Name;
51 var typeArguments = xmlType.TypeArguments;
53 if (elementName.Contains("-"))
55 elementName = elementName.Replace('-', '+');
58 var lookupAssemblies = new List<XmlnsDefinitionAttribute>();
60 var lookupNames = new List<string>();
62 if (true == fromAllAssembly)
64 foreach (var xmlnsDef in s_xmlnsDefinitions)
66 lookupAssemblies.Add(xmlnsDef);
71 foreach (var xmlnsDef in s_xmlnsDefinitions)
73 if (xmlnsDef.XmlNamespace != namespaceURI)
75 lookupAssemblies.Add(xmlnsDef);
79 if (lookupAssemblies.Count == 0) {
83 string targetPlatform;
85 XmlnsHelper.ParseXmlns(namespaceURI, out typename, out ns, out asmstring, out targetPlatform);
86 asmstring = asmstring ?? module.Assembly.Name.Name;
88 lookupAssemblies.Add(new XmlnsDefinitionAttribute(namespaceURI, ns, 0) {
89 AssemblyName = asmstring
93 lookupNames.Add(elementName);
94 lookupNames.Add(elementName + "Extension");
96 for (var i = 0; i < lookupNames.Count; i++)
98 var name = lookupNames[i];
99 if (name.Contains(":"))
100 name = name.Substring(name.LastIndexOf(':') + 1);
101 if (typeArguments != null)
102 name += "`" + typeArguments.Count; //this will return an open generic Type
103 lookupNames[i] = name;
106 TypeReference type = null;
107 foreach (var asm in lookupAssemblies)
111 foreach (var name in lookupNames)
116 var clrNamespace = asm.ClrNamespace;
117 var typeName = name.Replace('+', '/'); //Nested types
118 var idx = typeName.LastIndexOf('.');
120 clrNamespace += '.' + typeName.Substring(0, typeName.LastIndexOf('.'));
121 typeName = typeName.Substring(typeName.LastIndexOf('.') + 1);
123 type = module.GetTypeDefinition((asm.AssemblyName, clrNamespace, typeName));
127 if (type != null && typeArguments != null && type.HasGenericParameters)
130 module.ImportReference(type)
131 .MakeGenericInstanceType(typeArguments.Select(x => GetTypeReference(x, module, xmlInfo)).ToArray());
135 throw new XamlParseException(string.Format("Type {0} not found in xmlns {1}", elementName, namespaceURI), xmlInfo);
137 return module.ImportReference(type);
140 public static TypeReference GetTypeExtensionReference(this XmlType xmlType, ModuleDefinition module, IXmlLineInfo xmlInfo, bool fromAllAssembly = false)
142 if (s_xmlnsDefinitions == null)
143 GatherXmlnsDefinitionAttributes();
145 var namespaceURI = xmlType.NamespaceUri;
146 var elementName = xmlType.Name;
147 var typeArguments = xmlType.TypeArguments;
149 if (elementName.Contains("-"))
151 elementName = elementName.Replace('-', '+');
154 var lookupAssemblies = new List<XmlnsDefinitionAttribute>();
156 var lookupNames = new List<string>();
158 if (true == fromAllAssembly)
160 foreach (var xmlnsDef in s_xmlnsDefinitions)
162 lookupAssemblies.Add(xmlnsDef);
167 foreach (var xmlnsDef in s_xmlnsDefinitions)
169 if (xmlnsDef.XmlNamespace != namespaceURI)
171 lookupAssemblies.Add(xmlnsDef);
175 if (lookupAssemblies.Count == 0)
180 string targetPlatform;
182 XmlnsHelper.ParseXmlns(namespaceURI, out typename, out ns, out asmstring, out targetPlatform);
183 asmstring = asmstring ?? module.Assembly.Name.Name;
185 lookupAssemblies.Add(new XmlnsDefinitionAttribute(namespaceURI, ns, 0)
187 AssemblyName = asmstring
191 lookupNames.Add(elementName + "Extension");
193 for (var i = 0; i < lookupNames.Count; i++)
195 var name = lookupNames[i];
196 if (name.Contains(":"))
197 name = name.Substring(name.LastIndexOf(':') + 1);
198 if (typeArguments != null)
199 name += "`" + typeArguments.Count; //this will return an open generic Type
200 lookupNames[i] = name;
203 TypeReference type = null;
204 foreach (var asm in lookupAssemblies)
208 foreach (var name in lookupNames)
213 var clrNamespace = asm.ClrNamespace;
214 var typeName = name.Replace('+', '/'); //Nested types
215 var idx = typeName.LastIndexOf('.');
218 clrNamespace += '.' + typeName.Substring(0, typeName.LastIndexOf('.'));
219 typeName = typeName.Substring(typeName.LastIndexOf('.') + 1);
221 type = module.GetTypeDefinition((asm.AssemblyName, clrNamespace, typeName));
225 type = module.GetTypeDefinition((module.Assembly.Name.Name, clrNamespace, typeName));
230 if (type != null && typeArguments != null && type.HasGenericParameters)
233 module.ImportReference(type)
234 .MakeGenericInstanceType(typeArguments.Select(x => GetTypeReference(x, module, xmlInfo)).ToArray());
238 throw new XamlParseException(string.Format("Type {0} not found in xmlns {1}", elementName, namespaceURI), xmlInfo);
240 return module.ImportReference(type);