Example #1
0
        private void GenerateMatch(IMatchProxy matchProxy, IMatchInfoReferences references, IBatch batch, string filename, int startLineNumber, string matchExtract)
        {
            IMatch m = matchProxy.MatchesFactory <IMatch>(typeof(IMatch));

            m.Id    = Guid.NewGuid();
            m.Batch = batch;
            m.ProjectDefinitionRef   = references.ProjectDefinitionReference;
            m.CategoryDeclarationRef = references.CategoryDeclarationReference;
            m.CategoryDefinitionRef  = references.CategoryDefinitionReference;
            m.LanguageDeclarationRef = references.LanguageDeclarationReference;
            m.RuleDeclarationRef     = references.RuleDeclarationReference;
            m.RootDirectoryPath      = @"N/A";
            m.Filename    = filename;
            m.CodeExtract = matchExtract;
            m.LineNumber  = startLineNumber;
            m.Result      = MatchStatus.Match;
            m.Severity    = references.RuleDeclarationReference.Severity;

            matchProxy.AddMatch(m);
        }
Example #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);
            }
        }
Example #3
0
        private void GenerateMatch(IMatchProxy matchProxy, IMatchInfoReferences references, IBatch batch, string filename, int startLineNumber, string matchExtract)
        {
            IMatch m = matchProxy.MatchesFactory<IMatch>(typeof(IMatch));
            m.Id                     = Guid.NewGuid();
            m.Batch                  = batch;
            m.ProjectDefinitionRef   = references.ProjectDefinitionReference;
            m.CategoryDeclarationRef = references.CategoryDeclarationReference;
            m.CategoryDefinitionRef  = references.CategoryDefinitionReference;
            m.LanguageDeclarationRef = references.LanguageDeclarationReference;
            m.RuleDeclarationRef     = references.RuleDeclarationReference;
            m.RootDirectoryPath      = @"N/A";
            m.Filename               = filename;
            m.CodeExtract            = matchExtract;
            m.LineNumber             = startLineNumber;
            m.Result                 = MatchStatus.Match;
            m.Severity               = references.RuleDeclarationReference.Severity;

            matchProxy.AddMatch(m);
        }
Example #4
0
        private void ExecuteRegularExpressionsOnBuffer(string filename, IMatchInfoReferences references, IRuleDeclaration ruleDeclaration)
        {
            const string summary = "summary";

            string buffer = LoadFile(filename);

            if (string.IsNullOrEmpty(buffer))
            {
                return;
            }

            int lastLineNum;

            string[] lines = buffer.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
            lastLineNum = (lines != null && lines.Length >= 1) ? lines.Length - 1 : 0;


            #region Statistics: update/increment the total number of lines and total number of whitespace lines in tested/scanned files.
            if (!ProxyHome.Instance.CheckCache(filename))
            {
                ProxyHome.Instance.RetrieveStatisticsProxy(EngineKeyKeeper.Instance.AccessKey).IncrementCounter(CounterIds.TotalNumberOfLinesInFiles, lastLineNum);
                ProxyHome.Instance.RetrieveStatisticsProxy(EngineKeyKeeper.Instance.AccessKey).IncrementCounter(CounterIds.TotalNumberOfWhitespaceLinesInFiles, lines.Count(string.IsNullOrWhiteSpace));
                ProxyHome.Instance.InsertInCache(filename);
            }
            #endregion


            string correctedRegExp = "(?<" + summary + ">" + ruleDeclaration.Expression + ")";


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

            MatchCollection matches = Regex.Matches(buffer, correctedRegExp, RegexOptions.IgnoreCase);
            foreach (System.Text.RegularExpressions.Match match in matches)
            {
                int startIndx = match.Index;
                int endIndx   = startIndx + match.Groups[summary].Value.Length;

                int accumulation    = 0;
                int startLineNumber = 0;
                int endLineNumber   = 0;
                foreach (string line in lines)
                {
                    if (accumulation < startIndx)
                    {
                        // This is for finding the line number where the match starts...
                        accumulation += line.Length + Environment.NewLine.Length;
                        endLineNumber = startLineNumber++;
                    }
                    else
                    {
                        if (accumulation < endIndx)
                        {
                            // This is for finding the line number where the match ends...
                            accumulation += line.Length + Environment.NewLine.Length;

                            if ((endLineNumber + 1) < lastLineNum)
                            {
                                endLineNumber++;
                            }
                            else
                            {
                                endLineNumber = lastLineNum;

                                // Extract the code from the actual file...
                                string matchExtract = ExtractTheMatchAndSurroundings(lines, buffer.Length, ++startLineNumber, ++endLineNumber);

                                // Create the match and insert it into the MatchProxy...
                                GenerateMatch(matchProxy, references, batch, filename, startLineNumber, matchExtract);

                                // Let's update the statistics manager about the match we just found.
                                ProxyHome.Instance.RetrieveStatisticsProxy(EngineKeyKeeper.Instance.AccessKey).IncrementCounter(RuleSeverityMapper.Severity2CounterId((RuleSeverity)ruleDeclaration.Severity));
                            }
                        }
                        else
                        {
                            // Extract the code from the actual file...
                            string matchExtract = ExtractTheMatchAndSurroundings(lines, buffer.Length, ++startLineNumber, ++endLineNumber);

                            // Create the match and insert it into the MatchProxy...
                            GenerateMatch(matchProxy, references, batch, filename, startLineNumber, matchExtract);

                            // Let's update the statistics manager about the match we just found.
                            ProxyHome.Instance.RetrieveStatisticsProxy(EngineKeyKeeper.Instance.AccessKey).IncrementCounter(RuleSeverityMapper.Severity2CounterId((RuleSeverity)ruleDeclaration.Severity));
                            break;
                        }
                    }
                }
            }
        }
