Example #1
0
 /// <summary>
 /// 读取配置文件
 /// </summary>
 /// <returns>是否读取成功</returns>
 public static bool Read()
 {
     try
     {
         Config = new XmlConfig(ConfigPath, true);
     }
     catch (ApplicationException)
     {
         return(false);
     }
     _converterMapsPath = Config.GetValue(ConfigInfo.ConverterMapsPath, "");
     _convertType       = Config.GetValue(ConfigInfo.ConverterType, 0);
     _convertMethod     = Config.GetValue(ConfigInfo.ConvertMethod, 0);
     _saveEncoding      = Config.GetValue(ConfigInfo.SaveEncoding, 0);
     _fileExt           = Config.GetValue(ConfigInfo.FileExt, DefaultFileExt);
     _isExclude         = Config.GetValue(ConfigInfo.IsExclude, false);
     _isTopMost         = Config.GetValue(ConfigInfo.IsTopMost, false);
     _isConvertFileName = Config.GetValue(ConfigInfo.IsConvertFileName, false);
     return(true);
 }
        /// <summary>
        /// 读取字符对照表配置
        /// </summary>
        /// <param name="path">字符对照表配置文件绝对路径</param>
        /// <returns><see cref="ConverterMaps"/>类新实例</returns>
        /// <exception cref="System.ApplicationException"></exception>
        public static ConverterMaps ReadConverterMapsConfig(string path)
        {
            XmlConfig config = null;

            try
            {
                config = new XmlConfig(path);
            }
            catch (ApplicationException)
            {
                throw new ApplicationException("配置文件加载失败!");
            }
            string name = config.GetValue(ConfigInfo.Name, "");

            if (name == String.Empty)
            {
                throw new ApplicationException("字符对照表名字为空!");
            }
            Dictionary <string, Map> maps = new Dictionary <string, Map>();

            foreach (XElement element in config.GetElements(ConfigInfo.Maps))
            {
                string mapId = config.GetAttributeValue(element, ConfigInfo.MapAttribute.Id, "");
                if (mapId == String.Empty)
                {
                    continue;
                }
                string mapValue = element.Value;
                if (mapValue == null || mapValue.Trim() == String.Empty)
                {
                    continue;
                }
                try
                {
                    maps.Add(mapId, new Map(mapId, mapValue));
                }
                catch (Exception)
                {
                    continue;
                }
            }
            if (maps.Count == 0)
            {
                throw new ApplicationException("字符对照表序列未找到!");
            }
            SortedDictionary <int, Converter> converters = new SortedDictionary <int, Converter>();

            foreach (XElement element in config.GetElements(ConfigInfo.Converters))
            {
                int converterId = config.GetAttributeValue(element, ConfigInfo.ConverterAttribute.Id, -1);
                if (converterId == -1)
                {
                    continue;
                }
                string converterName = config.GetAttributeValue(element, ConfigInfo.ConverterAttribute.Name, "");
                if (converterName == String.Empty)
                {
                    continue;
                }
                string sourceMapId = config.GetAttributeValue(element, ConfigInfo.ConverterAttribute.SourceMapId, "");
                if (sourceMapId == String.Empty)
                {
                    continue;
                }
                string destMapId = config.GetAttributeValue(element, ConfigInfo.ConverterAttribute.DestMapId, "");
                if (destMapId == String.Empty)
                {
                    continue;
                }
                if (sourceMapId == destMapId)
                {
                    continue;
                }
                if (!CheckConverter(maps, sourceMapId, destMapId))
                {
                    continue;
                }
                try
                {
                    converters.Add(converterId, new Converter(converterId, converterName, sourceMapId, destMapId));
                }
                catch (Exception)
                {
                    continue;
                }
            }
            if (converters.Count == 0)
            {
                throw new ApplicationException("转换器未找到!");
            }
            return(new ConverterMaps(path, name, maps, converters));
        }