private void MatchFile(string outputRootDir, ICategorySummary category, IRuleSummary ruleSummary) { XmlDocument doc = new XmlDocument(); XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null); doc.AppendChild(declaration); var pi = doc.CreateProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"" + Ids.XSLT_DIR + "/" + Ids.MATCHES_XSLT_FILE + "\""); doc.AppendChild(pi); XmlNode matches = doc.CreateNode(XmlNodeType.Element, "Matches", null); XmlNode summaryNode = doc.CreateNode(XmlNodeType.Element, "Summary", null); XmlNode countNode = doc.CreateNode(XmlNodeType.Element, "Count", null); XmlNode ruleNameNode = doc.CreateNode(XmlNodeType.Element, "Name", null); XmlNode descriptionNode = doc.CreateNode(XmlNodeType.Element, "Description", null); XmlNode expressionNode = doc.CreateNode(XmlNodeType.Element, "Expression", null); summaryNode.AppendChild(expressionNode); summaryNode.AppendChild(ruleNameNode); summaryNode.AppendChild(descriptionNode); summaryNode.AppendChild(countNode); matches.AppendChild(summaryNode); doc.AppendChild(matches); // Summary.Name node... XmlCDataSection cdataRuleNameNode = doc.CreateCDataSection(ruleSummary.Name); ruleNameNode.AppendChild(cdataRuleNameNode); // Summary.Description node... XmlCDataSection cdataDescriptionNode = doc.CreateCDataSection(ruleSummary.Description); descriptionNode.AppendChild(cdataDescriptionNode); // Summary.Expression node... XmlCDataSection cdataExpressionNode = doc.CreateCDataSection(ruleSummary.Expression); expressionNode.AppendChild(cdataExpressionNode); // Summary.Count node... countNode.InnerText = ruleSummary.Matches.Count.ToString(); //int matchId = 1; foreach (IMatch match in ruleSummary.Matches) { //MatchXml(doc, matches, matchId++, match); } doc.Save(CreateFullPathForMatchFile(outputRootDir, category.Name, ruleSummary.Name)); }
//private void SubTocRulesContent(string outputRootDir, ICategorySummary categorySummary) //{ // XmlDocument doc = new XmlDocument(); // XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null); // doc.AppendChild(declaration); // var pi = doc.CreateProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"" + Ids.XSLT_DIR + "/" + Ids.SUB_TOC_XSLT_FILE + "\""); // doc.AppendChild(pi); // XmlNode rules = doc.CreateNode(XmlNodeType.Element, "Rules", null); // // Creating a list to hold all the 'ruleSummary summaries'... // Dictionary<string, IRuleSummary> ruleSummaries = new Dictionary<string, IRuleSummary>(); // // Extract all the matches related to the current category... // List<IMatch> matches = MatchHome.MatchesByCategory(categorySummary.Name); // // Extract all the rules from the matches in the current category... // int ruleCounter = 0; // foreach (IMatch match in matches) // { // if (!ruleSummaries.ContainsKey(match.RegExp.Name)) // { // IRuleSummary ruleSummary = ProxyHome.Instance.EngineComponentProxy.InstatiateRuleSummary(++ruleCounter, // match.RegExp.Enabled, // 1, // match.RegExp.Name, // match.RegExp.Description, // match.RegExp.Expression, // match.Severity, // CreateNameForMatchFile(categorySummary.Name, match.RegExp.Name)); // ruleSummary.Matches.Add(match); // // First ruleSummary of this specific ruleSummary. // ruleSummaries.Add(ruleSummary.Name, ruleSummary); // } // else // { // // Second or n'th encounter of this specific ruleSummary. // ruleSummaries[match.RegExp.Name].Count++; // ruleSummaries[match.RegExp.Name].Matches.Add(match); // } // } // // Now that have extracted all the 'ruleSummary summaries' let's go ahead and create the // // ruleSummary xml nodes... // foreach (KeyValuePair<string, IRuleSummary> pair in ruleSummaries) // { // RuleXml(doc, rules, outputRootDir, categorySummary, pair.Value); // MatchFile(outputRootDir, categorySummary, pair.Value); // } // doc.AppendChild(rules); // doc.Save(CreateFullPathForRulesFile(outputRootDir, categorySummary.Name)); //} private void RuleXml(XmlDocument doc, XmlNode node, string outputRootDir, ICategorySummary category, IRuleSummary ruleSummary) { // The following is an example of how a category is represented in the resulting report xml file: // ============================================================================================== // // <Rule Id="1"> // <Count>123</Count> // <Enabled>true</Enabled> // <Name>Finds classes</Name> // <Description>Finds all the class definitions.</Description> // <Severity>Critical</Severity> // <Expression>\s*public\s*class\s*</Expression> // <Link>SomeFile.xml</Link> // </Rule> // // ============================================================================================== XmlNode ruleNode = doc.CreateNode(XmlNodeType.Element, "Rule", null); XmlNode countNode = doc.CreateNode(XmlNodeType.Element, "Count", null); XmlNode enabledNode = doc.CreateNode(XmlNodeType.Element, "Enabled", null); XmlNode nameNode = doc.CreateNode(XmlNodeType.Element, "Name", null); XmlNode descriptionNode = doc.CreateNode(XmlNodeType.Element, "Description", null); XmlNode severityNode = doc.CreateNode(XmlNodeType.Element, "Severity", null); XmlNode expressionNode = doc.CreateNode(XmlNodeType.Element, "Expression", null); XmlNode linkNode = doc.CreateNode(XmlNodeType.Element, "Link", null); // Rule node... XmlAttribute idAttrib = doc.CreateAttribute("Id"); idAttrib.Value = ruleSummary.Id.ToString(); ruleNode.Attributes.Append(idAttrib); // Count node... countNode.InnerText = ruleSummary.Count.ToString(); // Enabled node... enabledNode.InnerText = ruleSummary.Enabled.ToString(); // Name node... XmlCDataSection cdataNameNode = doc.CreateCDataSection(ruleSummary.Name); nameNode.AppendChild(cdataNameNode); // Description node... XmlCDataSection cdataDescriptionNode = doc.CreateCDataSection(ruleSummary.Description); descriptionNode.AppendChild(cdataDescriptionNode); // Expression node... XmlCDataSection cdataExpression = doc.CreateCDataSection(ruleSummary.Expression); expressionNode.AppendChild(cdataExpression); // Severity node... severityNode.InnerText = ruleSummary.Severity.ToString(); // Link node... linkNode.InnerText = CreateNameForMatchFile(category.Name, ruleSummary.Name); // Add all nodes to the 'node' given in the arguments. ruleNode.AppendChild(countNode); ruleNode.AppendChild(enabledNode); ruleNode.AppendChild(nameNode); ruleNode.AppendChild(descriptionNode); ruleNode.AppendChild(severityNode); ruleNode.AppendChild(expressionNode); ruleNode.AppendChild(linkNode); node.AppendChild(ruleNode); // Finally let's add the current rule node to the overall container of rule nodes. // Later in the process all the rule nodes will be persisted to a AllRules.xml file. ContainerOfRuleNodes.Add(ruleNode); }