Esempio n. 1
0
        public static XmlNode CreateMatchXmlNode(XmlDocument doc, int id, IMatch match)
        {
            //throw new NotImplementedException();

            // <Match Id="" Name="" Open="">
            //   <File />
            //   <LineNumber />
            //   <RegExpName><CDATA ELEMENT></RegExpName>
            //   <RegExpDescription><CDATA ELEMENT></RegExpDescription>
            //   <RegExpExpression><CDATA ELEMENT></RegExpExpression>
            //   <RegExpSummary><CDATA ELEMENT></RegExpSummary>
            //   <Severity Value="">Fatal|Critical|Warning|Info</Severity>
            // </Match>

            XmlNode matchNode           = doc.CreateNode(XmlNodeType.Element, "Match", null);
            XmlNode projectNode         = doc.CreateNode(XmlNodeType.Element, "Project", null);
            XmlNode categoryNode        = doc.CreateNode(XmlNodeType.Element, "Category", null);
            XmlNode rootDirectoryNode   = doc.CreateNode(XmlNodeType.Element, "RootDirectory", null);
            XmlNode filenameNode        = doc.CreateNode(XmlNodeType.Element, "FileName", null);
            XmlNode lineNumberNode      = doc.CreateNode(XmlNodeType.Element, "LineNumber", null);
            XmlNode ruleNameNode        = doc.CreateNode(XmlNodeType.Element, "RuleName", null);
            XmlNode ruleDescriptionNode = doc.CreateNode(XmlNodeType.Element, "RuleDescription", null);
            XmlNode ruleExpressionNode  = doc.CreateNode(XmlNodeType.Element, "RuleExpression", null);
            XmlNode codeExtractNode     = doc.CreateNode(XmlNodeType.Element, "CodeExtract", null);
            XmlNode severityNode        = doc.CreateNode(XmlNodeType.Element, "Severity", null);

            IConfigurationProxy cfgProxy = ProxyHome.Instance.RetrieveConfigurationProxy(OutputKeyKeeper.Instance.AccessKey);

            // Match node...
            XmlAttribute idAttrib   = doc.CreateAttribute("Id");
            XmlAttribute nameAttrib = doc.CreateAttribute("Name");
            XmlAttribute openAttrib = doc.CreateAttribute("Open");

            idAttrib.Value   = id + "";
            nameAttrib.Value = match.Filename;
            openAttrib.Value = "false";

            matchNode.Attributes.Append(idAttrib);
            matchNode.Attributes.Append(nameAttrib);
            matchNode.Attributes.Append(openAttrib);


            IProjectDefinition   projectDefinition   = cfgProxy.Project(match.ProjectDefinitionRef.Id);
            ICategoryDeclaration categoryDeclaration = cfgProxy.CategoryDeclaration(match.RuleDeclarationRef.ParentDeclaration.Id);
            IRuleDeclaration     ruleDeclaration     = cfgProxy.RuleDeclarationFromRuleId(match.RuleDeclarationRef.Id);


            // File node and Line number node...
            projectNode.InnerText       = projectDefinition.Name;
            categoryNode.InnerText      = categoryDeclaration.Name;
            rootDirectoryNode.InnerText = match.RootDirectoryPath;
            filenameNode.InnerText      = match.Filename;
            lineNumberNode.InnerText    = match.LineNumber + "";


            // RegExpName node...
            XmlCDataSection cdataRegExpNameNode = doc.CreateCDataSection(ruleDeclaration.Name);

            ruleNameNode.AppendChild(cdataRegExpNameNode);

            // RegExpDescription node...
            XmlCDataSection cdataRegExpDescriptionNode = doc.CreateCDataSection(ruleDeclaration.Description);

            ruleDescriptionNode.AppendChild(cdataRegExpDescriptionNode);

            // RegExpExpression node...
            XmlCDataSection cdataRegExpExpression = doc.CreateCDataSection(ruleDeclaration.Expression);

            ruleExpressionNode.AppendChild(cdataRegExpExpression);

            // RegExpSummary node...
            XmlCDataSection cdataRegExpSummary = doc.CreateCDataSection(match.CodeExtract);

            codeExtractNode.AppendChild(cdataRegExpSummary);

            // Severity node...
            XmlAttribute severityValueAttrib = doc.CreateAttribute("Value");

            severityValueAttrib.Value = (int)match.Severity + "";
            severityNode.InnerText    = match.Severity.ToString();
            severityNode.Attributes.Append(severityValueAttrib);

            // Add all nodes to the 'node' given in the arguments.
            matchNode.AppendChild(projectNode);
            matchNode.AppendChild(categoryNode);
            matchNode.AppendChild(ruleNameNode);
            matchNode.AppendChild(ruleDescriptionNode);
            matchNode.AppendChild(ruleExpressionNode);
            matchNode.AppendChild(rootDirectoryNode);
            matchNode.AppendChild(filenameNode);
            matchNode.AppendChild(lineNumberNode);
            matchNode.AppendChild(codeExtractNode);
            matchNode.AppendChild(severityNode);
            return(matchNode);
        }
