예제 #1
0
        internal static IConfigurationGroup FindSubGroup(IConfigurationGroup group, string itemKey)
        {
            foreach (var item in group)
            {
                if (item.Key == itemKey)
                {
                    return(item);
                }
            }
            var newGroup = new ConfigurationGroup(itemKey);

            group.Add(newGroup);
            return(newGroup);
        }
예제 #2
0
        /// <summary>
        /// Converts the specified <paramref name="source"/>, a <see cref="StringBuilder"/> type, to an <see cref="IConfigurationGroup"/>.
        /// </summary>
        /// <param name="source">The object, a <see cref="StringBuilder"/> type, to convert into an <see cref="IConfigurationGroup"/>.</param>
        /// <returns>The object converted into an <see cref="IConfigurationGroup"/>.</returns>
        public IConfigurationGroup Deserialize(StringBuilder source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (source.Length == 0)
            {
                throw new ArgumentNullException(nameof(source), $"Parameter {nameof(source)} cannot be an empty string.");
            }
            IConfigurationGroup workGroup = new ConfigurationGroup("");
            var table = new DelimiterSeparatedValues.DsvTable(_Settings);

            table.ReadString(source.ToString());
            foreach (var row in table.Rows)
            {
                var groupName = row[0].ToString();
                if (string.IsNullOrWhiteSpace(groupName))
                {
                    workGroup.Items.Add(DeserializeItem(row));
                }
                else
                {
                    var group = ConfigurationShared.FindGroup(workGroup, groupName, _CsvGroupNameSeparator_);
                    var item  = DeserializeItem(row);
                    if (item != null)
                    {
                        group.Items.Add(item);
                    }
                }
            }
            if ((string.IsNullOrWhiteSpace(workGroup.Key)) && (workGroup.Items.Count == 0) && (workGroup.Count == 1))
            {
                workGroup = workGroup.ElementAt(0);
            }
            workGroup.MarkDirty();
            workGroup.MarkNew();
            return(workGroup);
        }
        /// <summary>
        /// Converts the specified <paramref name="source"/>, a <see cref="StringBuilder"/> type, to an <see cref="IConfigurationGroup"/>.
        /// </summary>
        /// <param name="source">The object, a <see cref="StringBuilder"/> type, to convert into an <see cref="IConfigurationGroup"/>.</param>
        /// <returns>The object converted into an <see cref="IConfigurationGroup"/>.</returns>
        public IConfigurationGroup Deserialize(StringBuilder source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (source.Length == 0)
            {
                throw new ArgumentNullException(nameof(source), $"Parameter {nameof(source)} cannot be an empty string.");
            }
            IConfigurationGroup rootGroup = null;

            using (var sr = new System.IO.StringReader(source.ToString()))
            {
                using (var xr = System.Xml.XmlReader.Create(sr))
                {
                    while (xr.Read())
                    {
                        if (xr.IsStartElement(_XmlElementGroupName_))
                        {
                            // process group
                            var key = xr.GetAttribute(_XmlElementGroupKeyName_);
                            if (rootGroup == null)
                            {
                                rootGroup = new ConfigurationGroup(key);
                                rootGroup.ClearDirty();
                                rootGroup.ClearNew();
                            }
                            ProcessGroup(xr, rootGroup, rootGroup);
                        }
                    }
                }
            }
            rootGroup.ClearDirty();
            rootGroup.ClearNew();
            return(rootGroup);
        }
예제 #4
0
        /// <summary>
        /// Converts the specified <paramref name="source"/>, a <see cref="StringBuilder"/> type, to an <see cref="IConfigurationGroup"/>.
        /// </summary>
        /// <param name="source">The object, a <see cref="StringBuilder"/> type, to convert into an <see cref="IConfigurationGroup"/>.</param>
        /// <param name="includeSubgroups">True to scan group names for the Group Name Separator and create sub groups; otherwise, false will ignore the Group Name Separator and use the entire string as the group name.</param>
        /// <param name="isClassicIni">if <b>true</b> the <paramref name="source"/> format will be interpreted as classic INI, {key}={value}; otherwise, <b>false</b> will interpret the file using the custom triple entry INI format, {key}={type}={value}</param>
        /// <returns>The object converted into an <see cref="IConfigurationGroup"/>.</returns>
        public IConfigurationGroup Deserialize(StringBuilder source, bool includeSubgroups, bool isClassicIni)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (source.Length == 0)
            {
                throw new ArgumentNullException(nameof(source), $"Parameter {nameof(source)} cannot be an empty string.");
            }
            IConfigurationGroup rootGroup    = new ConfigurationGroup("");
            IConfigurationGroup currentGroup = rootGroup;
            string candidateRootGroupName    = null;

            foreach (var line in from x in source.ToString().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)
                     select x.Trim())
            {
                if (line.StartsWith(_IniGroupNameStartCharacter_) && line.EndsWith(_IniGroupNameStopCharacter_))
                {
                    // process group
                    var groupFullName = line.Substring(1, line.Length - 2);
                    if (includeSubgroups)
                    {
                        var possibleRootGroup = groupFullName;
                        if (possibleRootGroup.Contains(_IniGroupNameSeparator_))
                        {
                            possibleRootGroup = possibleRootGroup.Substring(0, line.IndexOf(_IniGroupNameSeparator_) - 1);
                        }
                        if (candidateRootGroupName == null)
                        {
                            candidateRootGroupName = possibleRootGroup;
                        }
                        else if (candidateRootGroupName != possibleRootGroup)
                        {
                            candidateRootGroupName = "";
                        }
                        //
                        currentGroup = ConfigurationShared.FindGroup(rootGroup, groupFullName, _IniGroupNameSeparator_);
                    }
                    else
                    {
                        var newGroup = new ConfigurationGroup(groupFullName, !includeSubgroups);
                        rootGroup.Add(newGroup);
                        currentGroup = newGroup;
                    }
                    if (currentGroup == null)
                    {
                        currentGroup = rootGroup;
                    }
                }
                else
                {
                    // process item
                    if (currentGroup == null)
                    {
                        throw new InvalidOperationException($"There is no current configuration group. Somehow the group name was missed. Check the parameter {nameof(source)} for corruption or bad form.");
                    }
                    currentGroup.Items.Add(DeserializeItem(new StringBuilder(line), isClassicIni));
                }
            }
            if (!string.IsNullOrWhiteSpace(candidateRootGroupName))
            {
                ((IConfigurationGroupAdvanced)rootGroup).SetKey(candidateRootGroupName);
            }
            // check for subgroup with same name as root group
            var foundSubGroup = (from x in rootGroup
                                 where x.Key == rootGroup.Key
                                 select x).FirstOrDefault();

            if (foundSubGroup != null)
            {
                rootGroup = foundSubGroup;
                (rootGroup as IConfigurationGroupAdvanced).Parent = null;
            }
            //
            if ((rootGroup.Count == 1) && (rootGroup.ContainsKey(rootGroup.Key)) && (rootGroup[rootGroup.Key].Key == rootGroup.Key))
            {
            }
            //
            rootGroup.ClearDirty();
            rootGroup.ClearNew();
            return(rootGroup);
        }