private static bool IsFiltered(ConfTree conf, ConfItem item, int level) { if (FilterLevel >= 0 && level > 0)//jiangbo: 这个level是相对level? { return(true); } else { if (item.Attributes.ContainsKey("show-only")) { var filter = item.Attributes["show-only"].Split('=').Select(x => x.Trim()).ToList(); if (!IsMatch(conf, filter[0], filter[1])) { FilterLevel = level; return(true); } else { FilterLevel = -1; return(false); } } } FilterLevel = -1; return(false); }
private bool VisitTree(string func, Func <ConfItem, int, bool> executor, ConfTree tree) { _vlog.Debug($@"Visit({func}) ConfTree: {tree.Path}/{tree.Name}({tree.Tag})"); RunningTag.Register(tree); executor(tree, _depth); foreach (var c in tree.Sons) { _depth++; _maxDepth = _depth > _maxDepth ? _depth : _maxDepth; var ret = Visit(func, executor, c); _depth--; if (ret) { RunningTag.Unregister(tree); return(true); } } RunningTag.Unregister(tree); return(false); }
/// <summary> /// tree覆盖this, 注意:Merge没有修改xml文件的内容 /// </summary> /// <param name="tree"></param> /// <param name="targets"></param> /// <returns></returns> public ConfTree Merge(ConfTree tree, List <string> targets = null) { foreach (var item in tree.Items) { if (targets != null && !targets.Contains(item.Name)) { continue; } if (item is ConfTree) { continue; } var orig = Find(item.Name); if (null != orig) { orig.Value = item.Value; } else { Add(item.Clone()); } } return(this); }
/// <summary> /// 将this的ConItem值更新到tree /// </summary> /// <param name="target">被修改的Conf文件(XML)</param> public void OverWrite(ConfTree target) { Visit("OverWrite", (item, level) => { var conf = target.Find(item.Name); if (conf != null && ((conf as ConfTree) == null)) { target[item.Name] = item.Value; } return(false); }); }
private static bool IsMatch(ConfTree conf, string key, string values) { var item = conf.Find(key); if (item != null) { var value = item.Value; return(values.Split('|').Select(x => x.Trim()).Contains(value)); } return(false); }
public static ConfTree Generate(Dictionary <string, string> kvs, string name) { ConfTree result = new ConfTree(name); result.Source = Source.Dictionary; result.Tag = "Default"; foreach (var kv in kvs) { result.Add(new ConfItem(kv.Key, kv.Value)); } return(result); }
public ConfTree Append(ConfTree tree) { foreach (var item in tree.Items) { if (null == Find(item.Name)) { Add(item); } else { _log.Warn($"ConfTree({this.Name}) Append Failed : Item({item.Name}) already exist({item.Value})"); } } return(this); }
public static ConfTree ToTree(XmlNode node, XmlDoc doc = null) { var result = new ConfTree(node.Name); result.XmlDoc = doc; var tag = (node as XmlElement).Attributes.GetNamedItem("tag"); if (tag != null) { result.Tag = tag.Value; } foreach (XmlAttribute attr in (node as XmlElement).Attributes) { result.Attributes[attr.Name] = attr.Value; } foreach (XmlNode n in node.ChildNodes) { if (IsItem(n)) { result.Add(ToItem(n)); } else { if (n.ChildNodes.Count > 1) { result.Add(ToTree(n, doc)); } else if (n.ChildNodes.Count == 1 && n.ChildNodes[0] is XmlElement) { result.Add(ToTree(n, doc)); } else { var ele = (n as XmlElement); if (ele != null && ele.Attributes.GetNamedItem("tag") != null) { result.Add(ToTree(n, doc)); } } } } return(result); }
public void Save(string path = null) { if (path == null) { foreach (var conf in Root.Values) { Builder.Xml.Save(conf, path); } } else { ConfTree root = new ConfTree("Root"); foreach (var conf in Root.Values) { root.Add(conf); } Builder.Xml.Save(root, path); } }
//添加同名节点 public XmlDoc AddSibling(ConfTree refer, ConfTree tree) { XmlNode sibling = XmlOp.Find(this, tree.Name); if (sibling == ChildNodes[ChildNodes.Count - 1])//非唯一 { RemoveChild(sibling); var parent = new ConfTree($"{tree.Name}s"); parent.Add(refer); parent.Add(tree); AppendChild(XmlConf.CreateNode(this, parent)); } else { sibling.ParentNode.AppendChild(XmlConf.CreateNode(this, tree)); } return this; }
public override ConfItem Clone(string tag = null) { var conf = new ConfTree(Name) { Source = Source, Value = Value, Tag = tag == null ? Tag : tag, Attributes = Attributes, Refer = this, }; foreach (var son in Sons) { conf.Add(son.Clone()); } return(conf); }
public static void Save(ConfTree conf, string path = null) { try { XmlDocument doc = conf.XmlDoc as XmlDocument; if (doc != null) { path = path == null?doc.BaseURI.Substring(@"file:///".Length) : path; doc.Save(path); } else { if (path != null) { doc = XmlOp.CreateDoc(); doc.AppendChild(XmlConf.CreateNode(doc, conf)); doc.Save(path); } else if (!string.IsNullOrEmpty((conf.Refer.XmlDoc as XmlDocument).BaseURI)) { doc = conf.Refer.XmlDoc.AddSibling(conf.Refer, conf); path = doc.BaseURI.Substring(@"file:///".Length); doc.Save(path); } else { throw new Exception($"Invalid param for Save"); } conf.XmlDoc = Generate(path).XmlDoc; if (conf.Refer != null) { conf.Refer.XmlDoc = conf.XmlDoc; } } } catch (Exception ex) { _log.Error($"Save({conf.Name}, {path}) failed", ex); } }
public static DataTable ConvertToTable(ConfTree conf, string oplevel = null) { DataTable table = new DataTable(); for (int i = 0; i < conf.MaxDepth + 1; i++) { table.Columns.Add(); } int userLevel = oplevel == null ? int.MaxValue : int.Parse(oplevel); CurrentLevels = new List <KeyValuePair <int, int> >(); conf.Visit("ToTable", (item, level) => { var currentLevel = ChangeOpLevel(item, level); if (userLevel >= currentLevel) { if (!IsFiltered(conf, item, level)) { var row = table.NewRow(); if (string.IsNullOrEmpty(item.Path)) { row[0] = item.Name; row[1] = !string.IsNullOrEmpty(item.Value) ? item.Value : ""; } else { var nodes = item.Path.Split('/'); row[nodes.Length - 1] = DisplayName(item); row[table.Columns.Count - 1] = !string.IsNullOrEmpty(item.Value) ? item.Value : ""; } table.Rows.Add(row); } } return(false); }); return(table); }