Beispiel #1
0
        public ICollection <ConfigItem> GetConfigItems()
        {
            var itemsByKey = new SortedDictionary <string, ConfigItem>(StringComparer.InvariantCultureIgnoreCase);

            foreach (var key in Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Machine).Keys)
            {
                var keyValueItem = new KeyValueItem((string)key, null);
                keyValueItem.Value = Environment.GetEnvironmentVariable((string)key, EnvironmentVariableTarget.Machine);

                itemsByKey[keyValueItem.Key] = keyValueItem;
            }

            foreach (var key in Environment.GetEnvironmentVariables(EnvironmentVariableTarget.User).Keys)
            {
                var keyValueItem = new KeyValueItem((string)key, null);
                keyValueItem.Value = Environment.GetEnvironmentVariable((string)key, EnvironmentVariableTarget.User);

                itemsByKey[keyValueItem.Key] = keyValueItem;
            }

            foreach (var key in Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Process).Keys)
            {
                var keyValueItem = new KeyValueItem((string)key, null);
                keyValueItem.Value = Environment.GetEnvironmentVariable((string)key, EnvironmentVariableTarget.Process);

                itemsByKey[keyValueItem.Key] = keyValueItem;
            }

            return(itemsByKey.Values);
        }
Beispiel #2
0
            public KeyValueEnumerator(ConfigItem owner, ICollection <ConfigItem> configItems)
            {
                AssertUtil.ArgumentNotNull(configItems, nameof(configItems));

                Owner       = owner;
                ConfigItems = configItems;

                currentItem     = null;
                currentPair     = new KeyValuePair <string, string>(null, null);
                enumerator      = configItems.GetEnumerator();
                enumeratorStack = new Stack <IEnumerator <ConfigItem> >();
            }
Beispiel #3
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);
        }
Beispiel #4
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);
        }
Beispiel #5
0
            public void Dispose()
            {
                currentItem = null;
                currentPair = new KeyValuePair <string, string>(null, null);

                if (enumerator != null)
                {
                    enumerator.Dispose();
                    enumerator = null;
                }

                while (enumeratorStack.Count > 0)
                {
                    enumeratorStack.Pop().Dispose();
                }

                enumeratorStack.Clear();
                enumeratorStack = null;
            }
Beispiel #6
0
            public bool MoveNext()
            {
                while (true)
                {
                    if (enumerator == null)
                    {
                        break;
                    }

                    if (enumerator.MoveNext())
                    {
                        if (enumerator.Current is NamespaceItem)
                        {
                            enumeratorStack.Push(enumerator);
                            enumerator = (enumerator.Current as NamespaceItem).Children.Values.GetEnumerator();

                            continue;
                        }

                        currentItem = enumerator.Current as KeyValueItem;
                        currentPair = new KeyValuePair <string, string>(currentItem.GetRelativeKey(Owner), currentItem.Value);

                        return(true);
                    }


                    enumerator.Dispose();
                    enumerator = null;

                    if (enumeratorStack.Count > 0)
                    {
                        enumerator = enumeratorStack.Pop();
                        continue;
                    }

                    currentItem = null;
                    currentPair = new KeyValuePair <string, string>(null, null);

                    break;
                }

                return(false);
            }
Beispiel #7
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));
                }
            }
        }