Esempio n. 1
0
        internal virtual bool Add(T setting)
        {
            if (setting == null)
            {
                throw new ArgumentNullException(nameof(setting));
            }

            if (Origin.IsMachineWide)
            {
                throw new InvalidOperationException(Resources.CannotUpdateMachineWide);
            }

            if (Origin.IsReadOnly)
            {
                throw new InvalidOperationException(Resources.CannotUpdateReadOnlyConfig);
            }

            if (!Children.Contains(setting) && !setting.IsEmpty())
            {
                Children.Add(setting);

                setting.SetOrigin(Origin);
                setting.SetNode(setting.AsXNode());

                XElementUtility.AddIndented(Node as XElement, setting.Node);
                Origin.IsDirty = true;

                setting.Parent = this;

                return(true);
            }

            return(false);
        }
Esempio n. 2
0
        private void SetValueInternal(XElement sectionElement, string key, string value, IDictionary <string, string> attributes)
        {
            if (String.IsNullOrEmpty(key))
            {
                throw new ArgumentException(Resources.Argument_Cannot_Be_Null_Or_Empty, ConfigurationContants.KeyAttribute);
            }
            if (value == null)
            {
                throw new ArgumentNullException(ConfigurationContants.ValueAttribute);
            }

            var element = FindElementByKey(sectionElement, key, null);

            if (element != null)
            {
                SetElementValues(element, key, value, attributes);
                Save();
            }
            else
            {
                element = new XElement("add");
                SetElementValues(element, key, value, attributes);
                XElementUtility.AddIndented(sectionElement, element);
            }
        }
Esempio n. 3
0
        private void SetValueInternal(XElement sectionElement, string key, string value)
        {
            if (String.IsNullOrEmpty(key))
            {
                throw new ArgumentException(Resources.Argument_Cannot_Be_Null_Or_Empty, "key");
            }
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            var element = FindElementByKey(sectionElement, key, null);

            if (element != null)
            {
                element.SetAttributeValue("value", value);
                Save();
            }
            else
            {
                XElementUtility.AddIndented(sectionElement, new XElement("add",
                                                                         new XAttribute("key", key),
                                                                         new XAttribute("value", value)));
            }
        }
Esempio n. 4
0
        private static XElement GetOrCreateSection(XElement parentElement, string sectionName)
        {
            sectionName = XmlConvert.EncodeLocalName(sectionName);
            var section = parentElement.Element(sectionName);

            if (section == null)
            {
                section = new XElement(sectionName);
                XElementUtility.AddIndented(parentElement, section);
            }
            return(section);
        }
Esempio n. 5
0
        public void UpdateSections(string section, IReadOnlyList <SettingValue> values)
        {
            // machine wide settings cannot be changed.
            if (IsMachineWideSettings)
            {
                if (_next == null)
                {
                    throw new InvalidOperationException(Resources.Error_NoWritableConfig);
                }

                _next.UpdateSections(section, values);
                return;
            }

            if (string.IsNullOrEmpty(section))
            {
                throw new ArgumentException(Resources.Argument_Cannot_Be_Null_Or_Empty, nameof(section));
            }

            if (values == null)
            {
                throw new ArgumentNullException(nameof(values));
            }

            var sectionElement = GetSection(ConfigXDocument.Root, section);

            if (sectionElement != null)
            {
                XElementUtility.RemoveIndented(sectionElement);
            }

            var valuesToWrite = _next == null ? values : values.Where(v => v.Priority < _next._priority);

            if (valuesToWrite.Any())
            {
                sectionElement = GetOrCreateSection(ConfigXDocument.Root, section);
            }

            foreach (var value in valuesToWrite)
            {
                var element = new XElement("add");
                SetElementValues(element, value.Key, value.Value, value.AdditionalData);
                XElementUtility.AddIndented(sectionElement, element);
            }

            Save();

            if (_next != null)
            {
                _next.UpdateSections(section, values.Where(v => v.Priority >= _next._priority).ToList());
            }
        }