Example #5
0
        private void ExecuteRegularExpressionsOnBuffer(string filename, IMatchInfoReferences references, IRuleDeclaration ruleDeclaration)
        {
            const string summary = "summary";

              string buffer = LoadFile(filename);

              if (string.IsNullOrEmpty(buffer))
            return;

            int lastLineNum;
            string[] lines = buffer.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
            lastLineNum = (lines != null && lines.Length >= 1) ? lines.Length - 1 : 0;

            #region Statistics: update/increment the total number of lines and total number of whitespace lines in tested/scanned files.
            if (!ProxyHome.Instance.CheckCache(filename))
            {
            ProxyHome.Instance.RetrieveStatisticsProxy(EngineKeyKeeper.Instance.AccessKey).IncrementCounter(CounterIds.TotalNumberOfLinesInFiles, lastLineNum);
            ProxyHome.Instance.RetrieveStatisticsProxy(EngineKeyKeeper.Instance.AccessKey).IncrementCounter(CounterIds.TotalNumberOfWhitespaceLinesInFiles, lines.Count(string.IsNullOrWhiteSpace));
                ProxyHome.Instance.InsertInCache(filename);
            }
            #endregion

            string correctedRegExp = "(?<" + summary + ">" + ruleDeclaration.Expression + ")";

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

              MatchCollection matches = Regex.Matches(buffer, correctedRegExp, RegexOptions.IgnoreCase);
              foreach (System.Text.RegularExpressions.Match match in matches)
              {

            int startIndx = match.Index;
            int endIndx   = startIndx + match.Groups[summary].Value.Length;

            int accumulation    = 0;
            int startLineNumber = 0;
            int endLineNumber   = 0;
            foreach (string line in lines)
            {
              if (accumulation < startIndx)
              {
              // This is for finding the line number where the match starts...
            accumulation += line.Length + Environment.NewLine.Length;
                        endLineNumber = startLineNumber++;
              }
              else
              {
            if (accumulation < endIndx)
            {
              // This is for finding the line number where the match ends...
              accumulation += line.Length + Environment.NewLine.Length;

                if ((endLineNumber + 1) < lastLineNum)
                {
                    endLineNumber++;
                }
                else
                {
                    endLineNumber = lastLineNum;

                                // Extract the code from the actual file...
                                string matchExtract = ExtractTheMatchAndSurroundings(lines, buffer.Length, ++startLineNumber, ++endLineNumber);

                                // Create the match and insert it into the MatchProxy...
                                GenerateMatch(matchProxy, references, batch, filename, startLineNumber, matchExtract);

                                // Let's update the statistics manager about the match we just found.
                                ProxyHome.Instance.RetrieveStatisticsProxy(EngineKeyKeeper.Instance.AccessKey).IncrementCounter(RuleSeverityMapper.Severity2CounterId((RuleSeverity)ruleDeclaration.Severity));
                            }
            }
            else
            {
                            // Extract the code from the actual file...
                            string matchExtract = ExtractTheMatchAndSurroundings(lines, buffer.Length, ++startLineNumber, ++endLineNumber);

                            // Create the match and insert it into the MatchProxy...
                            GenerateMatch(matchProxy, references, batch, filename, startLineNumber, matchExtract);

                            // Let's update the statistics manager about the match we just found.
                            ProxyHome.Instance.RetrieveStatisticsProxy(EngineKeyKeeper.Instance.AccessKey).IncrementCounter(RuleSeverityMapper.Severity2CounterId((RuleSeverity)ruleDeclaration.Severity));
              break;
            }
              }
            }
              }
        }