Ejemplo n.º 1
0
        static void GenerateCU(XmlParsedDocument doc)
        {
            if (doc.XDocument == null || doc.XDocument.RootElement == null)
            {
                doc.Add(new Error(ErrorType.Error, "No root node found.", 1, 1));
                return;
            }

            XAttribute rootClass = doc.XDocument.RootElement.Attributes [new XName("x", "Class")];

            if (rootClass == null)
            {
                doc.Add(new Error(ErrorType.Error, "Root node does not contain an x:Class attribute.", 1, 1));
                return;
            }

            bool isApplication = doc.XDocument.RootElement.Name.Name == "Application";

            string rootNamespace, rootType, rootAssembly;

            XamlG.ParseXmlns(rootClass.Value, out rootType, out rootNamespace, out rootAssembly);

            var cu = new DefaultParsedDocument(doc.FileName);

            DomRegion rootRegion = doc.XDocument.RootElement.Region;

            if (doc.XDocument.RootElement.IsClosed)
            {
                rootRegion = new DomRegion(doc.XDocument.RootElement.Region.FileName, doc.XDocument.RootElement.Region.Begin, doc.XDocument.RootElement.ClosingTag.Region.End);
            }

            var declType = new DefaultUnresolvedTypeDefinition(rootNamespace, rootType)
            {
                Kind          = TypeKind.Class,
                Accessibility = Accessibility.Public,
                Region        = rootRegion
            };

            cu.TopLevelTypeDefinitions.Add(declType);

            var initcomp = new DefaultUnresolvedMethod(declType, "InitializeComponent")
            {
                ReturnType    = KnownTypeReference.Void,
                Accessibility = Accessibility.Public
            };

            declType.Members.Add(initcomp);

            var _contentLoaded = new DefaultUnresolvedField(declType, "_contentLoaded")
            {
                ReturnType = KnownTypeReference.Boolean
            };

// was missing in the original code: correct ?
//			declType.Fields.Add (_contentLoaded);

            if (isApplication)
            {
                return;
            }

//			cu.Add (new DomUsing (DomRegion.Empty, "System"));
//			cu.Add (new DomUsing (DomRegion.Empty, "System.Windows"));
//			cu.Add (new DomUsing (DomRegion.Empty, "System.Windows.Controls"));
//			cu.Add (new DomUsing (DomRegion.Empty, "System.Windows.Documents"));
//			cu.Add (new DomUsing (DomRegion.Empty, "System.Windows.Input"));
//			cu.Add (new DomUsing (DomRegion.Empty, "System.Windows.Media"));
//			cu.Add (new DomUsing (DomRegion.Empty, "System.Windows.Media.Animation"));
//			cu.Add (new DomUsing (DomRegion.Empty, "System.Windows.Shapes"));
//			cu.Add (new DomUsing (DomRegion.Empty, "System.Windows.Controls.Primitives"));

            //		Dictionary<string,string> namespaceMap = new Dictionary<string, string> ();
            //		namespaceMap["x"] = "http://schemas.microsoft.com/winfx/2006/xaml";

            XName nameAtt = new XName("x", "Name");

            foreach (XElement el in doc.XDocument.RootElement.AllDescendentElements)
            {
                XAttribute name = el.Attributes [nameAtt];
                if (name != null && name.IsComplete)
                {
                    string type = ResolveType(el);
                    if (type == null || type.Length == 0)
                    {
                        cu.Add(new Error(ErrorType.Error, "Could not find namespace for '" + el.Name.FullName + "'.", el.Region.Begin));
                    }
                    else
                    {
                        declType.Members.Add(new DefaultUnresolvedField(declType, name.Value)
                        {
                            Accessibility = Accessibility.Internal,
                            Region        = el.Region,
                            ReturnType    = new DefaultUnresolvedTypeDefinition(type)
                        });
                    }
                }
            }
        }
Ejemplo n.º 2
0
        static void GenerateCU(XmlParsedDocument doc)
        {
            if (doc.XDocument == null || doc.XDocument.RootElement == null)
            {
                doc.Add(new Error(ErrorType.Error, 1, 1, "No root node found."));
                return;
            }

            XAttribute rootClass = doc.XDocument.RootElement.Attributes [new XName("x", "Class")];

            if (rootClass == null)
            {
                doc.Add(new Error(ErrorType.Error, 1, 1, "Root node does not contain an x:Class attribute."));
                return;
            }

            bool isApplication = doc.XDocument.RootElement.Name.Name == "Application";

            string rootNamespace, rootType, rootAssembly;

            XamlG.ParseXmlns(rootClass.Value, out rootType, out rootNamespace, out rootAssembly);

            CompilationUnit cu = new CompilationUnit(doc.FileName);

            doc.CompilationUnit = cu;

            DomRegion rootRegion = doc.XDocument.RootElement.Region;

            if (doc.XDocument.RootElement.IsClosed)
            {
                rootRegion.End = doc.XDocument.RootElement.ClosingTag.Region.End;
            }

            DomType declType = new DomType(cu, ClassType.Class, Modifiers.Partial | Modifiers.Public, rootType,
                                           doc.XDocument.RootElement.Region.Start, rootNamespace, rootRegion);

            cu.Add(declType);

            DomMethod initcomp = new DomMethod();

            initcomp.Name       = "InitializeComponent";
            initcomp.Modifiers  = Modifiers.Public;
            initcomp.ReturnType = DomReturnType.Void;
            declType.Add(initcomp);

            DomField _contentLoaded = new DomField("_contentLoaded");

            _contentLoaded.ReturnType = new DomReturnType("System.Boolean");

            if (isApplication)
            {
                return;
            }

            cu.Add(new DomUsing(DomRegion.Empty, "System"));
            cu.Add(new DomUsing(DomRegion.Empty, "System.Windows"));
            cu.Add(new DomUsing(DomRegion.Empty, "System.Windows.Controls"));
            cu.Add(new DomUsing(DomRegion.Empty, "System.Windows.Documents"));
            cu.Add(new DomUsing(DomRegion.Empty, "System.Windows.Input"));
            cu.Add(new DomUsing(DomRegion.Empty, "System.Windows.Media"));
            cu.Add(new DomUsing(DomRegion.Empty, "System.Windows.Media.Animation"));
            cu.Add(new DomUsing(DomRegion.Empty, "System.Windows.Shapes"));
            cu.Add(new DomUsing(DomRegion.Empty, "System.Windows.Controls.Primitives"));

//			Dictionary<string,string> namespaceMap = new Dictionary<string, string> ();
//			namespaceMap["x"] = "http://schemas.microsoft.com/winfx/2006/xaml";

            XName nameAtt = new XName("x", "Name");

            foreach (XElement el in doc.XDocument.RootElement.AllDescendentElements)
            {
                XAttribute name = el.Attributes [nameAtt];
                if (name != null && name.IsComplete)
                {
                    string type = ResolveType(el);
                    if (type == null || type.Length == 0)
                    {
                        doc.Add(new Error(ErrorType.Error, el.Region.Start, "Could not find namespace for '" + el.Name.FullName + "'."));
                    }
                    else
                    {
                        declType.Add(new DomField(name.Value, Modifiers.Internal, el.Region.Start, new DomReturnType(type)));
                    }
                }
            }
        }