private int LoadXML(FileInfo afi_xml, ref ConfigUnit acu_config, ref string as_msg) { string ls_value = string.Empty; if (afi_xml == null) { as_msg = Messages.XML_FILE_NULL; return(-1); } if (!File.Exists(afi_xml.FullName)) { as_msg = string.Format(Messages.XML_FILE_UNEXIST, afi_xml.FullName); return(-1); } XmlDocument lxml_doc = new XmlDocument(); lxml_doc.Load(afi_xml.FullName); XmlNode lxml_config = lxml_doc.SelectSingleNode("/Config"); return(LoadXMLNode(lxml_config, null, null, ref acu_config, ref as_msg)); }
public ConfigUnit GetUnit(DirectoryInfo adi_path, ref string as_msg) { if (adi_path == null) { as_msg = Messages.PATH_NULL; return(null); } ConfigUnit lcu_unit = null; ConfigUnit lcu_from = null; string ls_root_mark = @".." + Path.DirectorySeparatorChar; string ls_parent_mark = @"." + Path.DirectorySeparatorChar; string ls_path_temp = adi_path.ToString().Replace(ls_root_mark, string.Empty).Replace(ls_parent_mark, string.Empty); DirectoryInfo ldi_path = new DirectoryInfo(ls_path_temp); string[] ls_path = ls_path_temp.Split(Path.DirectorySeparatorChar); if (adi_path.ToString().StartsWith(ls_root_mark)) { lcu_from = this.RootUnit; if (lcu_from == null) { lcu_from = this; } } else if (adi_path.FullName.StartsWith(ls_parent_mark)) { lcu_from = this.ParentUnit; if (lcu_from == null) { lcu_from = this; } } else { lcu_from = this; } Dictionary <string, ConfigUnit> ldt_values = null; List <ConfigUnit> lst_values = null; foreach (string ls_temp in ls_path) { if (lcu_from.Value == null) { break; } switch (lcu_from.Type) { case CONFIG_UNIT_TYPE.STRING: lcu_unit = lcu_from; lcu_from = null; break; case CONFIG_UNIT_TYPE.DICT: ldt_values = lcu_from.Value as Dictionary <string, ConfigUnit>; if (ldt_values.ContainsKey(ls_temp)) { lcu_from = ldt_values[ls_temp]; } else { lcu_from = null; } break; case CONFIG_UNIT_TYPE.LIST: lst_values = lcu_from.Value as List <ConfigUnit>; lcu_from = null; foreach (ConfigUnit lcu_temp in lst_values) { if (lcu_temp.ID == ls_temp) { lcu_from = lcu_temp; } } break; } if (lcu_from == null) { break; } } if (lcu_from != null) { lcu_unit = lcu_from; } if (lcu_unit == null || lcu_unit.ID != ldi_path.Name) { return(null); } else { return(lcu_unit); } }
private int LoadXMLNode(XmlNode lxml_source, ConfigUnit acu_root, ConfigUnit acu_parent, ref ConfigUnit acu_config, ref string as_msg) { int li_ret = 0; Dictionary <string, ConfigUnit> ldt_values = null; List <ConfigUnit> lst_values = null; ConfigUnit lcu_child; try { acu_config.Tag = lxml_source.Name; acu_config.Type = CONFIG_UNIT_TYPE.LIST; acu_config.RootUnit = acu_root; acu_config.ParentUnit = acu_parent; acu_config.Properties.Clear(); foreach (XmlAttribute lxa_temp in lxml_source.Attributes) { if (!acu_config.Properties.ContainsKey(lxa_temp.Name)) { acu_config.Properties.Add(lxa_temp.Name, lxa_temp.Value); } else { continue; } if (lxa_temp.Name.Trim().ToUpper().Equals("ID")) { acu_config.ID = lxa_temp.Value.Trim(); continue; } if (lxa_temp.Name.Trim().ToUpper().Equals("VALUE")) { acu_config.Value = lxa_temp.Value.Trim(); continue; } if (!lxa_temp.Name.Trim().ToUpper().Equals("TYPE")) { continue; } if (string.IsNullOrEmpty(lxa_temp.Value)) { continue; } if (lxa_temp.Value.ToUpper().Equals(CONFIG_UNIT_TYPE.LIST.ToString())) { acu_config.Type = CONFIG_UNIT_TYPE.LIST; } else if (lxa_temp.Value.ToUpper().Equals(CONFIG_UNIT_TYPE.DICT.ToString())) { acu_config.Type = CONFIG_UNIT_TYPE.DICT; } else if (lxa_temp.Value.ToUpper().Equals(CONFIG_UNIT_TYPE.STRING.ToString())) { acu_config.Type = CONFIG_UNIT_TYPE.STRING; } else { continue; } } if (string.IsNullOrEmpty(acu_config.ID)) { acu_config.ID = acu_config.Tag; } if (lxml_source.ChildNodes == null || lxml_source.ChildNodes.Count <= 0) { return(1); } switch (lxml_source.FirstChild.NodeType) { case XmlNodeType.Text: acu_config.Type = CONFIG_UNIT_TYPE.STRING; acu_config.Value = lxml_source.InnerText.StartsWith("{AppPath}") ? lxml_source.InnerText.Replace(@"{AppPath}\", AppDomain.CurrentDomain.SetupInformation.ApplicationBase) : lxml_source.InnerText; li_ret = 1; break; case XmlNodeType.Element: if (acu_config.Type == CONFIG_UNIT_TYPE.STRING) { acu_config.Type = CONFIG_UNIT_TYPE.LIST; } foreach (XmlNode lxml_node in lxml_source.ChildNodes) { lcu_child = new ConfigUnit(); li_ret = LoadXMLNode(lxml_node, (acu_root == null ? acu_config : acu_root), acu_config, ref lcu_child, ref as_msg); if (li_ret != 1) { return(li_ret); } switch (acu_config.Type) { case CONFIG_UNIT_TYPE.LIST: if (lst_values == null) { lst_values = new List <ConfigUnit>(); } lst_values.Add(lcu_child); break; case CONFIG_UNIT_TYPE.DICT: if (ldt_values == null) { ldt_values = new Dictionary <string, ConfigUnit>(); } if (string.IsNullOrEmpty(lcu_child.ID)) { as_msg = string.Format(Messages.TAG_ID_NULL_EMPTY, lcu_child.Tag); return(0); } if (ldt_values.ContainsKey(lcu_child.ID)) { as_msg = string.Format(Messages.TAG_ID_EXISTED, lcu_child.Tag); return(0); } ldt_values.Add(lcu_child.ID, lcu_child); break; } } switch (acu_config.Type) { case CONFIG_UNIT_TYPE.LIST: acu_config.Value = lst_values; break; case CONFIG_UNIT_TYPE.DICT: acu_config.Value = ldt_values; break; } li_ret = 1; break; default: break; } return(li_ret); } catch (System.Exception err) { as_msg = err.Message; return(-1); } }
public int Load(FileInfo afi_config, ref string as_msg) { Config = new ConfigUnit(); return(LoadXML(afi_config, ref Config, ref as_msg)); }