/// <summary> /// Set a Tag value. A null value removes the Tag. /// </summary> /// <param name="key">Tag Name</param> /// <param name="subkey">Nested SimpleTag to find (if non null) Tag name</param> /// <param name="value">value to be set. A list can be passed for a subtag by separating the values by ';'</param> public void Set(string key, string subkey, string value) { if (value == null) { Remove(key, subkey); return; } List<SimpleTag> list = null; SimpleTags.TryGetValue(key, out list); if (list == null) SimpleTags[key] = list = new List<SimpleTag>(1); if (list.Count == 0) list.Add(new SimpleTag()); if (subkey == null) { list[0].Value = value; } else { if (list[0].SimpleTags == null) list[0].SimpleTags = new Dictionary<string, List<SimpleTag>>(StringComparer.OrdinalIgnoreCase); List<SimpleTag> slist = null; list[0].SimpleTags.TryGetValue(subkey, out slist); if (slist == null) slist = new List<SimpleTag>(1); list[0].SimpleTags[subkey] = slist; if (slist.Count == 0) slist.Add(new SimpleTag()); // Sub-values var svalues = value.Split(';'); int j; for (j = 0; j < svalues.Length; j++) { SimpleTag subtag; if (j >= slist.Count) slist.Add(subtag = new SimpleTag()); else subtag = slist[j]; subtag.Value = svalues[j]; } if (j < slist.Count) slist.RemoveRange(j, slist.Count - j); } }
/// <summary> /// Retrieve a Tag list. If there are multiple tag inside a SimpleTag (when /// accessing a sub-key), these sub-list are represented as semicolon-separated /// values. /// </summary> /// <param name="key">Tag name</param> /// <param name="subkey">Nested SimpleTag to find (if non null) Tag name</param> /// <param name="recu">Also search in parent Tag if true (default: true)</param> /// <returns>Array of values. Nested sub-list are represented by a semicolon- /// separated string /// </returns> public string[] Get(string key, string subkey = null, bool recu = true) { string[] ret = null; List<SimpleTag> mtags; if ((!SimpleTags.TryGetValue(key, out mtags) || mtags == null) && recu) { Tag tag = this; while ((tag = tag.Parent) != null && !tag.SimpleTags.TryGetValue(key, out mtags)) ; } if (subkey != null && mtags != null) { ret = new string[mtags.Count]; // Handle Nested SimpleTags for (int i = 0; i < mtags.Count; i++) { string str = null; var stag = mtags[i]; if (stag.SimpleTags != null) { List<SimpleTag> list = null; stag.SimpleTags.TryGetValue(subkey, out list); if (list == null || list.Count==0) { str = null; } if (mtags.Count == 1) { str = list[0]; } else { str = string.Join("; ", list); } } ret[i] = str; } } else if (mtags != null) { ret = new string[mtags.Count]; for (int i = 0; i < mtags.Count; i++) { ret[i] = mtags[i]; } } return ret; }
/// <summary> /// Remove a Tag /// </summary> /// <param name="key">Tag Name</param> /// <param name="subkey">Nested SimpleTag to find (if non null) Tag name</param> public void Remove(string key, string subkey = null) { List <SimpleTag> list = null; if (SimpleTags.TryGetValue(key, out list)) { if (list != null) { if (subkey != null) { foreach (var stag in list) { if (stag.SimpleTags != null) { List <SimpleTag> slist = null; stag.SimpleTags.TryGetValue(subkey, out slist); if (slist != null) { if (list.Count > 1) { if (slist.Count > 0) { slist.RemoveAt(0); } } else { slist.Clear(); } } } } } else { list.Clear(); } } if (subkey == null) { SimpleTags.Remove(key); } } }
/// <summary> /// Retrieve a list of SimpleTag sharing the same TagName (key). /// </summary> /// <param name="key">Tag name</param> /// <param name="subkey">Nested SimpleTag to find (if non null) Tag name</param> /// <param name="recu">Also search in parent Tag if true (default: true)</param> /// <returns>Array of values</returns> public List <SimpleTag> GetSimpleTags(string key, string subkey = null, bool recu = true) { List <SimpleTag> mtags; if ((!SimpleTags.TryGetValue(key, out mtags) || mtags == null) && recu) { Tag tag = this; while ((tag = tag.Parent) != null && !tag.SimpleTags.TryGetValue(key, out mtags)) { ; } } // Handle Nested SimpleTags if (subkey != null && mtags != null) { var subtags = new List <SimpleTag>(mtags.Count); foreach (var stag in mtags) { if (stag.SimpleTags != null) { List <SimpleTag> list = null; stag.SimpleTags.TryGetValue(subkey, out list); if (mtags.Count > 1) { subtags.Add(list != null && list.Count > 0 ? list[0] : null); } else { subtags = list; } } } return(subtags); } return(mtags); }
/// <summary> /// Create or overwrite the actual tags of a given name/sub-name by new values. /// </summary> /// <param name="key">Tag Name</param> /// <param name="subkey">Nested SimpleTag to find (if non null) Tag name</param> /// <param name="values">Array of values. for each subtag value, a list can be passed by separating the values by ';'</param> public void Set(string key, string subkey, string[] values) { if (values == null) { Remove(key, subkey); return; } List <SimpleTag> list = null; SimpleTags.TryGetValue(key, out list); if (list == null) { SimpleTags[key] = list = new List <SimpleTag>(1); } int i; for (i = 0; i < values.Length; i++) { SimpleTag stag; if (i >= list.Count) { list.Add(stag = new SimpleTag()); } else { stag = list[i]; } if (subkey == null) { stag.Value = values[i]; } else { if (stag.SimpleTags == null) { stag.SimpleTags = new Dictionary <string, List <SimpleTag> >(StringComparer.OrdinalIgnoreCase); } List <SimpleTag> slist = null; stag.SimpleTags.TryGetValue(subkey, out slist); if (slist == null) { slist = new List <SimpleTag>(1); } stag.SimpleTags[subkey] = slist; // Sub-values var svalues = values[i].Split(';'); int j; for (j = 0; j < svalues.Length; j++) { SimpleTag subtag; if (j >= slist.Count) { slist.Add(subtag = new SimpleTag()); } else { subtag = slist[j]; } subtag.Value = svalues[j]; } if (j < slist.Count) { slist.RemoveRange(j, slist.Count - j); } } } if (subkey == null && i < list.Count) { list.RemoveRange(i, list.Count - i); } }