public static IRuleDeclaration RuleDeclarationMapper(ICategoryDeclaration parentDeclaration, Model.RuleDeclaration ruleDeclaration) { IRuleDeclaration rule = null; try { rule = ConfigurationComponentFactory().ConfigurationFactory <IRuleDeclaration>(typeof(IRuleDeclaration)); } catch (Exception e) { throw new DataAccessComponentException(null, -1, "Configuration proxy factory failure - unable to create an instance of " + typeof(IRuleDeclaration) + "?", e); } try { rule.Id = ruleDeclaration.Id; rule.Name = ruleDeclaration.Name; rule.Description = ruleDeclaration.Description; rule.Expression = ruleDeclaration.Expression; rule.Severity = RuleSeverityMapper.Int2RuleSeverity(ruleDeclaration.Severity); rule.ParentDeclaration = parentDeclaration; foreach (var language in ruleDeclaration.LanguageDeclaration) { rule.Languages.Add(language.Id, LanguageDeclarationMapper(language)); } } catch (Exception e) { throw new DataAccessComponentException(null, -1, "Mapping process failure?", e); } return(rule); }
private List <MatchMappedToExcel> ConvertMatchesToExcelFormat(List <IMatch> matches) { List <MatchMappedToExcel> list = new List <MatchMappedToExcel>(); foreach (IMatch m in matches) { list.Add(new MatchMappedToExcel() { BatchId = m.Batch.Id.ToString(), Timestamp = m.Batch.TimeStamp, ProjectName = m.ProjectDefinitionRef.Name, MatchId = m.Id.ToString(), Language = m.LanguageDeclarationRef.Name, Severity = $"{RuleSeverityMapper.Int2RuleSeverity((int)m.Severity)}", Category = m.CategoryDeclarationRef.Name, CategoryDescription = m.CategoryDeclarationRef.Description, Rule = m.RuleDeclarationRef.Name, RuleDescription = m.RuleDeclarationRef.Description, RuleExpression = m.RuleDeclarationRef.Expression, Filename = m.Filename, MatchOnLine = m.LineNumber.ToString(), CodeExtract = m.CodeExtract }); } return(list); }
private void LoadCategoryDeclarations(XDocument doc) { lock (_lockConfigurationFile) { // Validate input... if (doc == null) { throw new ArgumentNullException("doc"); } foreach (XElement categoryDeclaration in doc.Descendants(CATEGORY_DECLARATION)) { CategoryDeclaration cd = new CategoryDeclaration(); cd.Id = int.Parse(categoryDeclaration.Attribute(XName.Get(ID)).Value); cd.Name = categoryDeclaration.Attribute(XName.Get(NAME)).Value; cd.Description = categoryDeclaration.Element(XName.Get(CATEGORY_DESCRIPTION)).Value; cd.Rules = new Dictionary <int, IRuleDeclaration>(); foreach (XElement ruleDeclaration in categoryDeclaration.Descendants(XName.Get(RULE_DECLARATION))) { RuleDeclaration rd = new RuleDeclaration(); rd.ParentDeclaration = cd; rd.Id = int.Parse(ruleDeclaration.Attribute(XName.Get(ID)).Value); rd.Name = ruleDeclaration.Attribute(XName.Get(NAME)).Value; rd.Description = ruleDeclaration.Descendants(XName.Get(RULE_DESCRIPTION)).Single().Value; rd.Severity = RuleSeverityMapper.String2RuleSeverity(ruleDeclaration.Descendants(XName.Get(RULE_SEVERITY)).Single().Value); rd.Expression = ruleDeclaration.Descendants(XName.Get(RULE_EXPRESSION)).Single().Value; IEnumerable <XElement> languages = ruleDeclaration.Descendants(XName.Get("Language")); foreach (XElement l in languages) { int id = int.Parse(l.Attribute(XName.Get("RefId")).Value); string name = l.Attribute(XName.Get("RefName")).Value; rd.Languages.Add(id, Manager.Declarations.Languages[id]); } cd.Rules.Add(rd.Id, rd); // Update 'Rule Declaration' statistics counters... ProxyHome.Instance.RetrieveStatisticsProxy(ConfigKeyKeeper.Instance.AccessKey).IncrementCounter(CounterIds.RuleDeclarations); } Manager.Declarations.Categories.Add(cd.Id, cd); // Update 'Category Declaration' statistics counters... ProxyHome.Instance.RetrieveStatisticsProxy(ConfigKeyKeeper.Instance.AccessKey).IncrementCounter(CounterIds.CategoryDeclarations); } } }
public void GenerateOutput_Backup(string outputRootDir) { #region Input validation if (string.IsNullOrWhiteSpace(outputRootDir)) { throw new ArgumentNullException(nameof(outputRootDir)); } if (!Directory.Exists(outputRootDir)) { throw new ArgumentException($"Unable to find directory {outputRootDir}."); } #endregion #region Create output DIR and file OutputDir = Path.Combine(outputRootDir, Ids.EXCEL_DIR); if (!Directory.Exists(Directory.CreateDirectory(OutputDir).FullName)) { throw new OutputComponentException($"Unable to create output directory {OutputDir}."); } OutputFile = Path.Combine(outputRootDir, Ids.EXCEL_DIR, Ids.EXCEL_FILE); // File.Create(OutputFile); // if (!File.Exists(OutputFile)) // throw new OutputComponentException($"Unable to create output file {OutputFile}."); #endregion #region Define row 'n' columns values... int columns = 13; int firstHeaderRow = 0; int secondHeaderRow = firstHeaderRow + 1; int timestampRow = secondHeaderRow + 1; int tableHeaderRow = 0; int row = tableHeaderRow + 1; int freezeRow = tableHeaderRow + 1; #endregion #region Create the Excel xml workbook... // Create a instance... ExcelXmlWorkbook book = new ExcelXmlWorkbook(); // Many such properties exist. Details can be found in the documentation // The author of the document book.Properties.Author = "Code Analyzer"; // This returns the first worksheet. // Note that we have not declared a instance of a new worksheet // All the dirty work is done by the library. Worksheet dashboardSheet = book[0]; Worksheet matchesSheet = book[1]; // Name is the name of the sheet. If not set, the default name // style is "sheet" + sheet number, like sheet1, sheet2 dashboardSheet.Name = "Dashboard"; matchesSheet.Name = "Matches"; // More on this in documentation matchesSheet.FreezeTopRows = freezeRow; // and this too... dashboardSheet.PrintOptions.Orientation = PageOrientation.Landscape; dashboardSheet.PrintOptions.SetMargins(0.5, 0.4, 0.5, 0.4); #endregion #region Creating the header... // Text for the header... dashboardSheet[0, firstHeaderRow].Value = "Octopus | Code Analyzer"; dashboardSheet[0, secondHeaderRow].Value = "The Octopus Code Analyzer - the simple way to keep track of your code base"; // Setting the background color for the header... new Range(dashboardSheet[0, firstHeaderRow], dashboardSheet[30, firstHeaderRow]).Interior.Color = Color.FromArgb(22, 54, 92); new Range(dashboardSheet[0, secondHeaderRow], dashboardSheet[30, secondHeaderRow]).Interior.Color = Color.FromArgb(22, 54, 92); new Range(dashboardSheet[0, secondHeaderRow], dashboardSheet[30, secondHeaderRow]).Border.Sides = BorderSides.Bottom; new Range(dashboardSheet[0, secondHeaderRow], dashboardSheet[30, secondHeaderRow]).Border.LineStyle = Borderline.Double; new Range(dashboardSheet[0, secondHeaderRow], dashboardSheet[30, secondHeaderRow]).Border.Color = Color.FromArgb(255, 255, 255); new Range(dashboardSheet[0, secondHeaderRow], dashboardSheet[30, secondHeaderRow]).Border.Weight = 1; // Setting the foreground color for the header text... new Range(dashboardSheet[0, firstHeaderRow], dashboardSheet[8, firstHeaderRow]).Font.Color = Color.FromArgb(255, 255, 255); new Range(dashboardSheet[0, secondHeaderRow], dashboardSheet[8, secondHeaderRow]).Font.Color = Color.FromArgb(255, 255, 255); // Setting the font for the header text... new Range(dashboardSheet[0, firstHeaderRow], dashboardSheet[8, firstHeaderRow]).Font.Size = 18; new Range(dashboardSheet[0, firstHeaderRow], dashboardSheet[8, firstHeaderRow]).Font.Bold = true; new Range(dashboardSheet[0, secondHeaderRow], dashboardSheet[8, secondHeaderRow]).Font.Size = 8; new Range(dashboardSheet[0, secondHeaderRow], dashboardSheet[8, secondHeaderRow]).Font.Bold = true; dashboardSheet[0, timestampRow].Value = "Exported on: " + DateTime.Now.ToShortDateString(); dashboardSheet[0, timestampRow].Font.Bold = true; dashboardSheet[0, timestampRow].Font.Size = 8; #endregion #region Create the table header for all the data... // Setting the width of the columns... matchesSheet.Columns(0).Width = 100; matchesSheet.Columns(1).Width = 100; matchesSheet.Columns(2).Width = 100; matchesSheet.Columns(3).Width = 100; matchesSheet.Columns(4).Width = 100; matchesSheet.Columns(5).Width = 100; matchesSheet.Columns(6).Width = 100; matchesSheet.Columns(7).Width = 100; matchesSheet.Columns(8).Width = 100; matchesSheet.Columns(9).Width = 100; matchesSheet.Columns(10).Width = 100; matchesSheet.Columns(11).Width = 100; matchesSheet.Columns(12).Width = 100; matchesSheet.Columns(13).Width = 900; // Inserting headers and setting them to bold... matchesSheet[0, tableHeaderRow].Value = "BatchId Id"; matchesSheet[1, tableHeaderRow].Value = "Timestamp"; matchesSheet[2, tableHeaderRow].Value = "Match Id"; matchesSheet[3, tableHeaderRow].Value = "Severity"; matchesSheet[4, tableHeaderRow].Value = "Project"; matchesSheet[5, tableHeaderRow].Value = "Language"; matchesSheet[6, tableHeaderRow].Value = "Category"; matchesSheet[7, tableHeaderRow].Value = "Category description"; matchesSheet[8, tableHeaderRow].Value = "Rule"; matchesSheet[9, tableHeaderRow].Value = "Rule description"; matchesSheet[10, tableHeaderRow].Value = "Rule Expression"; matchesSheet[11, tableHeaderRow].Value = "File name"; matchesSheet[12, tableHeaderRow].Value = "Match on line"; matchesSheet[13, tableHeaderRow].Value = "Code extract"; new Range(matchesSheet[0, tableHeaderRow], matchesSheet[columns, tableHeaderRow]).Font.Bold = true; new Range(matchesSheet[0, tableHeaderRow], matchesSheet[columns, tableHeaderRow]).AutoFilter(); new Range(matchesSheet[0, tableHeaderRow], matchesSheet[columns, tableHeaderRow]).Border.Sides = BorderSides.Bottom; new Range(matchesSheet[0, tableHeaderRow], matchesSheet[columns, tableHeaderRow]).Border.LineStyle = Borderline.Continuous; new Range(matchesSheet[0, tableHeaderRow], matchesSheet[columns, tableHeaderRow]).Border.Weight = 2; #endregion #region Insert data... // Insert data... IMatchProxy matchProxy = ProxyHome.Instance.RetrieveMatchProxy(OutputKeyKeeper.Instance.AccessKey); foreach (IMatch match in matchProxy.Matches()) { if ((row % 2) == 0) { new Range(matchesSheet[0, row], matchesSheet[columns, row]).Interior.Color = Color.FromArgb(220, 230, 241); } else { new Range(matchesSheet[0, row], matchesSheet[columns, row]).Interior.Color = Color.FromArgb(197, 217, 241); } matchesSheet[0, row].Value = match.Batch.Id; matchesSheet[1, row].Value = $"{match.Batch.TimeStamp.ToShortDateString()} - {match.Batch.TimeStamp.ToShortTimeString()}"; matchesSheet[2, row].Value = match.Id; matchesSheet[3, row].Value = $"{RuleSeverityMapper.Int2RuleSeverity((int)match.Severity)}"; matchesSheet[4, row].Value = match.ProjectDefinitionRef.Name; matchesSheet[5, row].Value = match.LanguageDeclarationRef.Name; matchesSheet[6, row].Value = match.CategoryDeclarationRef.Name; matchesSheet[7, row].Value = match.CategoryDeclarationRef.Description; matchesSheet[8, row].Value = match.RuleDeclarationRef.Name; matchesSheet[9, row].Value = match.RuleDeclarationRef.Description; matchesSheet[10, row].Value = match.RuleDeclarationRef.Expression; matchesSheet[11, row].Value = match.Filename; matchesSheet[12, row].Value = match.LineNumber; matchesSheet[13, row].Value = match.CodeExtract; row++; } #endregion #region Export the Excel xml workbook... book.Export(OutputFile); #endregion }
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; } } } } }