IEnumerable <CodeMemberField> GetCodeMemberFields(XmlNode root, XmlNamespaceManager nsmgr) { var xPrefix = nsmgr.LookupPrefix(XamlParser.X2006Uri) ?? nsmgr.LookupPrefix(XamlParser.X2009Uri); if (xPrefix == null) { yield break; } XmlNodeList names = root.SelectNodes( "//*[@" + xPrefix + ":Name" + "][not(ancestor:: __f__:DataTemplate) and not(ancestor:: __f__:ControlTemplate) and not(ancestor:: __f__:Style) and not(ancestor:: __f__:VisualStateManager.VisualStateGroups)]", nsmgr); foreach (XmlNode node in names) { var name = GetAttributeValue(node, "Name", XamlParser.X2006Uri, XamlParser.X2009Uri); var typeArguments = GetAttributeValue(node, "TypeArguments", XamlParser.X2006Uri, XamlParser.X2009Uri); var fieldModifier = GetAttributeValue(node, "FieldModifier", XamlParser.X2006Uri, XamlParser.X2009Uri); var xmlType = new XmlType(node.NamespaceURI, node.LocalName, typeArguments != null ? TypeArgumentsParser.ParseExpression(typeArguments, nsmgr, null) : null); var access = MemberAttributes.Private; if (fieldModifier != null) { switch (fieldModifier.ToLowerInvariant()) { default: case "private": access = MemberAttributes.Private; break; case "public": access = MemberAttributes.Public; break; case "protected": access = MemberAttributes.Family; break; case "internal": case "notpublic": //WPF syntax access = MemberAttributes.Assembly; break; } } yield return(new CodeMemberField { Name = name, Type = GetType(xmlType, node.GetNamespaceOfPrefix), Attributes = access, CustomAttributes = { GeneratedCodeAttrDecl } }); } }
static Dictionary <string, CodeTypeReference> GetNamesAndTypes(XmlNode root, XmlNamespaceManager nsmgr) { var res = new Dictionary <string, CodeTypeReference>(); var xPrefix = nsmgr.LookupPrefix(XAML2006) ?? nsmgr.LookupPrefix(XAML2009); if (xPrefix == null) { return(null); } XmlNodeList names = root.SelectNodes( "//*[@" + xPrefix + ":Name" + "][not(ancestor:: __f__:DataTemplate) and not(ancestor:: __f__:ControlTemplate) and not(ancestor:: __f__:Style)]", nsmgr); foreach (XmlNode node in names) { // Don't take the root canvas if (node == root) { continue; } XmlAttribute attr = node.Attributes["Name", XAML2006] ?? node.Attributes["Name", XAML2009]; XmlAttribute typeArgsAttr = node.Attributes["TypeArguments", XAML2006] ?? node.Attributes["TypeArguments", XAML2009]; var xmlType = new XmlType(node.NamespaceURI, node.LocalName, typeArgsAttr != null ? TypeArgumentsParser.ParseExpression(typeArgsAttr.Value, nsmgr, null) : null); res[attr.Value] = GetType(xmlType, node.GetNamespaceOfPrefix); } return(res); }
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); }
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); }
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); }