internal static void ParseXaml(TextReader xaml, out string rootType, out string rootNs, out CodeTypeReference baseType, out IDictionary <string, CodeTypeReference> namesAndTypes) { var xmlDoc = new XmlDocument(); xmlDoc.Load(xaml); var nsmgr = new XmlNamespaceManager(xmlDoc.NameTable); nsmgr.AddNamespace("__f__", "http://xamarin.com/schemas/2014/forms"); var root = xmlDoc.SelectSingleNode("/*", nsmgr); if (root == null) { Console.Error.WriteLine("{0}: No root node found"); rootType = null; rootNs = null; baseType = null; namesAndTypes = null; return; } foreach (XmlAttribute attr in root.Attributes) { if (attr.Name == "xmlns") { nsmgr.AddNamespace("", attr.Value); //Add default xmlns } if (attr.Prefix != "xmlns") { continue; } nsmgr.AddNamespace(attr.LocalName, attr.Value); } var rootClass = root.Attributes["Class", XAML2006] ?? root.Attributes["Class", XAML2009]; if (rootClass == null) { rootType = null; rootNs = null; baseType = null; namesAndTypes = null; return; } string rootAsm, targetPlatform; XmlnsHelper.ParseXmlns(rootClass.Value, out rootType, out rootNs, out rootAsm, out targetPlatform); namesAndTypes = GetNamesAndTypes(root, nsmgr); var typeArgsAttr = root.Attributes["TypeArguments", XAML2006] ?? root.Attributes["TypeArguments", XAML2009]; var xmlType = new XmlType(root.NamespaceURI, root.LocalName, typeArgsAttr != null ? TypeArgumentsParser.ParseExpression(typeArgsAttr.Value, nsmgr, null): null); baseType = GetType(xmlType, root.GetNamespaceOfPrefix); }
static string GetClrNamespace(string namespaceuri, string className) { XmlnsInfo xmlnsInfo = null; xmlnsNameToInfo.TryGetValue(namespaceuri, out xmlnsInfo); if (null != xmlnsInfo) { string nameSpace = null; xmlnsInfo.classNameToNameSpace.TryGetValue(className, out nameSpace); if (null != nameSpace) { return(nameSpace); } } if (namespaceuri == "http://tizen.org/Tizen.NUI/2018/XAML") { return("Tizen.NUI.Xaml"); } if (namespaceuri == XamlParser.XFUri) { return("Tizen.NUI.Xaml"); } if (namespaceuri == XamlParser.X2009Uri) { return("System"); } //if (namespaceuri != XamlParser.X2006Uri && !namespaceuri.StartsWith("clr-namespace", StringComparison.InvariantCulture) && !namespaceuri.StartsWith("using", StringComparison.InvariantCulture)) // throw new Exception($"Can't load types from xmlns {namespaceuri}"); return(XmlnsHelper.ParseNamespaceFromXmlns(namespaceuri)); }
internal static void ParseXaml(TextReader xaml, out string rootType, out string rootNs, out CodeTypeReference baseType, out IEnumerable <CodeMemberField> namedFields) { var xmlDoc = new XmlDocument(); xmlDoc.Load(xaml); var nsmgr = new XmlNamespaceManager(xmlDoc.NameTable); nsmgr.AddNamespace("__f__", XamlParser.XFUri); var root = xmlDoc.SelectSingleNode("/*", nsmgr); if (root == null) { Console.Error.WriteLine("{0}: No root node found"); rootType = null; rootNs = null; baseType = null; namedFields = null; return; } foreach (XmlAttribute attr in root.Attributes) { if (attr.Name == "xmlns") { nsmgr.AddNamespace("", attr.Value); //Add default xmlns } if (attr.Prefix != "xmlns") { continue; } nsmgr.AddNamespace(attr.LocalName, attr.Value); } var rootClass = root.Attributes["Class", XamlParser.X2006Uri] ?? root.Attributes["Class", XamlParser.X2009Uri]; if (rootClass == null) { rootType = null; rootNs = null; baseType = null; namedFields = null; return; } string rootAsm, targetPlatform; XmlnsHelper.ParseXmlns(rootClass.Value, out rootType, out rootNs, out rootAsm, out targetPlatform); namedFields = GetCodeMemberFields(root, nsmgr); var typeArguments = GetAttributeValue(root, "TypeArguments", XamlParser.X2006Uri, XamlParser.X2009Uri); var xmlType = new XmlType(root.NamespaceURI, root.LocalName, typeArguments != null ? TypeArgumentsParser.ParseExpression(typeArguments, nsmgr, null): null); baseType = GetType(xmlType, root.GetNamespaceOfPrefix); }
static string GetClrNamespace(string namespaceuri) { if (namespaceuri == XamlParser.X2009Uri) { return("System"); } if (namespaceuri != XamlParser.X2006Uri && !namespaceuri.StartsWith("clr-namespace", StringComparison.InvariantCulture) && !namespaceuri.StartsWith("using:", StringComparison.InvariantCulture)) { return(null); } return(XmlnsHelper.ParseNamespaceFromXmlns(namespaceuri)); }
static string GetNamespace(string namespaceuri) { if (!XmlnsHelper.IsCustom(namespaceuri)) { return("Xamarin.Forms"); } if (namespaceuri == "http://schemas.microsoft.com/winfx/2009/xaml") { return("System"); } if (namespaceuri != "http://schemas.microsoft.com/winfx/2006/xaml" && !namespaceuri.Contains("clr-namespace")) { throw new Exception(String.Format("Can't load types from xmlns {0}", namespaceuri)); } return(XmlnsHelper.ParseNamespaceFromXmlns(namespaceuri)); }
static string GetClrNamespace(string namespaceuri) { if (namespaceuri == XamlParser.XFUri) { return("Xamarin.Forms"); } if (namespaceuri == XamlParser.X2009Uri) { return("System"); } if (namespaceuri != XamlParser.X2006Uri && !namespaceuri.Contains("clr-namespace")) { throw new Exception($"Can't load types from xmlns {namespaceuri}"); } return(XmlnsHelper.ParseNamespaceFromXmlns(namespaceuri)); }
static string GetClrNamespace(string namespaceuri) { if (namespaceuri == "http://xamarin.com/schemas/2014/forms") { return("Xamarin.Forms"); } if (namespaceuri == XAML2009) { return("System"); } if (namespaceuri != XAML2006 && !namespaceuri.Contains("clr-namespace")) { throw new Exception($"Can't load types from xmlns {namespaceuri}"); } return(XmlnsHelper.ParseNamespaceFromXmlns(namespaceuri)); }
static string GetClrNamespace(string namespaceuri) { if (namespaceuri == XamlParser.XFUri) { return("Xamarin.Forms"); } if (namespaceuri == XamlParser.X2009Uri) { return("System"); } if (namespaceuri != XamlParser.X2006Uri && !namespaceuri.StartsWith("clr-namespace", StringComparison.InvariantCulture) && !namespaceuri.StartsWith("using", StringComparison.InvariantCulture)) { throw new Exception($"Can't load types from xmlns {namespaceuri}"); } return(XmlnsHelper.ParseNamespaceFromXmlns(namespaceuri)); }
static string GetClrNamespace(string namespaceuri) { if (namespaceuri == XamlParser.XFUri) { return("Xamarin.Forms"); } if (namespaceuri == XamlParser.X2009Uri) { return("System"); } if (namespaceuri != XamlParser.X2006Uri && !namespaceuri.Contains("clr-namespace")) { return(null); } return(XmlnsHelper.ParseNamespaceFromXmlns(namespaceuri)); }
internal static void ParseXaml(TextReader xaml, out string rootType, out string rootNs, out CodeTypeReference baseType, out IDictionary <string, CodeTypeReference> namesAndTypes) { var xmlDoc = new XmlDocument(); xmlDoc.Load(xaml); var nsmgr = new XmlNamespaceManager(xmlDoc.NameTable); nsmgr.AddNamespace("x", "http://schemas.microsoft.com/winfx/2006/xaml"); nsmgr.AddNamespace("x2009", "http://schemas.microsoft.com/winfx/2009/xaml"); nsmgr.AddNamespace("f", "http://xamarin.com/schemas/2014/forms"); var root = xmlDoc.SelectSingleNode("/*", nsmgr); if (root == null) { Console.Error.WriteLine("{0}: No root node found"); rootType = null; rootNs = null; baseType = null; namesAndTypes = null; return; } var rootClass = root.Attributes["Class", "http://schemas.microsoft.com/winfx/2006/xaml"] ?? root.Attributes["Class", "http://schemas.microsoft.com/winfx/2009/xaml"]; if (rootClass == null) { rootType = null; rootNs = null; baseType = null; namesAndTypes = null; return; } string rootAsm, targetPlatform; XmlnsHelper.ParseXmlns(rootClass.Value, out rootType, out rootNs, out rootAsm, out targetPlatform); namesAndTypes = GetNamesAndTypes(root, nsmgr); var typeArguments = root.Attributes["TypeArguments", "http://schemas.microsoft.com/winfx/2009/xaml"]; baseType = GetType(root.NamespaceURI, root.LocalName, typeArguments == null ? null : typeArguments.Value.Split(','), root.GetNamespaceOfPrefix); }
internal bool ParseXaml(TextReader xaml) { var xmlDoc = new XmlDocument(); xmlDoc.Load(xaml); // if the following xml processing instruction is present // // <?xaml-comp compile="true" ?> // // we will generate a xaml.g.cs file with the default ctor calling InitializeComponent, and a XamlCompilation attribute var hasXamlCompilationProcessingInstruction = GetXamlCompilationProcessingInstruction(xmlDoc); var nsmgr = new XmlNamespaceManager(xmlDoc.NameTable); nsmgr.AddNamespace("__f__", XamlParser.XFUri); var root = xmlDoc.SelectSingleNode("/*", nsmgr); if (root == null) { Logger?.LogMessage(MessageImportance.Low, " No root node found"); return(false); } foreach (XmlAttribute attr in root.Attributes) { if (attr.Name == "xmlns") { nsmgr.AddNamespace("", attr.Value); //Add default xmlns } if (attr.Prefix != "xmlns") { continue; } nsmgr.AddNamespace(attr.LocalName, attr.Value); } var rootClass = root.Attributes["Class", XamlParser.X2006Uri] ?? root.Attributes["Class", XamlParser.X2009Uri]; if (rootClass != null) { string rootType, rootNs, rootAsm, targetPlatform; XmlnsHelper.ParseXmlns(rootClass.Value, out rootType, out rootNs, out rootAsm, out targetPlatform); RootType = rootType; RootClrNamespace = rootNs; } else if (hasXamlCompilationProcessingInstruction) { RootClrNamespace = "__XamlGeneratedCode__"; RootType = $"__Type{Guid.NewGuid().ToString("N")}"; GenerateDefaultCtor = true; AddXamlCompilationAttribute = true; HideFromIntellisense = true; } else // rootClass == null && !hasXamlCompilationProcessingInstruction) { { XamlResourceIdOnly = true; //only generate the XamlResourceId assembly attribute return(true); } NamedFields = GetCodeMemberFields(root, nsmgr); var typeArguments = GetAttributeValue(root, "TypeArguments", XamlParser.X2006Uri, XamlParser.X2009Uri); var xmlType = new XmlType(root.NamespaceURI, root.LocalName, typeArguments != null ? TypeArgumentsParser.ParseExpression(typeArguments, nsmgr, null) : null); BaseType = GetType(xmlType, root.GetNamespaceOfPrefix); return(true); }
public static TypeReference GetTypeReference(this XmlType xmlType, ModuleDefinition module, IXmlLineInfo xmlInfo) { if (s_xmlnsDefinitions == null) { GatherXmlnsDefinitionAttributes(); } var namespaceURI = xmlType.NamespaceUri; var elementName = xmlType.Name; var typeArguments = xmlType.TypeArguments; 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; string typename; string asmstring; string targetPlatform; XmlnsHelper.ParseXmlns(namespaceURI, out typename, out ns, out asmstring, out targetPlatform); asmstring = asmstring ?? module.Assembly.Name.Name; lookupAssemblies.Add(new XmlnsDefinitionAttribute(namespaceURI, ns) { AssemblyName = asmstring }); } 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; } TypeReference type = null; foreach (var asm in lookupAssemblies) { if (type != null) { break; } foreach (var name in lookupNames) { if (type != null) { break; } var clrNamespace = asm.ClrNamespace; var typeName = name.Replace('+', '/'); //Nested types var idx = typeName.LastIndexOf('.'); if (idx >= 0) { clrNamespace += '.' + typeName.Substring(0, typeName.LastIndexOf('.')); typeName = typeName.Substring(typeName.LastIndexOf('.') + 1); } var assemblydefinition = module.Assembly.Name.Name == asm.AssemblyName ? module.Assembly : module.AssemblyResolver.Resolve(AssemblyNameReference.Parse(asm.AssemblyName)); type = assemblydefinition.MainModule.GetType(clrNamespace + "." + typeName); if (type == null) { var exportedtype = assemblydefinition.MainModule.ExportedTypes.FirstOrDefault( (ExportedType arg) => arg.IsForwarder && arg.Namespace == clrNamespace && arg.Name == typeName); if (exportedtype != null) { type = exportedtype.Resolve(); } } } } if (type != null && typeArguments != null && type.HasGenericParameters) { type = module.ImportReference(type) .MakeGenericInstanceType(typeArguments.Select(x => GetTypeReference(x, module, xmlInfo)).ToArray()); } if (type == null) { throw new XamlParseException(string.Format("Type {0} not found in xmlns {1}", elementName, namespaceURI), xmlInfo); } return(module.ImportReference(type)); }
public static TypeReference GetTypeReference(this XmlType xmlType, ModuleDefinition module, IXmlLineInfo xmlInfo) { var namespaceURI = xmlType.NamespaceUri; var elementName = xmlType.Name; var typeArguments = xmlType.TypeArguments; List <Tuple <string, string> > lookupAssemblies = new List <Tuple <string, string> >(); //assembly, namespace List <string> lookupNames = new List <string>(); if (!XmlnsHelper.IsCustom(namespaceURI)) { lookupAssemblies.Add(new Tuple <string, string>("Xamarin.Forms.Core", "Xamarin.Forms")); lookupAssemblies.Add(new Tuple <string, string>("Xamarin.Forms.Xaml", "Xamarin.Forms.Xaml")); } else if (namespaceURI == "http://schemas.microsoft.com/winfx/2009/xaml" || namespaceURI == "http://schemas.microsoft.com/winfx/2006/xaml") { lookupAssemblies.Add(new Tuple <string, string>("Xamarin.Forms.Xaml", "Xamarin.Forms.Xaml")); lookupAssemblies.Add(new Tuple <string, string>("mscorlib", "System")); lookupAssemblies.Add(new Tuple <string, string>("System", "System")); } else { string ns; string typename; string asmstring; string targetPlatform; XmlnsHelper.ParseXmlns(namespaceURI, out typename, out ns, out asmstring, out targetPlatform); asmstring = asmstring ?? module.Assembly.Name.Name; lookupAssemblies.Add(new Tuple <string, string>(asmstring, ns)); } lookupNames.Add(elementName); if (namespaceURI == "http://schemas.microsoft.com/winfx/2009/xaml") { 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; } TypeReference type = null; foreach (var asm in lookupAssemblies) { if (type != null) { break; } foreach (var name in lookupNames) { if (type != null) { break; } var assemblydefinition = module.Assembly.Name.Name == asm.Item1 ? module.Assembly : module.AssemblyResolver.Resolve(asm.Item1); type = assemblydefinition.MainModule.GetType(asm.Item2, name); if (type == null) { var exportedtype = assemblydefinition.MainModule.ExportedTypes.FirstOrDefault( (ExportedType arg) => arg.IsForwarder && arg.Namespace == asm.Item2 && arg.Name == name); if (exportedtype != null) { type = exportedtype.Resolve(); } } } } if (type != null && typeArguments != null && type.HasGenericParameters) { type = module.Import(type) .MakeGenericInstanceType(typeArguments.Select(x => GetTypeReference(x, module, xmlInfo)).ToArray()); } if (type == null) { throw new XamlParseException(string.Format("Type {0} not found in xmlns {1}", elementName, namespaceURI), xmlInfo); } return(module.Import(type)); }
public static TypeReference GetTypeReference(this XmlType xmlType, ModeOfGetType mode, ModuleDefinition module, IXmlLineInfo xmlInfo, bool fromAllAssembly = false) { if (s_xmlnsDefinitions == null) { GatherXmlnsDefinitionAttributes(); } var namespaceURI = xmlType.NamespaceUri; var elementName = xmlType.Name; var typeArguments = xmlType.TypeArguments; if (elementName.Contains("-")) { elementName = elementName.Replace('-', '+'); } var lookupAssemblies = new List <XmlnsDefinitionAttribute>(); var lookupNames = new List <string>(); if (true == fromAllAssembly) { foreach (var xmlnsDef in s_xmlnsDefinitions) { lookupAssemblies.Add(xmlnsDef); } } else { foreach (var xmlnsDef in s_xmlnsDefinitions) { if (xmlnsDef.XmlNamespace != namespaceURI) { continue; } lookupAssemblies.Add(xmlnsDef); } } if (lookupAssemblies.Count == 0) { string ns; string typename; string asmstring; string targetPlatform; XmlnsHelper.ParseXmlns(namespaceURI, out typename, out ns, out asmstring, out targetPlatform); asmstring = asmstring ?? module.Assembly.Name.Name; if (ns != null) { lookupAssemblies.Add(new XmlnsDefinitionAttribute(namespaceURI, ns, 0) { AssemblyName = asmstring }); } } switch (mode) { case ModeOfGetType.OnlyGetType: lookupNames.Add(elementName); break; case ModeOfGetType.OnlyGetTypeExtension: lookupNames.Add(elementName + "Extension"); break; case ModeOfGetType.Both: lookupNames.Add(elementName); lookupNames.Add(elementName + "Extension"); break; } 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; } TypeReference type = null; foreach (var asm in lookupAssemblies) { if (type != null) { break; } foreach (var name in lookupNames) { if (type != null) { break; } var clrNamespace = asm.ClrNamespace; var typeName = name.Replace('+', '/'); //Nested types var idx = typeName.LastIndexOf('.'); if (idx >= 0) { clrNamespace += '.' + typeName.Substring(0, typeName.LastIndexOf('.')); typeName = typeName.Substring(typeName.LastIndexOf('.') + 1); } type = module.GetTypeDefinition((asm.AssemblyName, clrNamespace, typeName)); } } if (type != null && typeArguments != null && type.HasGenericParameters) { type = module.ImportReference(type) .MakeGenericInstanceType(typeArguments.Select(x => GetTypeReference(x, mode, module, xmlInfo)).ToArray()); } if (type == null) { throw new XamlParseException(string.Format("Type {0} not found in xmlns {1}", elementName, namespaceURI), xmlInfo); } return(module.ImportReference(type)); }
public static TypeReference GetTypeReference(this XmlType xmlType, ModuleDefinition module, IXmlLineInfo xmlInfo) { IList <XmlnsDefinitionAttribute> xmlnsDefinitions = null; lock (_nsLock) { if (!s_xmlnsDefinitions.TryGetValue(module, out xmlnsDefinitions)) { xmlnsDefinitions = GatherXmlnsDefinitionAttributes(module); } } var namespaceURI = xmlType.NamespaceUri; var elementName = xmlType.Name; var typeArguments = xmlType.TypeArguments; var lookupAssemblies = new List <XmlnsDefinitionAttribute>(); var lookupNames = new List <string>(); foreach (var xmlnsDef in xmlnsDefinitions) { if (xmlnsDef.XmlNamespace != namespaceURI) { continue; } lookupAssemblies.Add(xmlnsDef); } if (lookupAssemblies.Count == 0) { string ns; string typename; string asmstring; string targetPlatform; XmlnsHelper.ParseXmlns(namespaceURI, out typename, out ns, out asmstring, out targetPlatform); asmstring = asmstring ?? module.Assembly.Name.Name; if (ns != null) { lookupAssemblies.Add(new XmlnsDefinitionAttribute(namespaceURI, ns) { AssemblyName = asmstring }); } } 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; } TypeReference type = null; foreach (var asm in lookupAssemblies) { if (type != null) { break; } foreach (var name in lookupNames) { if (type != null) { break; } var clrNamespace = asm.ClrNamespace; var typeName = name.Replace('+', '/'); //Nested types var idx = typeName.LastIndexOf('.'); if (idx >= 0) { clrNamespace += '.' + typeName.Substring(0, typeName.LastIndexOf('.')); typeName = typeName.Substring(typeName.LastIndexOf('.') + 1); } type = module.GetTypeDefinition((asm.AssemblyName, clrNamespace, typeName)); } } if (type != null && typeArguments != null && type.HasGenericParameters) { type = module.ImportReference(type) .MakeGenericInstanceType(typeArguments.Select(x => GetTypeReference(x, module, xmlInfo)).ToArray()); } if (type == null) { throw new XamlParseException(string.Format("Type {0} not found in xmlns {1}. Ensure third party control libraries are referenced in the code of your project and not just in XAML.", elementName, namespaceURI), xmlInfo); } return(module.ImportReference(type)); }