Example #1
0
        public override void Execute()
        {
            if (Pause)
            {
                Console.WriteLine("Ready to begin (press Enter)");
                Console.ReadLine();
            }

            Console.WriteLine("Using srcML file {0}", this.File);

            var builder = new UnigramSwumBuilder();

            if (!string.IsNullOrWhiteSpace(CountFile))
            {
                Console.WriteLine("Initializing SamuraiIdSplitter using word count file {0}", this.CountFile);
                builder.Splitter = new SamuraiIdSplitter(CountFile);
            }
            Console.WriteLine("SwumBuilder initialized");

            int methodCount = 0, fieldCount = 0;

            {
                SrcMLFile testFile      = new SrcMLFile(this.File);
                var       functionTypes = new XName[] { SRC.Function, SRC.Constructor, SRC.Destructor };
                foreach (XElement file in testFile.FileUnits)
                {
                    string fileName = file.Attribute("filename").Value;
                    Console.WriteLine("File {0}:", fileName);

                    //compute SWUM on each function
                    foreach (var func in (from func in file.Descendants()
                                          where functionTypes.Contains(func.Name) && !func.Ancestors(SRC.Declaration).Any()
                                          select func))
                    {
                        var nameElement = SrcMLElement.GetNameForMethod(func);
                        if (nameElement != null)
                        {
                            string funcName      = nameElement.Value;
                            string funcSignature = SrcMLElement.GetMethodSignature(func);
                            if (PrintSwum)
                            {
                                Console.WriteLine("<{0}> {1}", func.Name.LocalName, funcSignature);
                            }

                            MethodDeclarationNode mdn = new MethodDeclarationNode(funcName, ContextBuilder.BuildMethodContext(func));
                            builder.ApplyRules(mdn);
                            methodSwum[string.Format("{0}:{1}", fileName, funcSignature)] = mdn;
                            if (PrintSwum)
                            {
                                Console.WriteLine(mdn.ToString() + Environment.NewLine);
                            }

                            methodCount++;
                        }
                    }

                    //compute SWUM on each field
                    foreach (var fieldDecl in (from declStmt in file.Descendants(SRC.DeclarationStatement)
                                               where !declStmt.Ancestors().Any(n => functionTypes.Contains(n.Name))
                                               select declStmt.Element(SRC.Declaration)))
                    {
                        int declPos = 1;
                        foreach (var nameElement in fieldDecl.Elements(SRC.Name))
                        {
                            string fieldName = nameElement.Elements(SRC.Name).Any() ? nameElement.Elements(SRC.Name).Last().Value : nameElement.Value;
                            if (PrintSwum)
                            {
                                Console.WriteLine("Field: {0}, Name: {1}", fieldDecl.Value, fieldName);
                            }

                            FieldDeclarationNode fdn = new FieldDeclarationNode(fieldName, ContextBuilder.BuildFieldContext(fieldDecl));
                            builder.ApplyRules(fdn);
                            fieldSwum[string.Format("{0}:{1}:{2}", fileName, fieldDecl.Value, declPos)] = fdn;
                            if (PrintSwum)
                            {
                                Console.WriteLine(fdn.ToString() + Environment.NewLine);
                            }

                            fieldCount++;
                            declPos++;
                        }
                    }
                }
            }

            GC.Collect();

            Console.WriteLine("{0} functions analyzed", methodCount);
            Console.WriteLine("{0} functions in dictionary", methodSwum.Count);
            Console.WriteLine("{0} fields analyzed", fieldCount);
            Console.WriteLine("{0} fields in dictionary", fieldSwum.Count);

            if (Pause)
            {
                Console.WriteLine("Finished building SWUM (press Enter)");
                Console.ReadLine();
            }
        }
