public void AddViolation(Violation violation) { Param.RequireNotNull(violation, "violation"); if (violation.Element != null) { base.Core.AddViolation(violation.Element, violation); } else if (violation.SourceCode != null) { base.Core.AddViolation(violation.SourceCode, violation); } else { base.Core.AddViolation((CodeElement) null, violation); } }
/// <summary> /// Adds one violation to this element. /// </summary> /// <param name="violation">The violation to add.</param> /// <returns>Returns false if there is already an identical violation in the element.</returns> internal bool AddViolation(Violation violation) { Param.AssertNotNull(violation, "violation"); string key = violation.Key; if (!this.violations.ContainsKey(key)) { this.violations.Add(violation.Key, violation); return true; } return false; }
/// <summary> /// Exports the contents of the given violation into the given xml node. /// </summary> /// <param name="violation">The violation to save.</param> /// <param name="violationsDocument">The xml document in which to store the violation information.</param> /// <param name="parentNode">The parent node within this xml document under which to store the violation information.</param> private static void ExportViolation(Violation violation, XmlDocument violationsDocument, XmlNode parentNode) { Param.AssertNotNull(violation, "violation"); Param.AssertNotNull(violationsDocument, "violationsDocument"); Param.AssertNotNull(parentNode, "parentNode"); // Create the root element for this violation. XmlElement item = violationsDocument.CreateElement("violation"); parentNode.AppendChild(item); // Create the namespace attribute. XmlAttribute nameSpace = violationsDocument.CreateAttribute("namespace"); nameSpace.Value = violation.Rule.Namespace; item.Attributes.Append(nameSpace); // Create the rule name attribute. XmlAttribute name = violationsDocument.CreateAttribute("rule"); name.Value = violation.Rule.Name; item.Attributes.Append(name); // Create the rule check-id attribute. XmlAttribute checkId = violationsDocument.CreateAttribute("ruleCheckId"); checkId.Value = violation.Rule.CheckId; item.Attributes.Append(checkId); // Create a child node for the violation content string. XmlElement context = violationsDocument.CreateElement("context"); context.InnerText = violation.Message; item.AppendChild(context); // Create a child node for the violation line number. XmlElement lineNumber = violationsDocument.CreateElement("line"); lineNumber.InnerText = violation.Line.ToString(CultureInfo.InvariantCulture); item.AppendChild(lineNumber); // Create a child node for the warning indicator. XmlElement warning = violationsDocument.CreateElement("warning"); warning.InnerText = violation.Rule.Warning.ToString(CultureInfo.InvariantCulture); item.AppendChild(warning); }
/// <summary> /// Imports the cached violations under the given node. /// </summary> /// <param name="sourceCode">The source code containing the violations.</param> /// <param name="parentNode">The parent xml node containing the list of violations.</param> /// <returns>Returns true if all the data was loaded successfully from the file.</returns> internal bool ImportViolations(SourceCode sourceCode, XmlNode parentNode) { Param.AssertNotNull(sourceCode, "sourceCode"); Param.AssertNotNull(parentNode, "parentNode"); bool success = true; try { XmlNodeList violations = parentNode.SelectNodes("violation"); if (violations != null && violations.Count > 0) { foreach (XmlNode violationNode in violations) { // Get the violation data from the xml node. XmlNode nameSpace = violationNode.SelectSingleNode("@namespace"); XmlNode ruleName = violationNode.SelectSingleNode("@rule"); XmlNode ruleCheckId = violationNode.SelectSingleNode("@ruleCheckId"); XmlNode context = violationNode.SelectSingleNode("context"); XmlNode lineNumber = violationNode.SelectSingleNode("line"); XmlNode warning = violationNode.SelectSingleNode("warning"); // Create a Rule object representing this data. Rule rule = new Rule( ruleName.InnerText, nameSpace.InnerText, ruleCheckId.InnerText, context.InnerText, Convert.ToBoolean(warning.InnerText, CultureInfo.InvariantCulture)); // Create a Violation object representing this data. Violation violation = new Violation( rule, sourceCode, Convert.ToInt32(lineNumber.InnerText, null), context.InnerText); this.AddViolation(violation); } } } catch (ArgumentException) { success = false; } catch (XmlException) { success = false; } catch (FormatException) { success = false; } catch (OverflowException) { success = false; } return success; }
/// <summary> /// Adds a generic violation. /// </summary> /// <param name="sourceCode">The source code document to add the violation to.</param> /// <param name="type">The type of violation to add.</param> /// <param name="line">Line the violation appears on.</param> /// <param name="values">The string values to add to the context string.</param> internal void AddViolation(SourceCode sourceCode, Rule type, int line, params object[] values) { Param.Ignore(sourceCode); Param.AssertNotNull(type, "type"); Param.AssertGreaterThanZero(line, "line"); Param.Ignore(values); // Build up the context string. StringBuilder message = new StringBuilder(); message.AppendFormat(CultureInfo.CurrentCulture, type.Context, values); // Create the violation object and add it to the list. Violation violation = new Violation(type, sourceCode, line, message.ToString()); // Finally, add the violation. this.AddViolation(sourceCode, violation); }
/// <summary> /// Adds a generic violation. /// </summary> /// <param name="element">The element to add the violation to.</param> /// <param name="violation">The violation to add to the element.</param> internal void AddViolation(ICodeElement element, Violation violation) { Param.Ignore(element, "element"); Param.AssertNotNull(violation, "violation"); // Add the violation to the element. if (element != null) { if (element.AddViolation(violation)) { this.OnViolationEncountered(new ViolationEventArgs(violation)); } } }
/// <summary> /// Adds a generic violation. /// </summary> /// <param name="sourceCode">The file to add the violation to.</param> /// <param name="violation">The violation to add to the element.</param> internal void AddViolation(SourceCode sourceCode, Violation violation) { Param.Ignore(sourceCode, "sourceCode"); Param.AssertNotNull(violation, "violation"); bool signal = true; // Add the violation to the file. if (sourceCode != null) { if (!sourceCode.AddViolation(violation)) { signal = false; } } // Signal that there is a new violation. if (signal) { this.OnViolationEncountered(new ViolationEventArgs(violation)); } }
internal bool ImportViolations(SourceCode sourceCode, XmlNode parentNode) { bool flag = true; try { XmlNodeList list = parentNode.SelectNodes("violation"); if ((list != null) && (list.Count > 0)) { foreach (XmlNode node in list) { XmlNode node2 = node.SelectSingleNode("@namespace"); XmlNode node3 = node.SelectSingleNode("@rule"); XmlNode node4 = node.SelectSingleNode("@ruleCheckId"); XmlNode node5 = node.SelectSingleNode("context"); XmlNode node6 = node.SelectSingleNode("line"); XmlNode node7 = node.SelectSingleNode("warning"); Rule rule = new Rule(node3.InnerText, node2.InnerText, node4.InnerText, node5.InnerText, Convert.ToBoolean(node7.InnerText, CultureInfo.InvariantCulture)); Violation violation = new Violation(rule, sourceCode, Convert.ToInt32(node6.InnerText, (IFormatProvider) null), node5.InnerText); this.AddViolation(violation); } } return flag; } catch (ArgumentException) { flag = false; } catch (XmlException) { flag = false; } catch (FormatException) { flag = false; } catch (OverflowException) { flag = false; } return flag; }
private static void ExportViolation(Violation violation, XmlDocument violationsDocument, XmlNode parentNode) { XmlElement newChild = violationsDocument.CreateElement("violation"); parentNode.AppendChild(newChild); XmlAttribute node = violationsDocument.CreateAttribute("namespace"); node.Value = violation.Rule.Namespace; newChild.Attributes.Append(node); XmlAttribute attribute2 = violationsDocument.CreateAttribute("rule"); attribute2.Value = violation.Rule.Name; newChild.Attributes.Append(attribute2); XmlAttribute attribute3 = violationsDocument.CreateAttribute("ruleCheckId"); attribute3.Value = violation.Rule.CheckId; newChild.Attributes.Append(attribute3); XmlElement element2 = violationsDocument.CreateElement("context"); element2.InnerText = violation.Message; newChild.AppendChild(element2); XmlElement element3 = violationsDocument.CreateElement("line"); element3.InnerText = violation.Line.ToString(CultureInfo.InvariantCulture); newChild.AppendChild(element3); XmlElement element4 = violationsDocument.CreateElement("warning"); element4.InnerText = violation.Rule.Warning.ToString(CultureInfo.InvariantCulture); newChild.AppendChild(element4); }
internal bool AddViolation(Violation violation) { string key = violation.Key; if (!this.violations.ContainsKey(key)) { this.violations.Add(violation.Key, violation); return true; } return false; }
/// <summary> /// Initializes a new instance of the ViolationEventArgs class. /// </summary> /// <param name="violation">The violation.</param> internal ViolationEventArgs(Violation violation) { Param.AssertNotNull(violation, "violation"); this.violation = violation; }
internal void AddViolation(SourceCode sourceCode, Rule type, int line, params object[] values) { StringBuilder builder = new StringBuilder(); builder.AppendFormat(CultureInfo.CurrentUICulture, type.Context, values); Violation violation = new Violation(type, sourceCode, line, builder.ToString()); this.AddViolation(sourceCode, violation); }
internal void AddViolation(SourceCode sourceCode, Violation violation) { bool flag = true; if ((sourceCode != null) && !sourceCode.AddViolation(violation)) { flag = false; } if (flag) { this.OnViolationEncountered(new ViolationEventArgs(violation)); } }
internal void AddViolation(CodeElement element, Violation violation) { if ((element != null) && element.AddViolation(violation)) { this.OnViolationEncountered(new ViolationEventArgs(violation)); } }