Example #1
0
        public XmlDocument WriteSettingsToDocument(StyleCopEnvironment environment)
        {
            Param.RequireNotNull(environment, "environment");

            // Create a new document for the settings.
            XmlDocument document = WritableSettings.NewDocument();

            this.SaveSettingsIntoXmlDocument(document, environment, document.DocumentElement, this);

            return(document);
        }
Example #2
0
        /// <summary>
        /// Creates an empty settings file at the given path.
        /// </summary>
        /// <param name="path">The path to the document to create.</param>
        /// <param name="exception">If the document could not be created, this returns the
        /// resulting exception information.</param>
        /// <returns>Returns the document if it was successfully saved.</returns>
        private WritableSettings CreateSettingsDocument(string path, out Exception exception)
        {
            Param.AssertValidString(path, "path");

            exception = null;

            try
            {
                XmlDocument document = WritableSettings.NewDocument();
                document.Save(path);

                // Get the last write time for the file.
                DateTime writeTime = File.GetLastWriteTime(path);

                return(new WritableSettings(this.Core, path, Path.GetDirectoryName(path), document, writeTime));
            }
            catch (ArgumentException argex)
            {
                exception = argex;
            }
            catch (IOException ioex)
            {
                exception = ioex;
            }
            catch (SecurityException secex)
            {
                exception = secex;
            }
            catch (UnauthorizedAccessException unauthex)
            {
                exception = unauthex;
            }
            catch (XmlException xmlex)
            {
                exception = xmlex;
            }

            return(null);
        }
Example #3
0
        public XmlDocument WriteSettingsToDocument(StyleCopEnvironment environment)
        {
            Param.RequireNotNull(environment, "environment");

            // Create a new document for the settings.
            XmlDocument document = WritableSettings.NewDocument();

            // Get the parent settings if there are any.
            SettingsMerger merger         = new SettingsMerger(this, environment);
            Settings       parentSettings = merger.ParentMergedSettings;

            // Add the global settings if there are any.
            if (this.GlobalSettings != null && this.GlobalSettings.Count > 0)
            {
                // Get the global settings from the parent.
                PropertyCollection parentGlobalSettings = null;
                if (parentSettings != null)
                {
                    parentGlobalSettings = parentSettings.GlobalSettings;
                }

                SavePropertyCollection(document.DocumentElement, "GlobalSettings", this.GlobalSettings, parentGlobalSettings, true, null);
            }

            // Add the parser settings if there are any.
            if (this.ParserSettings.Count > 0)
            {
                bool       parserSettingsAdded = false;
                XmlElement parsersNode         = document.CreateElement("Parsers");

                foreach (AddInPropertyCollection parserSettings in this.ParserSettings)
                {
                    // Add the settings for this parser if there are any.
                    if (parserSettings.Count > 0)
                    {
                        // Create a node for this parser.
                        XmlElement   parserNode        = document.CreateElement("Parser");
                        XmlAttribute parserIdAttribute = document.CreateAttribute("ParserId");
                        parserIdAttribute.Value = parserSettings.AddIn.Id;
                        parserNode.Attributes.Append(parserIdAttribute);

                        // Get the parser settings from the parent.
                        PropertyCollection parentParserSettings = null;
                        if (parentSettings != null)
                        {
                            parentParserSettings = parentSettings.GetAddInSettings(parserSettings.AddIn);
                        }

                        if (SavePropertyCollection(parserNode, "ParserSettings", parserSettings, parentParserSettings, true, null))
                        {
                            parsersNode.AppendChild(parserNode);
                            parserSettingsAdded = true;
                        }
                    }
                }

                if (parserSettingsAdded)
                {
                    document.DocumentElement.AppendChild(parsersNode);
                }
            }

            // Add the analyzer settings if there are any.
            if (this.AnalyzerSettings.Count > 0)
            {
                bool       analyzerSettingsAdded = false;
                XmlElement analyzersNode         = document.CreateElement("Analyzers");

                foreach (AddInPropertyCollection analyzerSettings in this.AnalyzerSettings)
                {
                    // Add the settings for this analyzer if there are any.
                    if (analyzerSettings.Count > 0)
                    {
                        // Create a node for this analzyer.
                        XmlElement   analyzerNode        = document.CreateElement("Analyzer");
                        XmlAttribute analyzerIdAttribute = document.CreateAttribute("AnalyzerId");
                        analyzerIdAttribute.Value = analyzerSettings.AddIn.Id;
                        analyzerNode.Attributes.Append(analyzerIdAttribute);

                        // Get the analyzer settings from the parent.
                        PropertyCollection parentAnalyzerSettings = null;
                        if (parentSettings != null)
                        {
                            parentAnalyzerSettings = parentSettings.GetAddInSettings(analyzerSettings.AddIn);
                        }

                        if (SavePropertyCollection(analyzerNode, "AnalyzerSettings", analyzerSettings, parentAnalyzerSettings, true, null))
                        {
                            analyzersNode.AppendChild(analyzerNode);
                            analyzerSettingsAdded = true;
                        }
                    }
                }

                if (analyzerSettingsAdded)
                {
                    document.DocumentElement.AppendChild(analyzersNode);
                }
            }

            return(document);
        }