示例#1
0
        /// <summary>
        /// Populates collections of exemptions from the XML of additional file(s).
        /// </summary>
        /// <param name="rootElement">
        /// Root node of an XML document containing exemptions for fields, string, files, assemblies, types, members, and namespaces.
        /// </param>
        protected override void LoadConfigurations(XElement rootElement, string filePath)
        {
            if (TryGetRootElementDiagnostic(rootElement, "Exemptions", filePath, StringsShouldBeInResourcesAnalyzer.FileParseRule, out var diagnostic))
            {
                _additionalFileService.ParsingDiagnostics.Add(diagnostic);
            }

            _exemptStrings.UnionWith(rootElement.Elements("String"));
            _exemptFilenames.UnionWith(rootElement.Elements("Filename"));
            _exemptAssemblies.UnionWith(rootElement.Elements("Assembly"));
            _exemptNamespaces.UnionWith(rootElement.Elements("Namespace"));

            LoadScopeAndOrInvocationExemptions(rootElement.Elements("Field"), _exemptFieldScopes, _exemptFields);
            LoadScopeAndOrInvocationExemptions(rootElement.Elements("Type"), _exemptTypeScopes, _exemptTypes);
            LoadScopeAndOrInvocationExemptions(rootElement.Elements("Member"), _exemptMemberScopes, _exemptMembers);
        }
示例#2
0
        private static void LoadScopeAndOrInvocationExemptions(
            IEnumerable <XElement> exemptionNodes,
            ExemptionCollection scopeExemptions,
            ExemptionCollection invocationExemptions)
        {
            // Type and member exemptions will, by default, apply to both scopes and invocations.
            // This method allows users to specify an "AppliesTo" attribute to control whether an
            // exemption applies to only one or the other.

            foreach (var exemption in exemptionNodes)
            {
                var appliesAttribute = exemption.Attribute("AppliesTo");
                if (appliesAttribute != null)
                {
                    var attributePairs = exemption.Attributes()
                                         .Where(x => x != appliesAttribute)
                                         .Select(x => Tuple.Create(x.Name.LocalName, x.Value)).ToArray();

                    AttributeCollection attributes = null;
                    if (attributePairs.Length > 0)
                    {
                        attributes = new AttributeCollection(attributePairs);
                    }

                    if (string.Equals(appliesAttribute.Value, "Scope", StringComparison.OrdinalIgnoreCase))
                    {
                        scopeExemptions.Add(exemption.Value, attributes);
                    }
                    else
                    {
                        invocationExemptions.Add(exemption.Value, attributes);
                    }
                }
                else
                {
                    var exemptions = new[] { exemption };

                    scopeExemptions.UnionWith(exemptions);
                    invocationExemptions.UnionWith(exemptions);
                }
            }
        }