/// <summary> /// Gets the settings given the path to the local settings. /// </summary> /// <param name="settingsPath">The path to the settings to load.</param> /// <param name="merge">Indicates whether to merge the settings with parent settings before returning them.</param> /// <param name="exception">Returns an exception if one occured while loading the settings.</param> /// <returns>Returns the settings.</returns> public override Settings GetSettings(string settingsPath, bool merge, out Exception exception) { Param.RequireValidString(settingsPath, "settingsPath"); Param.Ignore(merge); Param.Ignore(merge); // Load the settings file. Settings settings = this.LoadSettingsDocument(settingsPath, true, out exception); if (merge) { // If there are no local settings, create an empty settings file pointing // at the location where we expected the local settings to be. This // will allow us to do a parent merge from this location. if (settings == null) { settings = new Settings(this.Core, settingsPath, Path.GetDirectoryName(settingsPath)); } // Merge the file and return it. SettingsMerger merger = new SettingsMerger(settings, this); settings = merger.MergedSettings; } return(settings); }
/// <summary> /// Loads the settings files to use for the analysis. /// </summary> /// <param name="projects"> /// The list of projects to use. /// </param> private void LoadSettingsFiles(IList <CodeProject> projects) { Param.AssertNotNull(projects, "projects"); Settings mergedSettings = null; // Load the local settings without merging. Settings localSettings = null; if (this.settingsPath != null) { localSettings = this.Core.Environment.GetSettings(this.settingsPath, false); if (localSettings != null) { // Merge the local settings. SettingsMerger merger = new SettingsMerger(localSettings, this.Core.Environment); mergedSettings = merger.MergedSettings; } } foreach (CodeProject project in projects) { Settings settingsToUse = mergedSettings; if (settingsToUse == null) { settingsToUse = this.Core.Environment.GetProjectSettings(project, true); } if (settingsToUse != null) { project.Settings = settingsToUse; project.SettingsLoaded = true; } } }
/// <summary> /// Called when the parent settings have changed. /// </summary> public void RefreshMergedSettings() { // Set the contents of the parent settings file. SettingsMerger merger = new SettingsMerger(this.localSettings, this.core.Environment); this.parentSettings = merger.ParentMergedSettings; this.mergedSettings = merger.MergedSettings; // Set up the settings comparer. this.settingsComparer = new SettingsComparer(this.localSettings, this.parentSettings); for (int i = 0; i < this.pageInterfaces.Count; ++i) { if (this.pageInterfaces[i] != null) { this.pageInterfaces[i].RefreshSettingsOverrideState(); } } }
/// <summary> /// Gets the settings given the path to the local settings. /// </summary> /// <param name="settingsPath">The path to the settings to load.</param> /// <param name="merge">Indicates whether to merge the settings with parent settings before returning them.</param> /// <param name="exception">Returns an exception if one occured while loading the settings.</param> /// <returns>Returns the settings.</returns> public override Settings GetSettings(string settingsPath, bool merge, out Exception exception) { Param.RequireValidString(settingsPath, "settingsPath"); Param.Ignore(merge); Param.Ignore(merge); // Load the settings file. Settings settings = this.LoadSettingsDocument(settingsPath, true, out exception); if (merge) { // If there are no local settings, create an empty settings file pointing // at the location where we expected the local settings to be. This // will allow us to do a parent merge from this location. if (settings == null) { settings = new Settings(this.Core, settingsPath, Path.GetDirectoryName(settingsPath)); } // Merge the file and return it. SettingsMerger merger = new SettingsMerger(settings, this); settings = merger.MergedSettings; } return settings; }
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; }
/// <summary> /// The control must be initialized by calling this method during the host's OnLoad event. /// </summary> /// <param name="hostInstance">Interface implemented by the host object.</param> /// <param name="propertyPages">The array of pages to display on the tab control.</param> /// <param name="settings">The settings to read from and write to.</param> /// <param name="coreInstance">The StyleCop core instance.</param> /// <param name="contextItem">The context for the property control.</param> internal void Initialize( IPropertyControlHost hostInstance, IList <IPropertyControlPage> propertyPages, WritableSettings settings, StyleCopCore coreInstance, params object[] contextItem) { Param.AssertNotNull(hostInstance, "hostInstance"); Param.Assert(propertyPages != null && propertyPages.Count > 0, "propertyPages", "Cannot be null or empty"); Param.AssertNotNull(settings, "settings"); Param.AssertNotNull(coreInstance, "coreInstance"); Param.Ignore(contextItem); // Make sure we haven't already been intialized. if (this.host != null) { throw new StyleCopException(Strings.PropertyControlAlreadyInitialized); } this.host = hostInstance; this.pageInterfaces = propertyPages; this.localSettings = settings; this.core = coreInstance; this.context = contextItem; // Set the contents of the parent settings file. SettingsMerger merger = new SettingsMerger(this.localSettings, this.core.Environment); this.parentSettings = merger.ParentMergedSettings; this.mergedSettings = merger.MergedSettings; // Set up the settings comparer. this.settingsComparer = new SettingsComparer(this.localSettings, this.parentSettings); // Make sure the context is non-null. if (this.context == null) { this.context = new object[] { }; } this.tabPages = new TabPage[propertyPages.Count]; this.pages = new UserControl[propertyPages.Count]; // Add each of the property pages. int pageCount = 0; // Initialize the settings pages. for (int i = 0; i < propertyPages.Count; ++i) { this.pages[pageCount] = (UserControl)this.pageInterfaces[i]; TabPage tabPage = new TabPage(this.pageInterfaces[i].TabName); this.tabPages[pageCount] = tabPage; tabPage.Controls.Add(this.pages[i]); this.Controls.Add(tabPage); this.pages[i].Dock = DockStyle.Fill; this.SizePage(i); // The first page has already been initialized. this.pageInterfaces[i].Initialize(this); ++pageCount; } // Activate the first page. if (this.TabPages[0] != null) { this.SelectedTab = this.tabPages[0]; this.pageInterfaces[0].Activate(true); } this.SizeChanged += new System.EventHandler(this.OnSizeChanged); }
/// <summary> /// The control must be initialized by calling this method during the host's OnLoad event. /// </summary> /// <param name="hostInstance"> /// Interface implemented by the host object. /// </param> /// <param name="propertyPages"> /// The array of pages to display on the tab control. /// </param> /// <param name="settings"> /// The settings to read from and write to. /// </param> /// <param name="coreInstance"> /// The StyleCop core instance. /// </param> /// <param name="contextItem"> /// The context for the property control. /// </param> internal void Initialize( IPropertyControlHost hostInstance, IList<IPropertyControlPage> propertyPages, WritableSettings settings, StyleCopCore coreInstance, params object[] contextItem) { Param.AssertNotNull(hostInstance, "hostInstance"); Param.Assert(propertyPages != null && propertyPages.Count > 0, "propertyPages", "Cannot be null or empty"); Param.AssertNotNull(settings, "settings"); Param.AssertNotNull(coreInstance, "coreInstance"); Param.Ignore(contextItem); // Make sure we haven't already been intialized. if (this.host != null) { throw new StyleCopException(Strings.PropertyControlAlreadyInitialized); } this.host = hostInstance; this.pageInterfaces = propertyPages; this.localSettings = settings; this.core = coreInstance; this.context = contextItem; // Set the contents of the parent settings file. SettingsMerger merger = new SettingsMerger(this.localSettings, this.core.Environment); this.parentSettings = merger.ParentMergedSettings; this.mergedSettings = merger.MergedSettings; // Set up the settings comparer. this.settingsComparer = new SettingsComparer(this.localSettings, this.parentSettings); // Make sure the context is non-null. if (this.context == null) { this.context = new object[] { }; } this.tabPages = new TabPage[propertyPages.Count]; this.pages = new UserControl[propertyPages.Count]; // Add each of the property pages. int pageCount = 0; // Initialize the settings pages. for (int i = 0; i < propertyPages.Count; ++i) { this.pages[pageCount] = (UserControl)this.pageInterfaces[i]; TabPage tabPage = new TabPage(this.pageInterfaces[i].TabName); this.tabPages[pageCount] = tabPage; tabPage.Controls.Add(this.pages[i]); this.Controls.Add(tabPage); this.pages[i].Dock = DockStyle.Fill; this.SizePage(i); // The first page has already been initialized. this.pageInterfaces[i].Initialize(this); ++pageCount; } // Activate the first page. if (this.TabPages[0] != null) { this.SelectedTab = this.tabPages[0]; this.pageInterfaces[0].Activate(true); } this.SizeChanged += this.OnSizeChanged; }
/// <summary> /// Saves the Settings provided into the XmlDocument. /// </summary> /// <param name="document"> /// The root document. /// </param> /// <param name="environment"> /// The environment that StyleCop is running under, if any. /// </param> /// <param name="rootElement"> /// The element to save our settings to. /// </param> /// <param name="settingsToSave"> /// The settings to save. /// </param> private void SaveSettingsIntoXmlDocument(XmlDocument document, StyleCopEnvironment environment, XmlElement rootElement, Settings settingsToSave) { // Get the parent settings if there are any. SettingsMerger merger = new SettingsMerger(settingsToSave, environment); Settings parentSettings = merger.ParentMergedSettings; // Add the global settings if there are any. if (settingsToSave.GlobalSettings != null && settingsToSave.GlobalSettings.Count > 0) { // Get the global settings from the parent. PropertyCollection parentGlobalSettings = null; if (parentSettings != null) { parentGlobalSettings = parentSettings.GlobalSettings; } SavePropertyCollection(rootElement, "GlobalSettings", settingsToSave.GlobalSettings, parentGlobalSettings, true, null); } // Add the parser settings if there are any. if (settingsToSave.ParserSettings.Count > 0) { bool parserSettingsAdded = false; XmlElement parsersNode = document.CreateElement("Parsers"); foreach (AddInPropertyCollection parserSettings in settingsToSave.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) { rootElement.AppendChild(parsersNode); } } // Add the analyzer settings if there are any. if (settingsToSave.AnalyzerSettings.Count > 0) { bool analyzerSettingsAdded = false; XmlElement analyzersNode = document.CreateElement("Analyzers"); foreach (AddInPropertyCollection analyzerSettings in settingsToSave.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) { rootElement.AppendChild(analyzersNode); } } // Add the sourcefilelists settings if there are any. if (settingsToSave.SourceFileLists.Count > 0) { foreach (SourceFileListSettings sourceFileListSettings in settingsToSave.SourceFileLists) { XmlElement sourceFileListNode = document.CreateElement("SourceFileList"); foreach (string sourceFileListSetting in sourceFileListSettings.SourceFiles) { XmlElement sourceFileNode = document.CreateElement("SourceFile"); sourceFileNode.InnerText = sourceFileListSetting; sourceFileListNode.AppendChild(sourceFileNode); } XmlElement settingsNode = document.CreateElement("Settings"); this.SaveSettingsIntoXmlDocument(document, environment, settingsNode, sourceFileListSettings.Settings); sourceFileListNode.AppendChild(settingsNode); rootElement.AppendChild(sourceFileListNode); } } }
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); }
/// <summary> /// Start processing /// </summary> /// <param name="projects">Projects to analyze</param> /// <returns>Indicates if errors were encountered</returns> public bool Start(IList<CodeProject> projects) { Param.RequireNotNull(projects, "projects"); bool error = false; string errorMessage = null; try { Settings mergedSettings = null; Settings localSettings = null; if (this.settingsPath != null) { localSettings = this.Core.Environment.GetSettings(this.settingsPath, false); if (localSettings != null) { SettingsMerger merger = new SettingsMerger(localSettings, this.Core.Environment); mergedSettings = merger.MergedSettings; } } foreach (CodeProject project in projects) { Settings settingsToUse = mergedSettings; if (settingsToUse == null) { settingsToUse = this.Core.Environment.GetProjectSettings(project, true); } if (settingsToUse != null) { project.Settings = settingsToUse; project.SettingsLoaded = true; } } this.Reset(); // Delete the output file if it already exists. if (!string.IsNullOrEmpty(this.outputFile)) { this.Core.Environment.RemoveAnalysisResults(this.outputFile); } this.Core.ViolationEncountered += this.Violation; using (var outputStream = new System.IO.StreamWriter(this.outputFile)) { this.write = outputStream; this.Core.FullAnalyze(projects); if (this.violationReported) { outputStream.WriteLine("</StyleCopViolations>"); } else { outputStream.WriteLine("<StyleCopViolations />"); } } } catch (System.IO.IOException ioex) { errorMessage = ioex.Message; error = true; } catch (System.Security.SecurityException secex) { errorMessage = secex.Message; error = true; } catch (UnauthorizedAccessException unauthex) { errorMessage = unauthex.Message; error = true; } if (error) { this.OnOutputGenerated(new OutputEventArgs(string.Format(CultureInfo.CurrentCulture, "Analysis error: {0}", errorMessage))); } return !error; }
/// <summary> /// Loads the settings files to use for the analysis. /// </summary> /// <param name="projects"> /// The list of projects to use. /// </param> private void LoadSettingsFiles(IList<CodeProject> projects) { Param.AssertNotNull(projects, "projects"); Settings mergedSettings = null; // Load the local settings without merging. Settings localSettings = null; if (this.settingsPath != null) { localSettings = this.Core.Environment.GetSettings(this.settingsPath, false); if (localSettings != null) { // Merge the local settings. SettingsMerger merger = new SettingsMerger(localSettings, this.Core.Environment); mergedSettings = merger.MergedSettings; } } foreach (CodeProject project in projects) { Settings settingsToUse = mergedSettings; if (settingsToUse == null) { settingsToUse = this.Core.Environment.GetProjectSettings(project, true); } if (settingsToUse != null) { project.Settings = settingsToUse; project.SettingsLoaded = true; } } }