Esempio n. 6
0
        public void UpdateSections(string section, IReadOnlyList <SettingValue> values)
        {
            // machine wide settings cannot be changed.
            if (IsMachineWideSettings ||
                ((section == ConfigurationConstants.PackageSources || section == ConfigurationConstants.DisabledPackageSources) && Cleared))
            {
                if (_next == null)
                {
                    throw new InvalidOperationException(Resources.Error_NoWritableConfig);
                }

                _next.UpdateSections(section, values);
                return;
            }

            if (string.IsNullOrEmpty(section))
            {
                throw new ArgumentException(Resources.Argument_Cannot_Be_Null_Or_Empty, nameof(section));
            }

            if (values == null)
            {
                throw new ArgumentNullException(nameof(values));
            }

            var valuesToWrite = _next == null ? values : values.Where(v => v.Priority < _next._priority);

            var sectionElement = GetSection(ConfigXDocument.Root, section);

            if (sectionElement == null && valuesToWrite.Any())
            {
                sectionElement = GetOrCreateSection(ConfigXDocument.Root, section);
            }

            // When updating attempt to preserve the clear tag (and any sources that appear prior to it)
            // to avoid creating extra diffs in the source.
            RemoveElementAfterClearTag(sectionElement);

            foreach (var value in valuesToWrite)
            {
                var element = new XElement("add");
                SetElementValues(element, value.Key, value.OriginalValue, value.AdditionalData);
                XElementUtility.AddIndented(sectionElement, element);
            }

            Save();

            if (_next != null)
            {
                _next.UpdateSections(section, values.Where(v => v.Priority >= _next._priority).ToList());
            }
        }
Esempio n. 7
0
        internal override void Update(SettingItem other)
        {
            var trustedSigner = other as TrustedSignerItem;

            if (!trustedSigner.Certificates.Any())
            {
                throw new InvalidOperationException(Resources.TrustedSignerMustHaveCertificates);
            }

            base.Update(other);

            var otherCerts     = trustedSigner.Certificates.ToDictionary(c => c, c => c);
            var immutableCerts = new List <CertificateItem>(Certificates);

            foreach (var cert in immutableCerts)
            {
                if (otherCerts.TryGetValue(cert, out var otherChild))
                {
                    otherCerts.Remove(cert);
                }

                if (otherChild == null)
                {
                    Certificates.Remove(cert);
                    cert.RemoveFromSettings();
                }
                else if (cert is SettingItem item)
                {
                    item.Update(otherChild as SettingItem);
                }
            }

            foreach (var newCert in otherCerts)
            {
                var certToAdd = newCert.Value;
                Certificates.Add(certToAdd);

                if (Origin != null)
                {
                    certToAdd.SetOrigin(Origin);

                    if (Node != null)
                    {
                        certToAdd.SetNode(certToAdd.AsXNode());

                        XElementUtility.AddIndented(Node as XElement, certToAdd.Node);
                        Origin.IsDirty = true;
                    }
                }
            }
        }
Esempio n. 8
0
        internal override void Update(SettingItem other)
        {
            var packageSourceMappingSourceItem = other as PackageSourceMappingSourceItem;

            if (!packageSourceMappingSourceItem.Patterns.Any())
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.Error_ItemNeedsAtLeastOnePackagePattern, packageSourceMappingSourceItem.Key));
            }

            base.Update(other);

            Dictionary <PackagePatternItem, PackagePatternItem> otherPatterns = packageSourceMappingSourceItem.Patterns.ToDictionary(c => c, c => c);
            var immutablePatterns = new List <PackagePatternItem>(Patterns);

            foreach (PackagePatternItem packagePatternItem in immutablePatterns)
            {
                if (otherPatterns.TryGetValue(packagePatternItem, out PackagePatternItem otherChild))
                {
                    otherPatterns.Remove(packagePatternItem);
                }

                if (otherChild == null)
                {
                    Patterns.Remove(packagePatternItem);
                    packagePatternItem.RemoveFromSettings();
                }
                else if (packagePatternItem is SettingItem item)
                {
                    item.Update(otherChild);
                }
            }

            foreach (var newPackagePatternItem in otherPatterns)
            {
                var itemToAdd = newPackagePatternItem.Value;
                Patterns.Add(itemToAdd);

                if (Origin != null)
                {
                    itemToAdd.SetOrigin(Origin);

                    if (Node != null)
                    {
                        itemToAdd.SetNode(itemToAdd.AsXNode());

                        XElementUtility.AddIndented(Node as XElement, itemToAdd.Node);
                        Origin.IsDirty = true;
                    }
                }
            }
        }
Esempio n. 9
0
        internal bool Add(SettingBase setting)
        {
            if (setting == null)
            {
                throw new ArgumentNullException(nameof(setting));
            }

            if (Origin != null && Origin.IsMachineWide)
            {
                throw new InvalidOperationException(Resources.CannotUpdateMachineWide);
            }

            if (!_mutableChildren.ContainsKey(setting) && !setting.IsEmpty())
            {
                _mutableChildren.Add(setting, setting);

                if (Origin != null)
                {
                    setting.SetOrigin(Origin);

                    if (Node != null)
                    {
                        setting.SetNode(setting.AsXNode());

                        XElementUtility.AddIndented(Node as XElement, setting.Node);
                        Origin.IsDirty = true;
                    }
                }

                setting.Parent = this;

                return(true);
            }

            return(false);
        }