Esempio n. 1
0
        /// <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;
            }
        }
Esempio n. 2
0
        /// <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);
                }
            }
        }
Esempio n. 3
0
 /// <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>
 /// Checks whether specified rule is suppressed.
 /// </summary>
 public override bool IsRuleSuppressed(ICodeElement element, Rule rule)
 {
     return false;
 }
 private void InitializeRuleCheckedState(Rule rule, TreeNode ruleNode)
 {
     SourceAnalyzer tag = null;
     for (TreeNode node = ruleNode.Parent; node != null; node = node.Parent)
     {
         tag = node.Tag as SourceAnalyzer;
         if (tag != null)
         {
             break;
         }
     }
     BooleanProperty property = tag.GetRuleSetting(this.tabControl.MergedSettings, rule.Name, "Enabled") as BooleanProperty;
     if (property == null)
     {
         ruleNode.Checked = rule.EnabledByDefault;
     }
     else
     {
         ruleNode.Checked = property.Value;
     }
 }
Esempio n. 6
0
        /// <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;
        }
Esempio n. 7
0
        /// <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);
        }
Esempio n. 8
0
 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;
 }
Esempio n. 9
0
        /// <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;
        }
Esempio n. 10
0
        /// <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;
        }
Esempio n. 11
0
        /// <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;
            }
        }
Esempio n. 12
0
 /// <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);
 }
Esempio n. 13
0
 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);
 }