Пример #1
0
 public bool HasValue(PropertyInformation prop)
 {
     if (Mode == ConfigurationSaveMode.Full)
     {
         return(true);
     }
     return(Element.HasValue(Parent, prop, Mode));
 }
Пример #2
0
        protected internal virtual void Unmerge(
            ConfigurationElement source, ConfigurationElement parent,
            ConfigurationSaveMode updateMode)
        {
            if (parent != null && source.GetType() != parent.GetType())
            {
                throw new ConfigurationException("Can't unmerge two elements of different type");
            }

            foreach (PropertyInformation prop in source.ElementInformation.Properties)
            {
                if (prop.ValueOrigin == PropertyValueOrigin.Default)
                {
                    continue;
                }

                PropertyInformation unmergedProp = ElementInformation.Properties [prop.Name];

                object sourceValue = prop.Value;
                if (parent == null || !parent.HasValue(prop.Name))
                {
                    unmergedProp.Value = sourceValue;
                    continue;
                }
                else if (sourceValue != null)
                {
                    object parentValue = parent [prop.Name];
                    if (prop.IsElement)
                    {
                        if (parentValue != null)
                        {
                            ConfigurationElement copy = (ConfigurationElement)unmergedProp.Value;
                            copy.Unmerge((ConfigurationElement)sourceValue, (ConfigurationElement)parentValue, updateMode);
                        }
                        else
                        {
                            unmergedProp.Value = sourceValue;
                        }
                    }
                    else
                    {
                        if (!object.Equals(sourceValue, parentValue) ||
                            (updateMode == ConfigurationSaveMode.Full) ||
                            (updateMode == ConfigurationSaveMode.Modified && prop.ValueOrigin == PropertyValueOrigin.SetHere))
                        {
                            unmergedProp.Value = sourceValue;
                        }
                    }
                }
            }
        }
Пример #3
0
        /*
         * FIXME: LAMESPEC
         *
         * SerializeElement() and SerializeToXmlElement() need to emit different output
         * based on the ConfigurationSaveMode that's being used.  Unfortunately, neither
         * of these methods take it as an argument and there seems to be no documented way
         * how to get it.
         *
         * The parent element is needed because the element could be set to a different
         * than the default value in a parent configuration file, then set locally to that
         * same value.  This makes the element appear locally modified (so it's included
         * with ConfigurationSaveMode.Modified), but it should not be emitted with
         * ConfigurationSaveMode.Minimal.
         *
         * In theory, we could save it into some private field in Unmerge(), but the
         * problem is that Unmerge() is kinda expensive and we also need a way of
         * determining whether or not the configuration has changed in Configuration.Save(),
         * prior to opening the output file for writing.
         *
         * There are two places from where HasValues() is called:
         * a) From Configuration.Save() / SaveAs() to check whether the configuration needs
         *    to be saved.  This check is done prior to opening the file for writing.
         * b) From SerializeToXmlElement() to check whether to emit the element, using the
         *    parent and mode values from the cached 'SaveContext'.
         *
         */

        /*
         * Check whether property 'prop' should be included in the serialized XML
         * based on the current ConfigurationSaveMode.
         */
        internal bool HasValue(ConfigurationElement parent, PropertyInformation prop,
                               ConfigurationSaveMode mode)
        {
            if (prop.ValueOrigin == PropertyValueOrigin.Default)
            {
                return(false);
            }

            if (mode == ConfigurationSaveMode.Modified &&
                prop.ValueOrigin == PropertyValueOrigin.SetHere && prop.IsModified)
            {
                // Value has been modified locally, so we always emit it
                // with ConfigurationSaveMode.Modified.
                return(true);
            }

            /*
             * Ok, now we have to check whether we're different from the inherited
             * value - which could either be a value that's set in a parent
             * configuration file or the default value.
             */

            var hasParentValue  = parent != null && parent.HasValue(prop.Name);
            var parentOrDefault = hasParentValue ? parent [prop.Name] : prop.DefaultValue;

            if (!prop.IsElement)
            {
                return(!object.Equals(prop.Value, parentOrDefault));
            }

            /*
             * Ok, it's an element that has been set in a parent configuration file.			 *
             * Recursively call HasValues() to check whether it's been locally modified.
             */
            var element       = (ConfigurationElement)prop.Value;
            var parentElement = (ConfigurationElement)parentOrDefault;

            return(element.HasValues(parentElement, mode));
        }
