文件配置提供者,支持监听文件修改,然后重新加载配置,并绑定对象
大石头 authored at 2020-03-31 23:27:41
3.49 KiB
X
using System;
using System.IO;
using System.Text;
using NewLife.Log;

namespace NewLife.Configuration
{
    /// <summary>Ini文件配置提供者</summary>
    /// <remarks>
    /// 支持从不同配置文件加载到不同配置模型
    /// </remarks>
    public class InIConfigProvider : FileConfigProvider
    {
        /// <summary>初始化</summary>
        /// <param name="value"></param>
        public override void Init(String value)
        {
            // 加上默认后缀
            if (!value.IsNullOrEmpty() && Path.GetExtension(value).IsNullOrEmpty()) value += ".ini";

            base.Init(value);
        }

        /// <summary>读取配置文件</summary>
        /// <param name="fileName">文件名</param>
        /// <param name="section">配置段</param>
        protected override void OnRead(String fileName, IConfigSection section)
        {
            var lines = File.ReadAllLines(fileName);

            var currentSection = section;
            var remark = "";
            foreach (var item in lines)
            {
                var str = item.Trim();
                if (str.IsNullOrEmpty()) continue;

                // 读取注释
                if (str[0] == '#' || str[0] == ';')
                {
                    remark = str.TrimStart('#', ';').Trim();
                    continue;
                }

                if (str[0] == '[' && str[str.Length - 1] == ']')
                {
                    currentSection = section.GetOrAddChild(str.Trim('[', ']'));
                    currentSection.Comment = remark;
                }
                else
                {
                    var p = str.IndexOf('=');
                    if (p > 0)
                    {
                        var name = str.Substring(0, p).Trim();

                        // 构建配置值和注释
                        var cfg = currentSection.AddChild(name);
                        if (p + 1 < str.Length) cfg.Value = str.Substring(p + 1).Trim();
                        cfg.Comment = remark;
                    }
                }

                // 清空注释
                remark = null;
            }
        }

        /// <summary>获取字符串形式</summary>
        /// <param name="section">配置段</param>
        /// <returns></returns>
        public override String GetString(IConfigSection section = null)
        {
            if (section == null) section = Root;

            // 分组写入
            var sb = new StringBuilder();
            foreach (var item in section.Childs)
            {
                if (item.Childs != null && item.Childs.Count > 0)
                {
                    // 段前空一行
                    sb.AppendLine();
                    // 注释
                    if (!item.Comment.IsNullOrEmpty()) sb.AppendLine("; " + item.Comment);
                    sb.AppendLine($"[{item.Key}]");

                    // 写入当前段
                    foreach (var elm in item.Childs)
                    {
                        // 注释
                        if (!elm.Comment.IsNullOrEmpty()) sb.AppendLine("; " + elm.Comment);

                        sb.AppendLine($"{elm.Key} = {elm.Value}");
                    }
                }
                else
                {
                    // 注释
                    if (!item.Comment.IsNullOrEmpty()) sb.AppendLine("; " + item.Comment);

                    sb.AppendLine($"{item.Key} = {item.Value}");
                }
            }

            return sb.ToString();
        }
    }
}