Esempio n. 2
0
        private void StartSearch()
        {
            IConfigurationProxy cfgProxy = ProxyHome.Instance.RetrieveConfigurationProxy(EngineKeyKeeper.Instance.AccessKey);

            List <LinkFile2Language> bindedProj2LanguageFileType = null;

            // Retrieve project definitions from the Configuration component.
            Dictionary <int, IProjectDefinition> projects = cfgProxy.Projects();

            // Let's run through all the projects and do our thing.
            foreach (KeyValuePair <int, IProjectDefinition> pair in projects)
            {
                IProjectDefinition project = pair.Value;
                if (!project.Enabled)
                {
                    continue; // Project definition was disabled.
                }
                // Here we create the language file type containers for all the languages
                // that are in play for the current project.
                bindedProj2LanguageFileType = BindProjectLanguagesToFileTypes(project);


                // Find all files associated with the language file extension in
                // the current file container 'linkFile2Language'.
                foreach (LinkFile2Language linkFile2Language in bindedProj2LanguageFileType)
                {
                    foreach (IDirectoryDefinition dir in project.Directories)
                    {
                        if (!dir.Enabled)
                        {
                            continue;
                        }

                        if (!Directory.Exists(dir.Path))
                        {
                            string s = string.Format("No directory found ({0})", dir.Path);
                            throw new IOException(s);
                        }

                        IRootDirectoryStatistics rds = ProxyHome.Instance.RetrieveStatisticsProxy(EngineKeyKeeper.Instance.AccessKey).CreateRootDirectory(project.Id);
                        rds.RootDirectory = dir.Path;

                        FileSearchEngine fileSearchEngine = new FileSearchEngine(dir, linkFile2Language.Language.Extension);
                        fileSearchEngine.ExcludeDirs            = project.ExcludedDirectories.Select(d => d).ToList();
                        fileSearchEngine.IncludeSubDirsInSearch = true;
                        fileSearchEngine.Search();

                        // Adding all the files found with the extention given by
                        // 'linkFile2Language.Language.Extension' to the file container 'linkFile2Language'.
                        linkFile2Language.Filenames.AddRange(fileSearchEngine.FileFoundDuringSearch);

                        // Adding all the files found to the StatisticsComponentProxy.
                        ProxyHome.Instance.RetrieveStatisticsProxy(EngineKeyKeeper.Instance.AccessKey).IncrementCounter(CounterIds.Files, fileSearchEngine.FileCount);
                        rds.Filenames.AddRange(fileSearchEngine.FileFoundDuringSearch);
                    }
                }


                TimeTracker tt = new TimeTracker("Execution of regular expression on each file.");
                tt.Start();


                IMatchProxy matchProxy = ProxyHome.Instance.RetrieveMatchProxy(EngineKeyKeeper.Instance.AccessKey);

                // Let's execute each regular expression from each rule that are enabled
                // and should be applied to the current language.
                foreach (LinkFile2Language linkFile2Language in bindedProj2LanguageFileType)
                {
                    foreach (KeyValuePair <int, ICategoryDefinition> categoryDefinition in project.Categories)
                    {
                        if (!categoryDefinition.Value.Enabled)
                        {
                            continue;
                        }


                        foreach (KeyValuePair <int, IRuleDefinition> ruleDefinition in categoryDefinition.Value.Rules)
                        {
                            if (!ruleDefinition.Value.Enabled)
                            {
                                continue;
                            }


                            // Let's check whether or not the current 'rule' is associated with the current 'language'?
                            IRuleDeclaration ruleDeclaration = cfgProxy.RuleDeclarationFromCategoryIdAndRuleId(categoryDefinition.Value.CategoryDeclarationReferenceId, ruleDefinition.Value.RuleDeclarationReferenceId);
                            foreach (KeyValuePair <int, ILanguageDeclaration> languageRef in ruleDeclaration.Languages)
                            {
                                if (languageRef.Key == linkFile2Language.Language.Id)
                                {
                                    // The language reference on the current rule is identical to the current 'linkFile2Language'
                                    // meaning that we should execute the regular expression from the current rule on all the
                                    // files placed in the 'linkFile2Language':
                                    IMatchInfoReferences references = matchProxy.MatchesFactory <IMatchInfoReferences>(typeof(IMatchInfoReferences));

                                    references.ProjectDefinitionReference   = project;
                                    references.CategoryDeclarationReference = cfgProxy.CategoryDeclaration(categoryDefinition.Value.CategoryDeclarationReferenceId);
                                    references.CategoryDefinitionReference  = categoryDefinition.Value;
                                    references.RuleDeclarationReference     = ruleDeclaration;
                                    references.RuleDefinitionReference      = ruleDefinition.Value;
                                    references.LanguageDeclarationReference = languageRef.Value;

                                    Parallel.ForEach(linkFile2Language.Filenames, file => ExecuteRegularExpressionsOnBuffer(file, references, ruleDeclaration));
                                }
                            }
                        }
                    }
                }

                tt.Stop("We are done.");
                tt.ToLog(Log);
            }
        }