示例#1
0
        private IConfigurationItem DeserializeItem(DelimiterSeparatedValues.DsvRow row)
        {
            if (row == null)
            {
                throw new ArgumentNullException(nameof(row));
            }
            if (row.ItemArray.Length != 4)
            {
                throw new ArgumentNullException(nameof(row), $"Parameter {nameof(row)} is invalid.");
            }
            var groupName     = row.ItemArray[0].ToString();
            var itemName      = row.ItemArray[1].ToString();
            var valueTypeName = row.ItemArray[2].ToString();
            var content       = row.ItemArray[3].ToString();

            // check for special case: empty group
            if (!(string.IsNullOrWhiteSpace(groupName)) &&
                (string.IsNullOrWhiteSpace(itemName)) &&
                (string.IsNullOrWhiteSpace(valueTypeName)) &&
                (string.IsNullOrWhiteSpace(content)))
            {
                return(null);
            }
            else if (string.IsNullOrWhiteSpace(valueTypeName))
            {
                // **** AUTO-DETECT
                return(ConfigurationShared.CreateConfigurationItem(itemName, content));
            }
            else
            {
                // **** TYPE PROVIDED
                return(ConfigurationShared.CreateConfigurationItem(itemName, valueTypeName, content, _Compressor));
            }
        }
        private IConfigurationItem ProcessItemSegment(XmlReader xr)
        {
            var key           = xr.GetAttribute(_XmlElementItemKeyName_);
            var valueTypeName = xr.GetAttribute(_XmlElementItemTypeName_);
            var content       = xr.ReadElementContentAsString();

            if (string.IsNullOrWhiteSpace(valueTypeName))
            {
                // **** AUTO-DETECT
                return(ConfigurationShared.CreateConfigurationItem(key, content));
            }
            // **** TYPE PROVIDED
            return(ConfigurationShared.CreateConfigurationItem(key, valueTypeName, content, _Compressor));
        }
示例#3
0
        /// <summary>
        /// Converts the specified <paramref name="source"/>, a <see cref="StringBuilder"/> type, to an <see cref="IConfigurationItem"/>.
        /// </summary>
        /// <param name="source">The object, a <see cref="StringBuilder"/> type, to convert into an <see cref="IConfigurationItem"/>.</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="IConfigurationItem"/>.</returns>
        private IConfigurationItem DeserializeItem(StringBuilder source, 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.");
            }
            if (!source.ToString().Contains(_IniElementSeparator_))
            {
                throw new ArgumentException($"Parameter {nameof(source)} must contain at least one {nameof(_IniElementSeparator_)}. ({nameof(_IniElementSeparator_)}='{_IniElementSeparator_}'); (source='{source}')", nameof(source));
            }
            var parts = new List <string>();
            var index = 0;

            parts.Add("");
            var insideQuote = false;

            foreach (var ch in source.ToString())
            {
                if (insideQuote)
                {
                    if (ch == '\"')
                    {
                        insideQuote = false;
                    }
                    else
                    {
                        parts[index] = parts[index] + ch;
                    }
                }
                else
                {
                    if (ch == '\"')
                    {
                        insideQuote = true;
                    }
                    else
                    {
                        if ((ch == _IniElementSeparator_) && (index < 2))
                        {
                            ++index;
                            parts.Add("");
                        }
                        else
                        {
                            parts[index] = parts[index] + ch;
                        }
                    }
                }
            }
            if (isClassicIni)
            {
                // FYI: classic INI has a problem with correctly auto-detecting the proper types.
                //      specifically, it thinks TYPEs are STRINGs and this is a problem for the Configuration Instantiation functions.


                // {key}={value}
                string key     = parts[0];
                string content = "";
                if (parts?.Count == 2)
                {
                    content = parts[1];
                }
                if (parts?.Count == 3)
                {
                    content = parts[1] + parts[2];
                }
                // **** AUTO-DETECT
                return(ConfigurationShared.CreateConfigurationItem(key, content));
            }
            else
            {
                if (parts?.Count == 3)
                {
                    // **** TYPE PROVIDED
                    // contains pattern: {key}={type}={value}
                    string key           = parts[0];
                    string valueTypeName = parts[1];
                    string content       = parts[2];
                    return(ConfigurationShared.CreateConfigurationItem(key, valueTypeName, content, _Compressor));
                }
                else if (parts?.Count == 2)
                {
                    // **** AUTO-DETECT
                    // contains pattern: {key}={value}
                    string key     = parts[0];
                    string content = parts[1];
                    return(ConfigurationShared.CreateConfigurationItem(key, content));
                }
            }
            throw new ArgumentException($"Parameter {nameof(source)} format error. (source='{source}')", nameof(source));
        }