/// <summary> /// 查找选项文件地址 /// </summary> /// <returns>选项文件地址</returns> private string FindOptionFile() { string fileName = OptionHelper.SearchOptionFile(DEFAULT_CONFIG_FILE, null); if (fileName != null) { return(fileName); } OptionAttribute[] configAttributes = OptionAttribute.GetOptionAttributeFromAssembly(null); if (configAttributes != null && configAttributes.Length > 0) { for (int i = 0; i < configAttributes.Length; i++) { fileName = OptionHelper.SearchOptionFile(configAttributes[i].OptionFile, null); if (fileName != null) { return(fileName); } } } fileName = ConfigurationManager.AppSettings[DEFAULT_CONFIG_FILE]; fileName = OptionHelper.SearchOptionFile(fileName, null); return(fileName); }
/// <summary> /// 创建选项 /// </summary> /// <param name="fileName">XML文件</param> /// <param name="readonly">是否只读</param> /// <param name="searchPath">XML搜索目录列表</param> /// <param name="configFiles">如果有子选项文件,则添加到此列表</param> /// <returns>选项节</returns> internal static XmlOption Create(string fileName, bool @readonly, string[] searchPath, List <string> configFiles) { fileName = OptionHelper.SearchOptionFile(fileName, searchPath); if (string.IsNullOrEmpty(fileName)) { return(null); } XmlNode xmlNode = OptionHelper.LoadXmlNodeFromFile(fileName, "/", false); if (xmlNode == null) { return(null); } if (searchPath == null || searchPath.Length <= 0) { searchPath = OptionHelper.OptionFileDefaultSearchPath; } string[] newSearchPath = new string[searchPath.Length + 1]; newSearchPath[0] = Path.GetDirectoryName(fileName); searchPath.CopyTo(newSearchPath, 1); return(Create(null, xmlNode, @readonly, newSearchPath, configFiles)); }
/// <summary> /// 分析初始化选项节 /// </summary> private void InitSetting() { bool init = false; if (this.configFile != null) { try { this.setting = OptionHelper.CreateFromXmlFile(configFile); init = true; } catch (OptionException) { } } if (!init) { XmlNode configNode = (XmlNode)ConfigurationManager.GetSection("htb.devfx"); if (configNode != null) { try { this.setting = OptionHelper.CreateFromXmlNode(configNode); init = true; } catch (OptionException) { } } } OptionFileAttribute[] configFileAttributes; if (!init) { configFileAttributes = OptionFileAttribute.GetOptionFileAttributeFromAssembly(null); if (configFileAttributes != null && configFileAttributes.Length > 0) { for (int i = 0; i < configFileAttributes.Length; i++) { if (configFileAttributes[i].OptionIndex == 0) { string configFile = configFileAttributes[i].OptionFile; Type fileInType = configFileAttributes[i].GetFileInType(); this.setting = OptionHelper.CreateFromXmlSource(configFile, fileInType); init = true; break; } } } } if (!init) { throw new OptionException("选项文件没找到"); } configFileAttributes = OptionFileAttribute.GetOptionFileAttributeFromAssembly(null); if (configFileAttributes != null && configFileAttributes.Length > 0) { SortedList <int, OptionFileAttribute> sortedOptionFiles = new SortedList <int, OptionFileAttribute>(); string indexList = string.Empty; for (int i = 0; i < configFileAttributes.Length; i++) { if (configFileAttributes[i].OptionIndex != 0) { indexList += configFileAttributes[i].OptionIndex + ", "; if (sortedOptionFiles.ContainsKey(configFileAttributes[i].OptionIndex)) { throw new OptionException(string.Format("在所有使用OptionFileAttribute定义中已有如下的顺序号 {0},请修改顺序号定义后重新编译代码", indexList)); } sortedOptionFiles.Add(configFileAttributes[i].OptionIndex, configFileAttributes[i]); } } for (int i = 0; i < sortedOptionFiles.Count; i++) { OptionFileAttribute configFile = sortedOptionFiles.Values[i]; Option option = (Option)OptionHelper.CreateFromXmlSource(configFile.OptionFile, configFile.GetFileInType()); this.setting.Merge(option); } } string[] files = OptionHelper.SearchOptionFileWithPattern(DEFAULT_CONFIG_FILE_PATTERN, null); foreach (string file in files) { Option configSetting = (Option)OptionHelper.CreateFromXmlFile(file); this.setting.Merge(configSetting); } }
/// <summary> /// 创建选项 /// </summary> /// <param name="parent">父选项节</param> /// <param name="xmlNode">XML节</param> /// <param name="readonly">是否只读</param> /// <param name="searchPath">XML搜索目录列表</param> /// <param name="configFiles">如果有子选项文件,则添加到此列表</param> internal static XmlOption Create(XmlOption parent, XmlNode xmlNode, bool @readonly, string[] searchPath, List <string> configFiles) { if (xmlNode.NodeType != XmlNodeType.Element) { throw new OptionException("解析到非法元素"); } if (searchPath == null || searchPath.Length <= 0) { searchPath = OptionHelper.OptionFileDefaultSearchPath; } XmlOption setting = new XmlOption(); setting.parent = parent; setting.ReadOnly = @readonly; setting.property = new XmlOptionSection(xmlNode, @readonly); setting.childSettings = new OptionCollection(true); setting.operatorSettings = new OptionCollection(false); setting.value = new XmlOptionValue(xmlNode, @readonly); setting.SettingName = setting.Name; OptionOperatorEnum settingOperator = setting.SettingOperator; if (settingOperator != 0 && settingOperator != OptionOperatorEnum.Clear) { string newName = setting.Property.TryGetPropertyValue(OptionNamePropertyName); if (string.IsNullOrEmpty(newName)) { throw new OptionException("选项命令未定义属性:" + OptionNamePropertyName); } setting.value.SetName(newName); } foreach (XmlNode node in xmlNode.ChildNodes) { if (node.NodeType != XmlNodeType.Element) { continue; } XmlOption childSetting = Create(setting, node, @readonly, searchPath, configFiles); setting.operatorSettings.Add(childSetting); } Compile(setting, setting.operatorSettings); string configFile = setting.OptionFile; string configNode = setting.OptionNode; if (!string.IsNullOrEmpty(configFile)) { configFile = OptionHelper.SearchOptionFile(configFile, searchPath); if (!string.IsNullOrEmpty(configFile)) { if (string.IsNullOrEmpty(configNode)) { configNode = "/"; } XmlNode newNode = OptionHelper.LoadXmlNodeFromFile(configFile, configNode, false); if (newNode != null) { if (configFiles != null) { configFiles.Add(configFile); } setting.Merge(Create(parent, newNode, @readonly, searchPath, configFiles)); } } } return(setting); }