Exemplo n.º 1
0
        private Configuration GetNamespace(NamespaceItem owner, string key)
        {
            string[] keyParts = key.Split(ConfigurationManager.SEPARATOR_CHAR, 2);

            var resolvedKey = keyParts[0];

            //var resolvedKey = ConfigurationUtil.ResolveKey(keyParts[0]);

            if (!owner.Children.ContainsKey(resolvedKey))
            {
                return(null);
            }

            if (owner.Children[resolvedKey] is KeyValueItem)
            {
                return(null);
            }

            if (keyParts.Length == 1)
            {
                var namespaceItem = (NamespaceItem)owner.Children[resolvedKey];
                return(new Configuration(namespaceItem, namespaceItem.Children.Values));
            }

            return(GetNamespace((NamespaceItem)owner.Children[resolvedKey], keyParts[1]));
        }
Exemplo n.º 2
0
        static IDictionary <string, ConfigItem> Parse(XmlElement parentElement, NamespaceItem parentItem = null)
        {
            var itemsByKey = new SortedDictionary <string, ConfigItem>(StringComparer.InvariantCultureIgnoreCase);

            foreach (XmlNode childNode in parentElement.ChildNodes)
            {
                if (childNode.NodeType != XmlNodeType.Element)
                {
                    continue;
                }

                var element = (XmlElement)childNode;
                if (element.LocalName == ADD_TAG)
                {
                    if (!element.HasAttribute(NAME_ATTR) || string.IsNullOrWhiteSpace(element.GetAttribute(NAME_ATTR)))
                    {
                        throw new ConfigurationException($"Attribute '{NAME_ATTR}' is required", element.OuterXml);
                    }

                    var keyValueItem = new KeyValueItem(element.GetAttribute(NAME_ATTR), parentItem);
                    keyValueItem.Value = element.InnerText;

                    if (element.HasAttribute(VALUE_ATTR))
                    {
                        keyValueItem.Value = element.GetAttribute(VALUE_ATTR);
                    }

                    itemsByKey[keyValueItem.Key] = keyValueItem;
                }
                else if (element.LocalName == NAMESPACE_TAG)
                {
                    if (!element.HasAttribute(NAME_ATTR) || string.IsNullOrWhiteSpace(element.GetAttribute(NAME_ATTR)))
                    {
                        throw new ConfigurationException($"Attribute '{NAME_ATTR}' is required", element.OuterXml);
                    }

                    var namespaceItem = new NamespaceItem(element.GetAttribute(NAME_ATTR), parentItem);
                    namespaceItem.Children = Parse(element, namespaceItem);

                    itemsByKey[namespaceItem.Key] = namespaceItem;
                }
                else
                {
                    throw new ConfigurationException($"Unsupported element '{element.LocalName}'", element.OuterXml);
                }
            }

            return(itemsByKey);
        }
Exemplo n.º 3
0
        static IDictionary <string, ConfigItem> Parse(JToken jsonValue, NamespaceItem parentItem = null)
        {
            if (!(jsonValue is JObject))
            {
                throw new ConfigurationException("Json config invalid");
            }

            var itemsByKey = new SortedDictionary <string, ConfigItem>(StringComparer.InvariantCultureIgnoreCase);

            foreach (var item in (JObject)jsonValue)
            {
                if (string.IsNullOrWhiteSpace(item.Key))
                {
                    throw new ConfigurationException($"Json config key required");
                }

                if (item.Value is JObject)
                {
                    var namespaceItem = new NamespaceItem(item.Key, parentItem);
                    namespaceItem.Children = Parse(item.Value, namespaceItem);

                    itemsByKey[namespaceItem.Key] = namespaceItem;
                }
                else
                {
                    var keyValueItem = new KeyValueItem(item.Key, parentItem);
                    itemsByKey[keyValueItem.Key] = keyValueItem;

                    if (item.Value is JValue)
                    {
                        if (item.Value.ValueKind == JValueKind.Null)
                        {
                            continue;
                        }

                        keyValueItem.Value = ((JValue)item.Value).Value;
                    }
                    else
                    {
                        keyValueItem.Value = item.Value.ToString();
                    }
                }
            }

            return(itemsByKey);
        }
Exemplo n.º 4
0
        static void ResolveChild(NamespaceItem owner, string key, string value)
        {
            string[] keyParts    = key.Split(SEPARATOR_CHAR, 2);
            var      resolvedKey = ConfigurationUtil.ResolveKey(keyParts[0]);

            if (keyParts.Length == 1)
            {
                owner.Children[resolvedKey] = new KeyValueItem(resolvedKey, value, owner);
                return;
            }

            if (!owner.Children.ContainsKey(resolvedKey))
            {
                owner.Children[resolvedKey] = new NamespaceItem(resolvedKey, owner);
            }
            else if (owner.Children[resolvedKey] is KeyValueItem)
            {
                owner.Children[resolvedKey] = new NamespaceItem(resolvedKey, owner);
            }

            ResolveChild((NamespaceItem)owner.Children[resolvedKey], keyParts[1], value);
        }
Exemplo n.º 5
0
        static void Resolve()
        {
            var newRootItems = new SortedDictionary <string, ConfigItem>(StringComparer.InvariantCultureIgnoreCase);

            foreach (var provider in Providers.ToArray())
            {
                foreach (var keyValuePair in new Configuration(null, provider.GetConfigItems()))
                {
                    string[] keyParts    = keyValuePair.Key.Split(SEPARATOR_CHAR, 2);
                    var      resolvedKey = ConfigurationUtil.ResolveKey(keyParts[0]);

                    if (keyParts.Length == 1)
                    {
                        newRootItems[resolvedKey] = new KeyValueItem(resolvedKey, keyValuePair.Value);
                        continue;
                    }

                    if (!newRootItems.ContainsKey(resolvedKey))
                    {
                        newRootItems[resolvedKey] = new NamespaceItem(resolvedKey);
                    }
                    else if (newRootItems[resolvedKey] is KeyValueItem)
                    {
                        newRootItems[resolvedKey] = new NamespaceItem(resolvedKey);
                    }

                    ResolveChild((NamespaceItem)newRootItems[resolvedKey], keyParts[1], keyValuePair.Value);
                }
            }

            var newKeyValues = new Dictionary <string, string>(StringComparer.InvariantCultureIgnoreCase);

            foreach (var keyValuePair in new Configuration(null, newRootItems.Values))
            {
                newKeyValues[keyValuePair.Key] = keyValuePair.Value;
            }


            var oldKeyValues = KeyValues;

            KeyValues = newKeyValues;
            RootItems = newRootItems;

            foreach (var keyValuePair in oldKeyValues)
            {
                if (!newKeyValues.ContainsKey(keyValuePair.Key))
                {
                    OnChanged(new ChangedEventArgs(keyValuePair.Key, keyValuePair.Value, ChangedStatus.Delete));
                }
            }

            foreach (var keyValuePair in newKeyValues)
            {
                if (!oldKeyValues.ContainsKey(keyValuePair.Key))
                {
                    OnChanged(new ChangedEventArgs(keyValuePair.Key, keyValuePair.Value, ChangedStatus.Set));
                }
                else if (keyValuePair.Value != oldKeyValues[keyValuePair.Key])
                {
                    OnChanged(new ChangedEventArgs(keyValuePair.Key, keyValuePair.Value, ChangedStatus.Set));
                }
            }
        }