/// <summary> /// Initializes a new instance of the Violation class. /// </summary> /// <param name="rule"> /// The rule that triggered the violation. /// </param> /// <param name="element"> /// The element that this violation appears in. /// </param> /// <param name="location"> /// The location in the source code where the violation occurs. /// </param> /// <param name="message"> /// The context message for the violation. /// </param> internal Violation(Rule rule, ICodeElement element, CodeLocation location, string message) { Param.AssertNotNull(rule, "rule"); Param.Ignore(element); Param.AssertNotNull(location, "location"); Param.AssertNotNull(message, "message"); this.rule = rule; this.element = element; // The CodeLocation passed in is zero based everywhere in StyleCop for the column. The line number is already 1 based. // We convert is to 1 based here so that are xml reports etc and VisualStudio UI friendly. this.location = new CodeLocation( location.StartPoint.Index, location.EndPoint.Index, location.StartPoint.IndexOnLine + 1, location.EndPoint.IndexOnLine + 1, location.StartPoint.LineNumber, location.EndPoint.LineNumber); // If the location has been passed in we set the linenumber. this.line = location.LineNumber; this.message = message; if (this.element != null && this.element.Document != null) { this.sourceCode = this.element.Document.SourceCode; } this.UpdateKey(); }
/// <summary> /// Initializes a new instance of the Violation class. /// </summary> /// <param name="rule">The rule that triggered the violation.</param> /// <param name="sourceCode">The source code that this violation appears in.</param> /// <param name="line">The line in the source code where the violation occurs.</param> /// <param name="message">The context message for the violation.</param> internal Violation(Rule rule, SourceCode sourceCode, int line, string message) { Param.AssertNotNull(rule, "rule"); Param.Ignore(sourceCode); Param.AssertGreaterThanOrEqualToZero(line, "line"); Param.AssertNotNull(message, "message"); this.rule = rule; this.sourceCode = sourceCode; this.line = line; this.message = message; }
/// <summary> /// Initializes a new instance of the Violation class. /// </summary> /// <param name="rule">The rule that triggered the violation.</param> /// <param name="element">The element that this violation appears in.</param> /// <param name="line">The line in the source code where the violation occurs.</param> /// <param name="message">The context message for the violation.</param> internal Violation(Rule rule, ICodeElement element, int line, string message) { Param.AssertNotNull(rule, "rule"); Param.Ignore(element); Param.AssertGreaterThanOrEqualToZero(line, "line"); Param.AssertNotNull(message, "message"); this.rule = rule; this.element = element; this.line = line; this.message = message; if (this.element != null && this.element.Document != null) { this.sourceCode = this.element.Document.SourceCode; } }
/// <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="type">The type of violation to add.</param> /// <param name="location">The location the violation appears on.</param> /// <param name="values">The string values to add to the context string.</param> internal void AddViolation(ICodeElement element, Rule type, CodeLocation location, params object[] values) { Param.Ignore(element); Param.AssertNotNull(type, "type"); Param.AssertNotNull(location, "location"); 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, element, location, message.ToString()); // Finally, add the violation. this.AddViolation(element, violation); }
/// <summary> /// Initializes the checked state of the given rule. /// </summary> /// <param name="rule"> /// The rule to check. /// </param> /// <param name="ruleNode"> /// The node representing the rule. /// </param> private void InitializeRuleCheckedState(Rule rule, TreeNode ruleNode) { Param.AssertNotNull(rule, "rule"); Param.AssertNotNull(ruleNode, "ruleNode"); // Extract the analyzer from the parent of this rule. SourceAnalyzer analyzer = null; TreeNode analyzerNode = ruleNode.Parent; while (analyzerNode != null) { analyzer = analyzerNode.Tag as SourceAnalyzer; if (analyzer != null) { break; } analyzerNode = analyzerNode.Parent; } Debug.Assert(analyzer != null, "The rule node does not have a parent analyzer node."); BooleanProperty enabledDisabledSetting = analyzer.GetRuleSetting(this.tabControl.MergedSettings, rule.Name, "Enabled") as BooleanProperty; if (enabledDisabledSetting == null) { ruleNode.Checked = rule.EnabledByDefault; } else { ruleNode.Checked = enabledDisabledSetting.Value; } }
/// <summary> /// Determines whether the given rule is suppressed for the given element. /// </summary> /// <param name="element">The element to check.</param> /// <param name="rule">The rule to check.</param> /// <returns>Returns true is the rule is suppressed; otherwise false.</returns> public override bool IsRuleSuppressed(ICodeElement element, Rule rule) { Param.Ignore(element, rule); if (element != null && rule != null) { // If the lock throws we are okay with it unwinding this.suppressionsLock.AcquireReaderLock(Timeout.Infinite); try { // First, check whether the entire rule namespace is suppressed for this element. if (this.IsRuleSuppressed(element, rule.UniqueRuleNamespaceId)) { return true; } // Now determine whether the specific rule is suppressed. if (this.IsRuleSuppressed(element, rule.UniqueRuleId)) { return true; } } finally { this.suppressionsLock.ReleaseReaderLock(); } } return false; }
/// <summary> /// Determines whether the given rule is suppressed for the given element. /// </summary> /// <param name="element">The element to check.</param> /// <param name="rule">The rule to check.</param> /// <returns>Returns true is the rule is suppressed; otherwise false.</returns> public override bool IsRuleSuppressed(ICodeElement element, Rule rule) { Param.Ignore(element, rule); return this.parser.IsRuleSuppressed(element, rule); }
/// <summary> /// Parses the given Xml document and loads the rules. /// </summary> /// <param name="rulesNode">The rules node.</param> /// <param name="ruleGroup">The optional rule group name.</param> /// <param name="isKnownAssembly">Indicates whether the add-in comes from a known assembly.</param> private void AddRulesFromXml(XmlNode rulesNode, string ruleGroup, bool isKnownAssembly) { Param.AssertNotNull(rulesNode, "rulesNode"); Param.Ignore(ruleGroup); Param.Ignore(isKnownAssembly); foreach (XmlNode rule in rulesNode.ChildNodes) { // Look for the Rule nodes. if (rule.Name == "RuleGroup") { // This is a rule group containing child rules. Extract the name of the group. XmlAttribute groupName = rule.Attributes["Name"]; if (groupName == null || groupName.Value.Length == 0) { throw new ArgumentException(Strings.RuleGroupHasNoNameAttribute); } // Extract the rules under this group. this.AddRulesFromXml(rule, groupName.Value, isKnownAssembly); } else if (rule.Name == "Rule") { // Get the name, check-id, and context and throw an exception if any of these doesn't exist. XmlNode ruleName = rule.Attributes["Name"]; if (ruleName == null || ruleName.Value.Length == 0) { throw new ArgumentException(Strings.RuleHasNoNameAttribute); } XmlNode ruleCheckId = rule.Attributes["CheckId"]; if (ruleCheckId == null || ruleCheckId.Value.Length == 0) { throw new ArgumentException(string.Format( CultureInfo.CurrentCulture, Strings.RuleHasNoCheckIdAttribute, ruleName.Value)); } // Validate the check-id to determine whether it uses the default prefix code. if (ruleCheckId.Value.StartsWith(DefaultCheckIdPrefix, StringComparison.Ordinal) && !isKnownAssembly) { throw new ArgumentException( string.Format(CultureInfo.CurrentCulture, Strings.UnknownAssemblyUsingDefaultCheckIdPrefix, ruleCheckId.Value)); } XmlNode ruleContext = rule["Context"]; if (ruleContext == null || ruleContext.InnerText.Length == 0) { throw new ArgumentException( string.Format(CultureInfo.CurrentCulture, Strings.RuleHasNoContextElement, ruleName.Value)); } string context = TrimXmlContent(ruleContext.InnerText); if (string.IsNullOrEmpty(context)) { throw new ArgumentException(string.Format( CultureInfo.CurrentCulture, Strings.RuleHasNoContextElement, ruleName.Value)); } // Get the optional description node. XmlNode ruleDescription = rule["Description"]; // Get the optional warning attribute. XmlAttribute warning = rule.Attributes["Warning"]; // Get the optional disabledByDefault attribute. XmlAttribute disabledByDefault = rule.Attributes["DisabledByDefault"]; // Get the optional can disable attribute. XmlAttribute canDisable = rule.Attributes["CanDisable"]; // Disabled by default defaults to false. bool disabledByDefaultValue = disabledByDefault == null ? false : Convert.ToBoolean(disabledByDefault.Value, CultureInfo.InvariantCulture); // Can disable defaults to true. bool canDisableValue = canDisable == null ? true : Convert.ToBoolean(canDisable.Value, CultureInfo.InvariantCulture); // Warning defaults to false. bool warningValue = warning == null ? false : Convert.ToBoolean(warning.Value, CultureInfo.InvariantCulture); // Add the rule. if (this.rules.ContainsKey(ruleName.Value)) { throw new ArgumentException(Strings.RuleWithSameNameExists); } Rule type = new Rule( ruleName.Value, this.id, ruleCheckId.Value, context, warningValue, ruleDescription == null ? string.Empty : TrimXmlContent(ruleDescription.InnerText), ruleGroup, !disabledByDefaultValue, canDisableValue); this.rules.Add(ruleName.Value, type); } } }
/// <summary> /// Gets a value indicating whether the given rule is suppressed for the given element. /// </summary> /// <param name="element">The element to check.</param> /// <param name="rule">The rule to check.</param> /// <returns>Returns true if the rule is suppressed; otherwise false.</returns> public virtual bool IsRuleSuppressed(ICodeElement element, Rule rule) { Param.Ignore(element, rule); return false; }
/// <summary> /// Determines whether the given rule is suppressed for the given element. /// </summary> /// <param name="element">The element to check.</param> /// <param name="rule">The rule to check.</param> /// <returns>Returns true is the rule is suppressed; otherwise false.</returns> public override bool IsRuleSuppressed(ICodeElement element, Rule rule) { Param.Ignore(element, rule); return(this.parser.IsRuleSuppressed(element, rule)); }
/// <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> /// Initializes a new instance of the Violation class. /// </summary> /// <param name="rule"> /// The rule that triggered the violation. /// </param> /// <param name="element"> /// The element that this violation appears in. /// </param> /// <param name="line"> /// The line in the source code where the violation occurs. /// </param> /// <param name="message"> /// The context message for the violation. /// </param> internal Violation(Rule rule, ICodeElement element, int line, string message) { Param.AssertNotNull(rule, "rule"); Param.Ignore(element); Param.AssertGreaterThanOrEqualToZero(line, "line"); Param.AssertNotNull(message, "message"); this.rule = rule; this.element = element; this.line = line; // As the line only is passed in we ensure the location is null. // A null location indicates we only know the line it was on. this.location = null; this.message = message; if (this.element != null && this.element.Document != null) { this.sourceCode = this.element.Document.SourceCode; } this.UpdateKey(); }