Пример #4
0
        /*
         * Cache the current 'parent' and 'mode' values for later use in SerializeToXmlElement()
         * and SerializeElement().
         *
         * Make sure to call base when overriding this in a derived class.
         */
        internal virtual void PrepareSave(ConfigurationElement parent, ConfigurationSaveMode mode)
        {
            saveContext = new SaveContext(this, parent, mode);

            foreach (PropertyInformation prop in ElementInformation.Properties)
            {
                if (!prop.IsElement)
                {
                    continue;
                }

                var elem = (ConfigurationElement)prop.Value;
                if (parent == null || !parent.HasValue(prop.Name))
                {
                    elem.PrepareSave(null, mode);
                }
                else
                {
                    var parentValue = (ConfigurationElement)parent [prop.Name];
                    elem.PrepareSave(parentValue, mode);
                }
            }
        }
        protected internal virtual void Unmerge(
            ConfigurationElement sourceElement, ConfigurationElement parentElement,
            ConfigurationSaveMode saveMode)
        {
            if (parentElement != null && sourceElement.GetType() != parentElement.GetType())
            {
                throw new ConfigurationErrorsException("Can't unmerge two elements of different type");
            }

            bool isMinimalOrModified = saveMode == ConfigurationSaveMode.Minimal ||
                                       saveMode == ConfigurationSaveMode.Modified;

            foreach (PropertyInformation prop in sourceElement.ElementInformation.Properties)
            {
                if (prop.ValueOrigin == PropertyValueOrigin.Default)
                {
                    continue;
                }

                PropertyInformation unmergedProp = ElementInformation.Properties [prop.Name];

                object sourceValue = prop.Value;
                if (parentElement == null || !parentElement.HasValue(prop.Name))
                {
                    unmergedProp.Value = sourceValue;
                    continue;
                }

                if (sourceValue == null)
                {
                    continue;
                }

                object parentValue = parentElement [prop.Name];
                if (!prop.IsElement)
                {
                    if (!object.Equals(sourceValue, parentValue) ||
                        (saveMode == ConfigurationSaveMode.Full) ||
                        (saveMode == ConfigurationSaveMode.Modified && prop.ValueOrigin == PropertyValueOrigin.SetHere))
                    {
                        unmergedProp.Value = sourceValue;
                    }
                    continue;
                }

                var sourceElementValue = (ConfigurationElement)sourceValue;
                if (isMinimalOrModified && !sourceElementValue.IsModified())
                {
                    continue;
                }
                if (parentValue == null)
                {
                    unmergedProp.Value = sourceValue;
                    continue;
                }

                var parentElementValue    = (ConfigurationElement)parentValue;
                ConfigurationElement copy = (ConfigurationElement)unmergedProp.Value;
                copy.Unmerge(sourceElementValue, parentElementValue, saveMode);
            }
        }
Пример #6
0
		/*
		 * Cache the current 'parent' and 'mode' values for later use in SerializeToXmlElement()
		 * and SerializeElement().
		 * 
		 * Make sure to call base when overriding this in a derived class.
		 */
		internal virtual void PrepareSave (ConfigurationElement parent, ConfigurationSaveMode mode)
		{
			saveContext = new SaveContext (this, parent, mode);

			foreach (PropertyInformation prop in ElementInformation.Properties)
			{
				if (!prop.IsElement)
					continue;

				var elem = (ConfigurationElement)prop.Value;
				if (parent == null || !parent.HasValue (prop.Name))
					elem.PrepareSave (null, mode);
				else {
					var parentValue = (ConfigurationElement)parent [prop.Name];
					elem.PrepareSave (parentValue, mode);
				}
			}
		}
