protected void BuildParents() { var x = 0; string relPath; while (LocalEntries.TryGetValue("parent." + x, out relPath)) { if (_root != null && !String.IsNullOrEmpty(_root.FullName) && !String.IsNullOrEmpty(relPath)) { var rootDir = Path.GetDirectoryName(_root.FullName); if (!String.IsNullOrEmpty(rootDir)) { var absPath = Path.GetFullPath(Path.Combine(rootDir, relPath)); var fi = new FileInfo(absPath); if (fi.Exists) { var parent = new FileDictionaryTree(fi); Parents.Add(parent); } } } x++; // increment for the next parent } }
/// <summary> /// This constructor accepts an enumerable of initial properties for loading into the local dictionary. /// The internal DirectoryInfo instance is created pointing to the current directory. This obviously /// has no specific meaning. /// </summary> public FileDictionaryTree(IEnumerable <KeyValuePair <string, string> > initialProperties) : this() { foreach (var pair in initialProperties) { LocalEntries.Add(pair.Key, pair.Value); } }
public void Build(FileInfo fileInfo) { Clear(); // This clears all local primary and default entries, as well as the list of parents. if (fileInfo == null) { throw new ArgumentNullException("fileInfo", "The argument must not be null."); } if (!fileInfo.Exists) { throw new FileNotFoundException("The specified file does not exist.", fileInfo.FullName); } _root = fileInfo; Name = _root.Name; FullName = _root.FullName; var propFile = new File(fileInfo); foreach (var entry in propFile.Properties) { LocalEntries.Add(entry.Key, entry.Value); } BuildParents(); }
/// <summary> /// This removes any Primary entries matching a specified key "shallowly". /// That is, it removes it from the top-level Primary dictionary, /// All ancestor Primary dictionaries remain unaffected. /// </summary> /// <remarks>Traking info is also removed.</remarks> /// <param name="key">The key of the entry to be removed.</param> /// <returns>A boolean indicating if the specified entry was found and removed.</returns> public bool RemoveLocal(TKey key) { lock (_syncRoot) { return(LocalEntries.Remove(key)); // Tracking info is also removed. } }
/// <summary> /// This adds a key/value pair "shallowly". /// That is, it only adds it to the top-level dictionary. /// The parents and defaults, if they exist, are unaffected. /// Note that this is just a convenience, since the method could also be called directly on the Primary dictionary. /// </summary> /// <param name="key"></param> /// <param name="value"></param> public void Add(TKey key, TValue value) { lock (_syncRoot) { LocalEntries.Add(key, value); } }
public string ToString(string initialIndent) { var sb = new StringBuilder(); sb = sb.AppendFormat("{0}{1} : {2}\n", initialIndent, "ParameterNamespace", FullName); sb = sb.AppendFormat("\t{0}{1}:\n", initialIndent, "LocalEntries"); sb = LocalEntries.Aggregate(sb, (current, entry) => current.AppendFormat("\t\t{0}{1} = {2}\n", initialIndent, entry.Key, entry.Value)); sb = Parents.Cast <FileDictionaryTree>().Aggregate(sb, (current, parent) => current.Append(parent.ToString(initialIndent + "\t"))); return(sb.ToString()); }
// The following INamespace properties are implemented on DictionaryTree //public string Name { get; protected set; } //public string FullName { get; protected set; } #endregion #region IParameterSource /// <summary> /// This method inserts or overwrites a parameter and its value in the Primary dictionary. /// Note that this only affects the top-level Primary dictionary. If parents contain entries /// for the same parameter, those entries remain unaffected and will "shadow" whatever /// entry is created or overwritten here. In other words, if this entry is later removed /// from the dictionary at this level, then the parent entries will come back into view. /// </summary> /// <param name="parameter">The parameter that will be used as the key.</param> /// <param name="paramValue">The parameter value that will be associated with the key.</param> public void SetParameter(IParameter parameter, string paramValue) { if (LocalEntries.ContainsKey(parameter.Param)) // update existing entry { LocalEntries[parameter.Param] = paramValue; } else { LocalEntries.Add(parameter.Param, paramValue); // add new entry } }
/// <summary> /// This performs a "deep" search for the specified key. /// That is, the Primary dictionary, /// and the Primary dictionaries of all ancestors will be searched. /// If a "shallow" search is required, call "Shallow().ContainsKey()" instead. /// </summary> /// <remarks>If the key is found, it is added to the Accessed list if TrackAccessed is enabled.</remarks> /// <param name="key">The key that will be searched.</param> /// <returns>A boolean indicating if the specified entry was found.</returns> public bool ContainsKey(TKey key) { lock (_syncRoot) { if (LocalEntries.ContainsKey(key)) { return(true); } for (var x = Parents.Count - 1; x >= 0; x--) { if (Parents[x].ContainsKey(key)) { return(true); } } return(false); } }
/// <summary> /// This removes a Primary entry matching the specified key "deeply". /// That is, it removes it from the top-level dictionary /// and from all ancestors, if they exist. /// </summary> /// <remarks>Traking info is also removed.</remarks> /// <param name="key">The key of the entry to be removed.</param> /// <returns>A boolean indicating if the specified entry was found and removed.</returns> public bool Remove(TKey key) { lock (_syncRoot) { var found = false; // Remove it from each of the parents (recursively) foreach (var parent in Parents.Where(parent => parent.Remove(key))) // Tracking info is also removed. { found = true; } // finally remove it from the top-level if (LocalEntries.Remove(key)) // Tracking info is also removed. { found = true; } return(found); } }
public XElement ToXml() { var entries = LocalEntries.Select( entry => new XElement("parameter", new XAttribute("name", entry.Key), new XAttribute("value", entry.Value))).ToList(); var defaults = LocalDefaults.Select( def => new XElement("default", new XAttribute("name", def.Key), new XAttribute("value", def.Value))).ToList(); var parents = Parents.OfType <FileDictionaryTree>().Select(parent => parent.ToXml()).ToList(); var root = new XElement("dictionary"); if (!string.IsNullOrEmpty(Name)) { root.Add(new XAttribute("name", Name)); } if (!string.IsNullOrEmpty(FullName)) { root.Add(new XAttribute("fullName", FullName)); } if (entries.Count > 0) { root.Add(entries); } if (defaults.Count > 0) { root.Add(defaults); } if (parents.Count > 0) { root.Add(new XElement("parents", new XAttribute("count", parents.Count), parents)); } return(root); //return new XElement("dictionary", new XAttribute("name", FullName), // new XElement("entries", new XAttribute("count", LocalEntries.Count), entries), // new XElement("defaults", new XAttribute("count", LocalDefaults.Count), defaults), // new XElement("parents", new XAttribute("count", Parents.Count), parents)); }