Example #2
0
        public async Task <XDocument> GetInfo(string id)
        {
            if (loadTask != null)
            {
                await loadTask;
            }

            // The XML file should be completely read by now
            XElement elm = FindGame(id);

            if (elm == null)
            {
                return(null);
            }

            IEnumerable <XElement> locales = elm.Descendants("locale").ToArray();
            // Find the correct locale
            XElement locale = locales.FirstOrDefault(l => l.Attribute("lang").Value == language) ??
                              locales.FirstOrDefault(l => l.Attribute("lang").Value == "EN");

            if (locale == null)
            {
                return(null);                // No locale found, this is impossible afaik
            }
            string xml = "<xml version=\"1.0\"?><gameinfo><title>" + locale.Descendants("title").First().Value + "</title><summary>";

            xml += locale.Descendants("synopsis").First().Value;
            xml += "</summary><info>";

            XName[] tagsToSkip = new XName[] { "id", "type", "locale", "rating", "rom" };
            foreach (XElement node in elm.DescendantNodes().Cast <XElement>().Where(node => !tagsToSkip.Contains(node.Name)))
            {
                // Add the rest as info item
                if (node.Name == "date")
                {
                    xml += ParseDate(node);
                }
                else if (node.Name == "wi-fi")
                {
                    xml += ParseWifi(node);
                }
                else if (node.Name == "input")
                {
                    xml += ParseInput(node);
                }

                xml += ParseElement(node);
            }

            xml += "</info>";

            return(XDocument.Parse(xml));
        }
Example #3
0
        private Item parseXml(XElement element)
        {
            Item item = null;

            switch (element.Name.ToString())
            {
            case "MenuItem":
                item =
                    new PxMenuItem(
                        this,
                        element.Element("Text").Value,
                        element.Element("TextShort").Value,
                        element.Attribute("sortCode").Value,
                        element.Parent.Attribute("selection") != null ? element.Parent.Attribute("selection").Value : "",
                        element.Attribute("selection").Value,
                        element.Element("Description").Value
                        );

                XName[] subItemLabels = new XName[] { "MenuItem", "Headline", "Link" };

                item.Cast <PxMenuItem>().AddSubItemRange(
                    from e in element.Elements()
                    where subItemLabels.Contains(e.Name)
                    select parseXml(e)
                    );

                break;

            case "Headline":
                item =
                    new Headline(
                        element.Element("Text").Value,
                        element.Element("TextShort").Value,
                        element.Attribute("sortCode").Value,
                        element.Parent.Attribute("selection").Value,
                        element.Attribute("selection").Value,
                        element.Element("Description").Value
                        );

                break;

            case "Link":
                item =
                    new TableLink(
                        element.Element("Text").Value,
                        element.Element("TextShort").Value,
                        element.Attribute("sortCode").Value,
                        element.Parent.Attribute("selection").Value,
                        element.Attribute("selection").Value,
                        element.Element("Description").Value,
                        element.Attribute("type").Value.ToEnum <LinkType>(),
                        element.Attribute("status").Value == "" ? TableStatus.NotSet : (TableStatus)element.Attribute("status").Value[0],
                        element.Element("Published").Value == "" ? null : (DateTime?)DateTime.Parse(element.Element("Published").Value),
                        element.Element("LastUpdated").Value == "" ? null : (DateTime?)DateTime.Parse(element.Element("LastUpdated").Value),
                        element.Element("StartTime").Value,
                        element.Element("EndTime").Value,
                        element.Attribute("tableId").Value,
                        element.Attribute("category").Value == "" ? PresCategory.NotSet : (PresCategory)element.Attribute("category").Value[0]
                        );

                foreach (XElement a in element.Elements("Attribute"))
                {
                    item.Cast <Link>().SetAttribute(a.Attribute("name").Value, a.Value);
                }

                break;

            case "Url":
                item =
                    new Url(
                        element.Element("Text").Value,
                        element.Attribute("sortCode").Value,
                        element.Parent.Attribute("selection").Value,
                        element.Attribute("selection").Value,
                        element.Element("Description").Value,
                        element.Attribute("category").Value == "" ? PresCategory.NotSet : (PresCategory)element.Attribute("category").Value[0],
                        element.Element("Url").Value,
                        element.Attribute("linkPres").Value == "" ? LinkPres.NotSet : (LinkPres)element.Attribute("linkPres").Value[0]
                        );

                foreach (XElement a in element.Elements("Attribute"))
                {
                    item.Cast <Link>().SetAttribute(a.Attribute("name").Value, a.Value);
                }

                break;

            default:
                break;
            }

            if (item.IsNotOfType <Url>())
            {
                foreach (Url u in element.Elements("Url").Select(e => parseXml(e)))
                {
                    if (u != null)
                    {
                        item.AddUrl(u);
                    }
                }
            }

            item.ID.Menu = element.Parent.Attribute("selection") != null?element.Parent.Attribute("selection").Value : "";

            if (Restriction == null || Restriction(item))
            {
                if (AlterItemBeforeStorage != null)
                {
                    AlterItemBeforeStorage(item);
                }

                return(item);
            }
            else
            {
                return(null);
            }
        }