Пример #7
0
		/*
		 * FIXME: LAMESPEC
		 * 
		 * SerializeElement() and SerializeToXmlElement() need to emit different output
		 * based on the ConfigurationSaveMode that's being used.  Unfortunately, neither
		 * of these methods take it as an argument and there seems to be no documented way
		 * how to get it.
		 * 
		 * The parent element is needed because the element could be set to a different
		 * than the default value in a parent configuration file, then set locally to that
		 * same value.  This makes the element appear locally modified (so it's included
		 * with ConfigurationSaveMode.Modified), but it should not be emitted with
		 * ConfigurationSaveMode.Minimal.
		 * 
		 * In theory, we could save it into some private field in Unmerge(), but the
		 * problem is that Unmerge() is kinda expensive and we also need a way of
		 * determining whether or not the configuration has changed in Configuration.Save(),
		 * prior to opening the output file for writing.
		 * 
		 * There are two places from where HasValues() is called:
		 * a) From Configuration.Save() / SaveAs() to check whether the configuration needs
		 *    to be saved.  This check is done prior to opening the file for writing.
		 * b) From SerializeToXmlElement() to check whether to emit the element, using the
		 *    parent and mode values from the cached 'SaveContext'.
		 * 
		 */

		/*
		 * Check whether property 'prop' should be included in the serialized XML
		 * based on the current ConfigurationSaveMode.
		 */
		internal bool HasValue (ConfigurationElement parent, PropertyInformation prop,
		                        ConfigurationSaveMode mode)
		{
			if (prop.ValueOrigin == PropertyValueOrigin.Default)
				return false;
			
			if (mode == ConfigurationSaveMode.Modified &&
			    prop.ValueOrigin == PropertyValueOrigin.SetHere && prop.IsModified) {
				// Value has been modified locally, so we always emit it
				// with ConfigurationSaveMode.Modified.
				return true;
			}

			/*
			 * Ok, now we have to check whether we're different from the inherited
			 * value - which could either be a value that's set in a parent
			 * configuration file or the default value.
			 */
			
			var hasParentValue = parent != null && parent.HasValue (prop.Name);
			var parentOrDefault = hasParentValue ? parent [prop.Name] : prop.DefaultValue;

			if (!prop.IsElement)
				return !object.Equals (prop.Value, parentOrDefault);

			/*
			 * Ok, it's an element that has been set in a parent configuration file.			 * 
			 * Recursively call HasValues() to check whether it's been locally modified.
			 */
			var element = (ConfigurationElement) prop.Value;
			var parentElement = (ConfigurationElement) parentOrDefault;
			
			return element.HasValues (parentElement, mode);
		}
Пример #8
0
		protected internal virtual void Unmerge (
				ConfigurationElement source, ConfigurationElement parent,
				ConfigurationSaveMode updateMode)
		{
			if (parent != null && source.GetType() != parent.GetType())
				throw new ConfigurationErrorsException ("Can't unmerge two elements of different type");

			bool isMinimalOrModified = updateMode == ConfigurationSaveMode.Minimal ||
				updateMode == ConfigurationSaveMode.Modified;

			foreach (PropertyInformation prop in source.ElementInformation.Properties)
			{
				if (prop.ValueOrigin == PropertyValueOrigin.Default)
					continue;
				
				PropertyInformation unmergedProp = ElementInformation.Properties [prop.Name];
				
				object sourceValue = prop.Value;
				if (parent == null || !parent.HasValue (prop.Name)) {
					unmergedProp.Value = sourceValue;
					continue;
				}

				if (sourceValue == null)
					continue;

				object parentValue = parent [prop.Name];
				if (!prop.IsElement) {
					if (!object.Equals (sourceValue, parentValue) || 
					    (updateMode == ConfigurationSaveMode.Full) ||
					    (updateMode == ConfigurationSaveMode.Modified && prop.ValueOrigin == PropertyValueOrigin.SetHere))
						unmergedProp.Value = sourceValue;
					continue;
				}

				var sourceElement = (ConfigurationElement) sourceValue;
				if (isMinimalOrModified && !sourceElement.IsModified ())
					continue;
				if (parentValue == null) {
					unmergedProp.Value = sourceValue;
					continue;
				}

				var parentElement = (ConfigurationElement) parentValue;
				ConfigurationElement copy = (ConfigurationElement) unmergedProp.Value;
				copy.Unmerge (sourceElement, parentElement, updateMode);
			}
		}