/// <summary> /// Loads and stores a boolean property. /// </summary> /// <param name="propertyName">The name of the property to load.</param> /// <param name="propertyNode">The node containing the property.</param> /// <param name="properties">The collection in which to store the property.</param> /// <param name="propertyDescriptors">The collection of property descriptors.</param> private static void LoadBooleanProperty( string propertyName, XmlNode propertyNode, PropertyCollection properties, PropertyDescriptorCollection propertyDescriptors) { Param.AssertValidString(propertyName, "propertyName"); Param.AssertNotNull(propertyNode, "propertyNode"); Param.AssertNotNull(properties, "properties"); Param.AssertNotNull(propertyDescriptors, "propertyDescriptors"); // Skip corrupted properties. bool value; if (Boolean.TryParse(propertyNode.InnerText, out value)) { // Get the property descriptor. PropertyDescriptor <bool> descriptor = propertyDescriptors[propertyName] as PropertyDescriptor <bool>; if (descriptor != null) { // Create and add the property. properties.Add(new BooleanProperty(descriptor, value)); } } }
/// <summary> /// Loads and stores an integer property. /// </summary> /// <param name="propertyName">The name of the property to load.</param> /// <param name="propertyNode">The node containing the property.</param> /// <param name="properties">The collection in which to store the property.</param> /// <param name="propertyDescriptors">The collection of property descriptors.</param> private static void LoadIntProperty( string propertyName, XmlNode propertyNode, PropertyCollection properties, PropertyDescriptorCollection propertyDescriptors) { Param.AssertValidString(propertyName, "propertyName"); Param.AssertNotNull(propertyNode, "propertyNode"); Param.AssertNotNull(properties, "properties"); Param.AssertNotNull(propertyDescriptors, "propertyDescriptors"); // Skip corrupted properties. int value; if (int.TryParse(propertyNode.InnerText, NumberStyles.Any, CultureInfo.InvariantCulture, out value)) { // Get the property descriptor. PropertyDescriptor <int> descriptor = propertyDescriptors[propertyName] as PropertyDescriptor <int>; if (descriptor != null) { // Create and add the property. properties.Add(new IntProperty(descriptor, value)); } } }
/// <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> /// Clones the contents of the collection. /// </summary> /// <returns>Returns the cloned collection.</returns> public virtual PropertyCollection Clone() { PropertyCollection clone = new PropertyCollection(); foreach (KeyValuePair<string, PropertyValue> item in this.properties) { clone.Add(item.Value.Clone()); } return clone; }
/// <summary> /// Loads and stores a string property. /// </summary> /// <param name="propertyName"> /// The name of the property to load. /// </param> /// <param name="propertyNode"> /// The node containing the property. /// </param> /// <param name="properties"> /// The collection in which to store the property. /// </param> /// <param name="propertyDescriptors"> /// The collection of property descriptors. /// </param> private static void LoadStringProperty(string propertyName, XmlNode propertyNode, PropertyCollection properties, PropertyDescriptorCollection propertyDescriptors) { Param.AssertValidString(propertyName, "propertyName"); Param.AssertNotNull(propertyNode, "propertyNode"); Param.AssertNotNull(properties, "properties"); Param.AssertNotNull(propertyDescriptors, "propertyDescriptors"); // Get the property descriptor. PropertyDescriptor <string> descriptor = propertyDescriptors[propertyName] as PropertyDescriptor <string>; // Create and add the property. properties.Add(new StringProperty(descriptor, propertyNode.InnerText)); }
/// <summary> /// Adds a boolean property. /// </summary> /// <param name="propertyName"> /// The name of the property. /// </param> /// <param name="value"> /// The property value. /// </param> /// <param name="properties"> /// The collection of properties. /// </param> /// <param name="propertyDescriptors"> /// The collection of property descriptors. /// </param> private static void AddBooleanProperty(string propertyName, bool value, PropertyCollection properties, PropertyDescriptorCollection propertyDescriptors) { Param.AssertValidString(propertyName, "propertyName"); Param.Ignore(value); Param.AssertNotNull(properties, "properties"); Param.AssertNotNull(propertyDescriptors, "propertyDescriptors"); // Get the property descriptor. PropertyDescriptor <bool> descriptor = propertyDescriptors[propertyName] as PropertyDescriptor <bool>; if (descriptor != null) { // Create and add the property. properties.Add(new BooleanProperty(descriptor, value)); } }
/// <summary> /// Loads and stores a collection property. /// </summary> /// <param name="propertyName">The name of the property to load.</param> /// <param name="propertyNode">The node containing the property.</param> /// <param name="properties">The collection in which to store the property.</param> /// <param name="propertyDescriptors">The collection of property descriptors.</param> private static void LoadCollectionProperty( string propertyName, XmlNode propertyNode, PropertyCollection properties, PropertyDescriptorCollection propertyDescriptors) { Param.AssertValidString(propertyName, "propertyName"); Param.AssertNotNull(propertyNode, "propertyNode"); Param.AssertNotNull(properties, "properties"); Param.AssertNotNull(propertyDescriptors, "propertyDescriptors"); // Create and load the inner property collection. List <string> innerCollection = new List <string>(); // Load the value list. XmlNodeList valueNodes = propertyNode.SelectNodes("Value"); if (valueNodes != null && valueNodes.Count > 0) { foreach (XmlNode valueNode in valueNodes) { if (!string.IsNullOrEmpty(valueNode.InnerText)) { innerCollection.Add(valueNode.InnerText); } } } // If at least one value was loaded, save the proeprty. if (innerCollection.Count > 0) { // Get the property descriptor. CollectionPropertyDescriptor descriptor = propertyDescriptors[propertyName] as CollectionPropertyDescriptor; if (descriptor != null) { // Create the collection node and pass in the inner collection. CollectionProperty collectionProperty = new CollectionProperty(descriptor, innerCollection); // Add this property to the parent collection. properties.Add(collectionProperty); } } }
/// <summary> /// Merged two sets of property collections together. /// </summary> /// <param name="originalPropertyCollection"> /// The original property collection. /// </param> /// <param name="overridingPropertyCollection"> /// The overriding property collection. /// </param> /// <param name="mergedPropertyCollection"> /// The merged property collection. /// </param> internal static void MergePropertyCollections( PropertyCollection originalPropertyCollection, PropertyCollection overridingPropertyCollection, PropertyCollection mergedPropertyCollection) { Param.Ignore(originalPropertyCollection); Param.Ignore(overridingPropertyCollection); Param.AssertNotNull(mergedPropertyCollection, "mergedPropertyCollection"); // The merge is based on whether one or both files are present. if (originalPropertyCollection == null && overridingPropertyCollection != null) { // There are no settings in the original settings file, only settings from the overriding file. foreach (PropertyValue property in overridingPropertyCollection) { mergedPropertyCollection.Add(property.Clone()); } } else if (originalPropertyCollection != null && overridingPropertyCollection == null) { // There are no settings from the overriding settings file, only settings from the original file. foreach (PropertyValue property in originalPropertyCollection) { // Only take a property from the original collection if the property is supposed to be merged. if (property.PropertyDescriptor.Merge) { mergedPropertyCollection.Add(property.Clone()); } } } else if (originalPropertyCollection != null && overridingPropertyCollection != null) { // There are settings in both settings files. Fist, loop through each property collection // in the original settings file. foreach (PropertyValue originalProperty in originalPropertyCollection) { if (originalProperty.PropertyDescriptor.Merge) { // Try to find a corresponding property in the overriding settings file. PropertyValue overridingProperty = overridingPropertyCollection[originalProperty.PropertyName]; if (overridingProperty == null) { // There is no corresponding overriding property. Just add the original property. mergedPropertyCollection.Add(originalProperty.Clone()); } else { // Merge the two property value collections together depending on the property type. switch (originalProperty.PropertyType) { case PropertyType.Int: case PropertyType.String: case PropertyType.Boolean: mergedPropertyCollection.Add(overridingProperty.Clone()); break; case PropertyType.Collection: MergeCollectionProperties(mergedPropertyCollection, originalProperty, overridingProperty); break; default: Debug.Fail("Unexpected property type."); break; } } } } // Now look through each property in the overriding property collection. If there is any // property here which is not contained in the merged collection, just add it directly. // This means that it was not present in the original collection. foreach (PropertyValue overridingProperty in overridingPropertyCollection) { PropertyValue mergedProperty = mergedPropertyCollection[overridingProperty.PropertyName]; if (mergedProperty == null) { mergedPropertyCollection.Add(overridingProperty.Clone()); } } } mergedPropertyCollection.IsReadOnly = true; }