Example #4
0
        /// <summary>
        /// Returns a collection of <see cref="Snapshot"/>s from configuration elements
        /// </summary>
        /// <param name="snapshots">A collection of snapshot configuration elements</param>
        /// <returns>Returns a list of <see cref="Snapshot"/>s</returns>
        public Group ParseScene(Group Root, DateTime Version, XElement x)
        {
            var scene = new Group()
            {
                Compiler = Compiler, XElement = x, IsScene = true
            };

            Root.Children.Add(scene);

            var xaml = x.Elements(ns + "Xaml").SingleOrDefault();

            xaml = x.Elements(ns + "Source").SingleOrDefault() ?? xaml;
            if (x.Attribute("Source") != null || x.Attribute("File") != null || x.Attribute("Type") != null)
            {
                xaml = x;
            }
            var srcAttr = xaml.Attribute("Source") ?? xaml.Attribute("File");

            if (srcAttr != null)
            {
                scene.Source = srcAttr.Value;
            }
            var src = scene.Source;

            if (xaml == null)
            {
                throw new CompilerException("Scene must contain source file specification.", 10, x, scene, null);
            }
            scene.XamlElement = xaml;

            var width  = (double?)xaml.Attribute("Width") ?? double.PositiveInfinity;
            var height = (double?)xaml.Attribute("Height") ?? double.PositiveInfinity;

            scene.Dependencies   = (string)x.Attribute("DependsOn");
            scene.CulturesString = (string)x.Attribute("Cultures");
            scene.OutputPath     = (string)x.Attribute("OutputPath");
            scene.PreferredSize  = new Size(width, height);

            ParseStyle(x, scene);

            DateTime XamlVersion = new DateTime();
            FileInfo info;

            if (src != null)
            {
                if (!(src.StartsWith("http://") || src.StartsWith("https://")))
                {
                    info = new FileInfo(Compiler.MapPath(src));
                    if (!info.Exists)
                    {
                        throw new CompilerException("Source file " + info.FullName + " not found.", 11, xaml, scene, null);
                    }

                    XamlVersion = info.LastWriteTimeUtc;
                }
                if (XamlVersion > Version)
                {
                    Version = XamlVersion;
                }
            }
            else
            {
                string assemblyName = (string)xaml.Attribute("Assembly");
                string typename     = (string)xaml.Attribute("Type");

                scene.AssemblyName = assemblyName;
                scene.TypeName     = typename;

                if (typename == null)
                {
                    scene.InnerXaml = xaml.Elements().SingleOrDefault();
                    if (scene.InnerXaml == null)
                    {
                        throw new CompilerException("Scene must contain a single XAML root element", 16, xaml, scene, null);
                    }
                }
            }

            if (xaml.Attribute("Dynamic") != null && xaml.Attribute("Dynamic").Value == "true")
            {
                Version = DateTime.Now.ToUniversalTime();
            }


            // parse dependencies
            foreach (var dependency in x.Elements().Where(child => child.Name == ns + "Depends"))
            {
                var d = new Dependency(dependency)
                {
                    Compiler = Compiler
                };
                if (d.Version > Version)
                {
                    Version = d.Version;
                }
            }

            scene.Version = Version;

            if (Compiler.Parameters.Count > 0)
            {
                var p = new Parameters(Compiler.Parameters);
                p.Compiler    = Compiler;
                p.ElementName = string.Empty;
                p.XElement    = null;
                scene.Children.Add(p);
            }

            // parse ordinary elements
            var names = new XName[] { ns + "Xaml", ns + "Depends" };

            foreach (var child in x.Elements().Where(child => !names.Contains(child.Name)))
            {
                Parse(child, scene);
            }

            foreach (var node in x.Nodes().Where(node => !(node is XElement || node is XComment ||
                                                           (node is XText && (string.IsNullOrWhiteSpace(((XText)node).Value) || node.NodeType == System.Xml.XmlNodeType.Whitespace || node.NodeType == System.Xml.XmlNodeType.SignificantWhitespace)))))
            {
                Root.Errors.Error(string.Format("Invalid content {0}", node.ToString(SaveOptions.DisableFormatting | SaveOptions.OmitDuplicateNamespaces)), "23", node);
            }

            return(scene);
        }