public void SetAddInSetting(StyleCopAddIn addIn, PropertyValue property) { Param.RequireNotNull(addIn, "addIn"); Param.RequireNotNull(property, "property"); this.SetAddInSettingInternal(addIn, property); }
/// <summary> /// Saves a single property value. /// </summary> /// <param name="rootCollectionNode">The collection node containing the property.</param> /// <param name="property">The property to save.</param> /// <param name="propertyName">The name of the property.</param> /// <returns>Returns true if the property was saved; otherwise false.</returns> private static bool SavePropertyValue(XmlNode rootCollectionNode, PropertyValue property, string propertyName) { Param.AssertNotNull(rootCollectionNode, "rootCollectionNode"); Param.AssertNotNull(property, "property"); Param.AssertValidString(propertyName, "propertyName"); bool propertyWritten = false; switch (property.PropertyType) { case PropertyType.Boolean: propertyWritten |= SaveBooleanProperty(rootCollectionNode, property as BooleanProperty, propertyName); break; case PropertyType.Int: propertyWritten |= SaveIntProperty(rootCollectionNode, property as IntProperty, propertyName); break; case PropertyType.String: propertyWritten |= SaveStringProperty(rootCollectionNode, property as StringProperty, propertyName); break; case PropertyType.Collection: propertyWritten |= SaveCollectionProperty(rootCollectionNode, property as CollectionProperty, propertyName); break; default: Debug.Fail("Unexpected property type."); break; } return propertyWritten; }
/// <summary> /// Saves a rule property. /// </summary> /// <param name="rootNode">The node to store the property collection beneath.</param> /// <param name="property">The property to save.</param> /// <param name="ruleName">The name of the rule.</param> /// <param name="propertyName">The name of the property.</param> /// <returns>Returns true if the property was saved; otherwise false.</returns> private static bool SaveRuleProperty(XmlNode rootNode, PropertyValue property, string ruleName, string propertyName) { Param.AssertNotNull(rootNode, "rootNode"); Param.AssertNotNull(property, "property"); Param.AssertValidString(ruleName, "ruleName"); Param.AssertValidString(propertyName, "propertyName"); // Get or create the Rules node under the root. XmlNode rulesNode = rootNode.SelectSingleNode("Rules"); if (rulesNode == null) { rulesNode = rootNode.OwnerDocument.CreateElement("Rules"); rootNode.AppendChild(rulesNode); } // Get or create the Rule node. XmlNode ruleNode = rulesNode.SelectSingleNode("Rule[@Name=\"" + ruleName + "\"]"); if (ruleNode == null) { ruleNode = rootNode.OwnerDocument.CreateElement("Rule"); rulesNode.AppendChild(ruleNode); XmlAttribute attrib = rootNode.OwnerDocument.CreateAttribute("Name"); attrib.Value = ruleName; ruleNode.Attributes.Append(attrib); } // Get or create the RuleSettings node. XmlNode ruleSettings = ruleNode.SelectSingleNode("RuleSettings"); if (ruleSettings == null) { ruleSettings = rootNode.OwnerDocument.CreateElement("RuleSettings"); ruleNode.AppendChild(ruleSettings); } // Save the setting. return SavePropertyValue(ruleSettings, property, propertyName); }
/// <summary> /// Determines whether the local property overrides the parent property. /// </summary> /// <param name="localProperty">The local property.</param> /// <param name="parentProperty">The parent property.</param> /// <returns>Returns true if the local property overrides the parent property.</returns> public static bool IsSettingOverwritten(PropertyValue localProperty, PropertyValue parentProperty) { Param.Ignore(localProperty, parentProperty); // If either the parent property or the local property is not set, then the setting is not overwritten. if (parentProperty == null || localProperty == null) { return false; } // Ensure that the two properties are the same kind of property. if (localProperty.PropertyType != parentProperty.PropertyType) { throw new ArgumentException(Strings.ComparingDifferentPropertyTypes); } return localProperty.OverridesProperty(parentProperty); }
/// <summary> /// Merges two collection properties together. /// </summary> /// <param name="mergedPropertyCollection"> /// The merged property collection. /// </param> /// <param name="originalProperty"> /// The original property to merge. /// </param> /// <param name="overridingProperty"> /// The overriding property to merge. /// </param> private static void MergeCollectionProperties(PropertyCollection mergedPropertyCollection, PropertyValue originalProperty, PropertyValue overridingProperty) { Param.AssertNotNull(mergedPropertyCollection, "mergedPropertyCollection"); Param.AssertNotNull(originalProperty, "originalProperty"); Param.AssertNotNull(overridingProperty, "overridingProperty"); CollectionProperty originalCollectionProperty = (CollectionProperty)originalProperty; CollectionProperty overridingCollectionProperty = (CollectionProperty)overridingProperty; // Create a new merged collection property. CollectionProperty mergedCollectionProperty = new CollectionProperty((CollectionPropertyDescriptor)originalCollectionProperty.PropertyDescriptor); mergedPropertyCollection.Add(mergedCollectionProperty); // Add each of the strings from the overriding collection. foreach (string value in overridingCollectionProperty.Values) { mergedCollectionProperty.Add(value); } // If necessary, also add the strings from the original collection. if (originalCollectionProperty.Aggregate) { foreach (string value in originalCollectionProperty.Values) { if (!mergedCollectionProperty.Contains(value)) { mergedCollectionProperty.Add(value); } } } }
/// <summary> /// Determines whether the given global setting overrides the parent setting. /// </summary> /// <param name="propertyName">The name of the property.</param> /// <param name="localProperty">The local value of the property.</param> /// <returns>Returns true if the given global setting overrides the parent setting.</returns> public bool IsGlobalSettingOverwritten(string propertyName, PropertyValue localProperty) { Param.RequireValidString(propertyName, "propertyName"); Param.Ignore(localProperty); if (this.parentSettings == null) { return false; } // Try to find this property in the parent settings file. PropertyValue parentProperty = this.parentSettings.GlobalSettings[propertyName]; if (parentProperty == null) { return false; } return IsSettingOverwritten(localProperty, parentProperty); }
public bool IsAddInSettingOverwritten(StyleCopAddIn addIn, string propertyName, PropertyValue localProperty) { Param.RequireNotNull(addIn, "addIn"); Param.RequireValidString(propertyName, "propertyName"); Param.Ignore(localProperty); if (this.parentSettings == null) { return false; } // Try to find this property in the parent settings file. PropertyValue parentProperty = null; PropertyCollection parentParserProperties = this.parentSettings.GetAddInSettings(addIn); if (parentParserProperties != null) { parentProperty = parentParserProperties[propertyName]; } if (parentProperty == null) { // If there is no parent setting, then the parent is set to the default. If the local setting // is not set to the default, then we consider that the local setting is overriding the parent setting. return !localProperty.HasDefaultValue || !localProperty.IsDefault; } // Compare the local and parent properties. return IsSettingOverwritten(localProperty, parentProperty); }
/// <summary> /// Sets the given property on the add-in. /// </summary> /// <param name="settings">The settings.</param> /// <param name="property">The property to set.</param> public void SetSetting(WritableSettings settings, PropertyValue property) { Param.RequireNotNull(settings, "settings"); Param.RequireNotNull(property, "property"); settings.SetAddInSetting(this, property); }
/// <summary> /// Determines whether this property overrides the given property. /// </summary> /// <param name="parentProperty">The parent property to compare with.</param> /// <returns>Returns true if this property overrides the given property.</returns> public abstract bool OverridesProperty(PropertyValue parentProperty);
/// <summary> /// Sets a setting for the given add-in. /// </summary> /// <param name="addIn"> /// The add-in. /// </param> /// <param name="property"> /// The setting property to set. /// </param> internal void SetAddInSettingInternal(StyleCopAddIn addIn, PropertyValue property) { Param.AssertNotNull(addIn, "addIn"); Param.AssertNotNull(property, "property"); AddInPropertyCollection properties = this.GetAddInSettings(addIn); if (properties == null) { properties = new AddInPropertyCollection(addIn); this.SetAddInSettings(properties); } properties.Add(property); }
/// <summary> /// Saves the given property collection. /// </summary> /// <param name="rootNode"> /// The node to store the property collection beneath. /// </param> /// <param name="nodeName"> /// The name of the new property collection node. /// </param> /// <param name="properties"> /// The property collection to store. /// </param> /// <param name="parentProperties"> /// The corresponding property collection from the parent settings, if any. /// </param> /// <param name="aggregate"> /// Indicates whether the collection is aggregated with the parent collection. /// </param> /// <param name="nodeNameAttribute"> /// An optional name attribute value for the new property node. /// </param> /// <returns> /// Returns true if at least one property was saved. /// </returns> private static bool SavePropertyCollection( XmlNode rootNode, string nodeName, PropertyCollection properties, PropertyCollection parentProperties, bool aggregate, string nodeNameAttribute) { Param.AssertNotNull(rootNode, "rootNode"); Param.AssertValidString(nodeName, "nodeName"); Param.AssertNotNull(properties, "properties"); Param.Ignore(parentProperties); Param.Ignore(aggregate); Param.Ignore(nodeNameAttribute); // If there are no properties in the collection, don't save anything. if (properties.Count == 0) { return(false); } bool propertyWritten = false; // Create the root node for this property collection. Debug.Assert(rootNode.OwnerDocument != null, "The root node has not been attached to a document."); XmlElement rootCollectionNode = rootNode.OwnerDocument.CreateElement(nodeName); if (!string.IsNullOrEmpty(nodeNameAttribute)) { XmlAttribute rootCollectionNodeNameAttribute = rootNode.OwnerDocument.CreateAttribute("Name"); rootCollectionNodeNameAttribute.Value = nodeNameAttribute; rootCollectionNode.Attributes.Append(rootCollectionNodeNameAttribute); } // Add each property in the collection. foreach (PropertyValue property in properties) { bool skip = false; PropertyValue parentProperty = null; if (parentProperties != null) { parentProperty = parentProperties[property.PropertyName]; // If the property also exists in the parent collection, determine whether the local property // is an override. If not, there is no need to add it. if (aggregate && parentProperty != null) { if (!SettingsComparer.IsSettingOverwritten(property, parentProperty)) { skip = true; } } else if (property.IsDefault) { skip = true; } } else if (property.IsDefault) { skip = true; } if (!skip) { // If the property is a rule setting, then add it under the rules section. int index = property.PropertyName.IndexOf('#'); if (index > 0) { propertyWritten |= SaveRuleProperty( rootNode, property, property.PropertyName.Substring(0, index), property.PropertyName.Substring(index + 1, property.PropertyName.Length - index - 1)); } else { // Just save the property value under this add-in's settings since it is // not a rule property. propertyWritten |= SavePropertyValue(rootCollectionNode, property, property.PropertyName); } } } // If at least one property was saved, add the property collection node into the document. if (propertyWritten) { rootNode.AppendChild(rootCollectionNode); } return(propertyWritten); }