public static void GetCounts( IProjectPlugin project, Block block, out int count, out int wordCount, out int characterCount, out int nonWhitespaceCount) { // Make sure we have a sane state. if (project == null) { throw new ArgumentNullException("project"); } if (block == null) { throw new ArgumentNullException("block"); } // Figure out the root path for the various components. HierarchicalPath rootPath = GetPluginRootPath(project); count = GetCount(block, rootPath, "Total/" + CountType); wordCount = GetCount(block, rootPath, "Total/" + WordCountType); characterCount = GetCount(block, rootPath, "Total/" + CharacterCountType); nonWhitespaceCount = GetCount( block, rootPath, "Total/" + NonWhitespaceCountType); }
/// <summary> /// Adds an item at the specified path. /// </summary> /// <param name="path">The path.</param> /// <param name="item">The item.</param> public void Add( HierarchicalPath path, TValue item) { // If are the top-level path, set the item. if (path.Count == 0) { Item = item; HasItem = true; return; } // We have at least one more level. Figure out the top-level string // and see if we have to create the child tree node for it. string topLevel = path.First; if (!nodes.ContainsKey(topLevel)) { nodes[topLevel] = CreateChild(topLevel); } // Pull out the child tree so we can add it. HierarchicalPathTreeCollection <TValue> child = nodes[topLevel]; HierarchicalPath childPath = path.Splice(1); child.Add(childPath, item); }
public void SubRef3() { var nr = new HierarchicalPath("/this/is/a/path"); var sr = new HierarchicalPath("/this/is"); sr.GetPathAfter(nr); }
public void SubRef1() { var nr = new HierarchicalPath("/this/is"); var sr = new HierarchicalPath("/this/is/a/path"); Assert.AreEqual("./a/path", sr.GetPathAfter(nr).Path); }
/// <summary> /// Initializes a new instance of the <see cref="HierarchicalSelector"/> class. /// </summary> /// <param name="path">The path.</param> /// <param name="context">The context.</param> public HierarchicalSelector( string path, HierarchicalPath context) : base(path, context) { ParseSelectors(); }
public void StartsWith2() { var nr = new HierarchicalPath("/this/is"); var sr = new HierarchicalPath("/not/in/is/a/path"); Assert.AreEqual(false, nr.StartsWith(sr)); }
public void StartsWith4() { var nr = new HierarchicalPath("/this/is"); var sr = new HierarchicalPath("/this"); Assert.AreEqual(true, nr.StartsWith(sr)); }
public void ParentRef() { var up = new HierarchicalPath("/dir1/sub1"); HierarchicalPath up1 = up.Parent; Assert.AreEqual("/dir1", up1.Path); }
public void ParsePluses() { var nr = new HierarchicalPath("/Test/Test +1/Test +2"); Assert.AreEqual("/Test/Test +1/Test +2", nr.ToString()); Assert.AreEqual("Test +2", nr.Last); }
public void NoAbsolute() { var path = new HierarchicalPath("foo"); Assert.IsTrue(path.IsRelative); Assert.AreEqual("./foo", path.Path); }
public void ParentPath() { var up = new HierarchicalPath("/dir1/sub1"); string up1 = up.Parent.Path; Assert.AreEqual("/dir1", up1); }
public void LeadingDotSlash() { var context = new HierarchicalPath("/"); var nr = new HierarchicalPath("./", context); Assert.AreEqual("/", nr.Path); }
public void LeadingDot() { var context = new HierarchicalPath("/"); var path = new HierarchicalPath(".", context); Assert.AreEqual("/", path.Path); }
public FilesystemPersistenceProjectPlugin(Project project) { Project = project; PluginId = Guid.NewGuid(); SettingsKey = RootSettingsKey; // TODO new HierarchicalPath(PluginId.ToString(),RootSettingsKey); }
public void SubRef4() { var nr = new HierarchicalPath("/this/is/a/path"); var sr = new HierarchicalPath("/not/a/path"); nr.GetPathAfter(sr); }
public void SubRef2() { var nr = new HierarchicalPath("/this/is"); var sr = new HierarchicalPath("/this/is"); Assert.AreEqual(".", nr.GetPathAfter(sr).Path); }
/// <summary> /// Retrieves an item at the given path. If the item doesn't exist, /// then a NotFoundException. /// </summary> /// <param name="path"></param> /// <returns></returns> public TValue Get(HierarchicalPath path) { // If we are at the top-level path, return ourselves. if (path.Count == 0) { // If we have the item, then return it. If we don't have the // item, then throw an exception. if (HasItem) { // We have the item, so return it. return(Item); } // Throw a not found exception since we can't find it. throw new KeyNotFoundException("Cannot retrieve value at path " + path); } // Get the top-level element for a child and make sure we have it. string topLevel = path.First; if (!nodes.ContainsKey(topLevel)) { throw new KeyNotFoundException("Cannot retrieve value at path " + path); } // Pass the request into the child level. HierarchicalPath childPath = path.Splice(1); HierarchicalPathTreeCollection <TValue> child = nodes[topLevel]; return(child.Get(childPath)); }
/// <summary> /// Sets the setting to the specified path. /// </summary> /// <typeparam name="TSetting">The type of the setting.</typeparam> /// <param name="path">The path.</param> /// <param name="setting">The setting.</param> public void Set <TSetting>( HierarchicalPath path, TSetting setting) { xmlSettings.Remove(path); objectSettings[path] = setting; }
/// <summary> /// Retrives all the settings of the given path including all the parents. /// The order of the resulting enumerable is from the current one with each /// parent added to the end. If a given SettingsManager doesn't include the /// path, then no item will be included. /// </summary> /// <typeparam name="TSetting">The type of the setting.</typeparam> /// <param name="path">The path.</param> /// <returns></returns> public IList <TSetting> GetAll <TSetting>(HierarchicalPath path) where TSetting : class, new() { // Go through the manager, adding each setting, until we run out of // parent managers. var settings = new List <TSetting>(); SettingsManager manager = this; while (manager != null) { // See if this manager has that item. TSetting setting; if (manager.TryGet(path, SettingSearchOptions.None, out setting)) { settings.Add(setting); } // Move to the parent setting manager. manager = manager.Parent; } // Return the resulting managers. return(settings); }
public void ChildIndex() { var up = new HierarchicalPath("/dir1/sub1"); HierarchicalPath c1 = up["sub2/sub3"]; Assert.AreEqual("/dir1/sub1/sub2/sub3", c1.Path); }
public void TestNodeCreateChild() { var up = new HierarchicalPath("/dir1/sub1"); HierarchicalPath c1 = up.Append("sub2"); Assert.AreEqual("/dir1/sub1/sub2", c1.Path); }
/// <summary> /// Loads the specified reader into the settings. /// </summary> /// <param name="reader">The reader.</param> public void Load(XmlReader reader) { // Read until we find the close tag for the settings. while (true) { // If we aren't in the right namespace, just skip it. if (reader.NamespaceURI != SettingsNamespace) { // Advance the reader, breaking out if we hit the end of the // XML. if (!reader.Read()) { break; } // Loop again to process the next read. continue; } // Look to see if we have a close tag. if (reader.LocalName == "settings") { if (reader.NodeType == XmlNodeType.EndElement) { return; } if (reader.NodeType == XmlNodeType.Element && reader.IsEmptyElement) { return; } } // Look for the opening setting tag. if (reader.NodeType == XmlNodeType.Element && reader.LocalName == "setting") { // Get the path from the attribute. var path = new HierarchicalPath(reader["path"]); // Pull in the XML directly. string xml = reader.ReadInnerXml(); // Put them into the settings. xmlSettings[path] = xml; // Because we used the ReadInnerXml(), we don't want to advance // the path. continue; } // Read the next node. if (!reader.Read()) { break; } } }
/// <summary> /// Gets a settings at the specific path. /// </summary> /// <typeparam name="TSetting">The type of the setting.</typeparam> /// <param name="path">The path.</param> /// <param name="searchOptions">The search options.</param> /// <returns></returns> public TSetting Get <TSetting>( string path, SettingSearchOptions searchOptions) where TSetting : class, new() { var hierarchicalPath = new HierarchicalPath(path); return(Get <TSetting>(hierarchicalPath, searchOptions)); }
/// <summary> /// Gets a settings at the specific path. /// </summary> public TSetting Get <TSetting>( HierarchicalPath path, SettingSearchOptions searchOptions) where TSetting : class, new() { SettingsManager containingManager; return(Get <TSetting>(path, searchOptions, out containingManager)); }
/// <summary> /// Retrives all the settings of the given path including all the parents. /// The order of the resulting enumerable is from the current one with each /// parent added to the end. If a given SettingsManager doesn't include the /// path, then no item will be included. /// </summary> /// <typeparam name="TSetting">The type of the setting.</typeparam> /// <param name="path">The path.</param> /// <returns></returns> public IList <TSetting> GetAll <TSetting>(string path) where TSetting : class, new() { var hierarchicalPath = new HierarchicalPath(path); IList <TSetting> settings = GetAll <TSetting>(hierarchicalPath); return(settings); }
/// <summary> /// Sets the setting to the specified path. /// </summary> /// <typeparam name="TSetting">The type of the setting.</typeparam> /// <param name="path">The path.</param> /// <param name="setting">The setting.</param> public void Set <TSetting>( string path, TSetting setting) { var hierarchicalPath = new HierarchicalPath(path); Set(hierarchicalPath, setting); }
public void SubpathWithPluses() { var nr = new HierarchicalPath("/Test +1/A"); var nr2 = new HierarchicalPath("/Test +1"); HierarchicalPath nr3 = nr.GetPathAfter(nr2); Assert.AreEqual("./A", nr3.ToString()); }
/// <summary> /// Tries to get settings at the given path without creating it if /// missing. /// </summary> /// <typeparam name="TSetting">The type of the setting.</typeparam> /// <param name="path">The path.</param> /// <param name="searchOptions">The search options.</param> /// <param name="containingManager">The containing manager.</param> /// <returns></returns> public bool TryGet <TSetting>( HierarchicalPath path, SettingSearchOptions searchOptions, out SettingsManager containingManager) where TSetting : class, new() { TSetting output; return(TryGet(path, searchOptions, out output, out containingManager)); }
/// <summary> /// Tries to get settings at the given path without creating it if /// missing. /// </summary> /// <typeparam name="TSetting">The type of the setting.</typeparam> /// <param name="path">The path.</param> /// <param name="searchOptions">The search options.</param> /// <param name="output">The output.</param> /// <returns></returns> public bool TryGet <TSetting>( HierarchicalPath path, SettingSearchOptions searchOptions, out TSetting output) where TSetting : class, new() { SettingsManager manager; return(TryGet(path, searchOptions, out output, out manager)); }
public void TestSplice3() { // Setup var path = new HierarchicalPath("/a/b/c/d/e"); // Operation HierarchicalPath results = path.Splice(3, 2); // Verification Assert.AreEqual("./d/e", results.ToString()); }
public EditorAction( string displayName, HierarchicalPath resourceKey, Action <BlockCommandContext> action, Importance importance = Importance.Normal) { DisplayName = displayName; Importance = importance; ResourceKey = resourceKey; Action = action; }
public EditorAction( string displayName, HierarchicalPath resourceKey, Action<BlockCommandContext> action, Importance importance = Importance.Normal) { DisplayName = displayName; Importance = importance; ResourceKey = resourceKey; Action = action; }
public void AreEqual() { // Setup var expected = new HierarchicalPath("/Application/Quit"); // Operation var path = new HierarchicalPath("/Application/Quit"); // Verification Assert.AreEqual(expected, path); Assert.IsTrue(expected == path); }
public void TestDepthAdd() { // Setup var collection = new HierarchicalPathTreeCollection<int>(); var path = new HierarchicalPath("/a/b"); // Operation collection.Add(path, 234); // Verification Assert.AreEqual(1, collection.Count); Assert.AreEqual(0, collection.Item); Assert.AreEqual(234, collection.Get(path)); }
public void ChangeSingleBlockTwoWords() { // Arrange ProjectBlockCollection blocks; BlockCommandSupervisor commands; WordCounterProjectPlugin projectPlugin; SetupPlugin(out blocks, out commands, out projectPlugin); // Arrange: Initial insert commands.InsertText(blocks[0], 0, "Line 1"); blocks.Project.Plugins.WaitForBlockAnalzyers(); // Act commands.InsertText(blocks[0], 0, "One "); blocks.Project.Plugins.WaitForBlockAnalzyers(); // Assert var path = new HierarchicalPath("/Plugins/Word Counter"); var total = new HierarchicalPath("Total", path); var paragraph = new HierarchicalPath("Block Types/Paragraph", path); Project project = blocks.Project; PropertiesDictionary blockProperties = blocks[0].Properties; PropertiesDictionary projectProperties = project.Properties; Assert.AreEqual(1, project.Plugins.Controllers.Count); Assert.AreEqual(3, blockProperties.Get<int>("Words", total)); Assert.AreEqual(10, blockProperties.Get<int>("Characters", total)); Assert.AreEqual(8, blockProperties.Get<int>("Non-Whitespace", total)); Assert.AreEqual(2, blockProperties.Get<int>("Whitespace", total)); Assert.AreEqual(1, blockProperties.Get<int>("Count", paragraph)); Assert.AreEqual(3, blockProperties.Get<int>("Words", paragraph)); Assert.AreEqual(10, blockProperties.Get<int>("Characters", paragraph)); Assert.AreEqual(8, blockProperties.Get<int>("Non-Whitespace", paragraph)); Assert.AreEqual(2, blockProperties.Get<int>("Whitespace", paragraph)); Assert.AreEqual(3, projectProperties.Get<int>("Words", total)); Assert.AreEqual(10, projectProperties.Get<int>("Characters", total)); Assert.AreEqual(8, projectProperties.Get<int>("Non-Whitespace", total)); Assert.AreEqual(2, projectProperties.Get<int>("Whitespace", total)); Assert.AreEqual(1, projectProperties.Get<int>("Count", paragraph)); Assert.AreEqual(3, projectProperties.Get<int>("Words", paragraph)); Assert.AreEqual(10, projectProperties.Get<int>("Characters", paragraph)); Assert.AreEqual(8, projectProperties.Get<int>("Non-Whitespace", paragraph)); Assert.AreEqual(2, projectProperties.Get<int>("Whitespace", paragraph)); }
public void TestMultipleDepthAdds() { // Setup var collection = new HierarchicalPathTreeCollection<int>(); var path1 = new HierarchicalPath("/a/b"); var path2 = new HierarchicalPath("/a/c"); // Operation collection.Add(path1, 234); collection.Add(path2, 567); // Verification Assert.AreEqual(2, collection.Count); Assert.AreEqual(4, collection.NodeCount); Assert.AreEqual(0, collection.Item); Assert.AreEqual(2, collection.GetChild(new HierarchicalPath("/a")).Count); Assert.AreEqual(234, collection.Get(path1)); Assert.AreEqual(567, collection.Get(path2)); }
/// <summary> /// Determines whether the specified path is match for the selector. /// </summary> /// <param name="path">The path.</param> /// <returns> /// <c>true</c> if the specified path is match; otherwise, <c>false</c>. /// </returns> public bool IsMatch(HierarchicalPath path) { return IsMatch(path, 0); }
/// <summary> /// Gets the deltas as a dictionary of key and deltas for the block. /// </summary> /// <param name="project">The project.</param> /// <param name="block">The block.</param> /// <param name="wordDelta">The word delta.</param> /// <param name="characterDelta">The character delta.</param> /// <param name="nonWhitespaceDelta">The non whitespace delta.</param> /// <returns> /// A dictionary of paths and deltas. /// </returns> public static Dictionary<HierarchicalPath, int> GetDeltas( IProjectPlugin project, Block block, int delta, int wordDelta, int characterDelta, int nonWhitespaceDelta) { // Make sure we have a sane arguments. if (project == null) { throw new ArgumentNullException("project"); } if (block == null) { throw new ArgumentNullException("block"); } // Create the dictionary and figure out the top-level elements. var deltas = new Dictionary<HierarchicalPath, int>(); HierarchicalPath rootPath = GetPluginRootPath(project); // Add in the path for the totals. var totalPath = new HierarchicalPath("Total", rootPath); AddDeltas( deltas, totalPath, delta, wordDelta, characterDelta, nonWhitespaceDelta); // Add in a block-type specific path along with a counter. string relativeBlockPath = "Block Types/" + block.BlockType.Name; var blockPath = new HierarchicalPath(relativeBlockPath, rootPath); AddDeltas( deltas, blockPath, delta, wordDelta, characterDelta, nonWhitespaceDelta); // Return the resulting delta. return deltas; }
private static int GetCount( IPropertiesContainer propertiesContainer, HierarchicalPath rootPath, string countType) { var path = new HierarchicalPath(countType, rootPath); string count; return propertiesContainer.Properties.TryGetValue(path, out count) ? Convert.ToInt32(count) : 0; }
public void TestEscapedNone() { var nr = new HierarchicalPath("/a/b/c"); Assert.AreEqual("/a/b/c", nr.ToString(), "String comparison"); }
public void Count1() { var nr = new HierarchicalPath("/a/b/c"); Assert.AreEqual(3, nr.Count); }
public void DoubleDotTop() { var nr = new HierarchicalPath("/a/.."); Assert.AreEqual("/", nr.Path); }
public void DoubleDot() { var nr = new HierarchicalPath("/a/b/../c"); Assert.AreEqual("/a/c", nr.Path); }
public void Count3() { var nr = new HierarchicalPath("/"); Assert.AreEqual(0, nr.Count); }
public void Count2() { var nr = new HierarchicalPath("/a"); Assert.AreEqual(1, nr.Count); }
static FilesystemPersistenceProjectPlugin() { RootSettingsKey = new HierarchicalPath("/Persistence/Filesystem/"); }
/// <summary> /// Determines whether the specified path is match for the selector. /// </summary> /// <param name="path">The path.</param> /// <param name="startIndex">The start index.</param> /// <returns> /// <c>true</c> if the specified path is match; otherwise, <c>false</c>. /// </returns> public bool IsMatch( HierarchicalPath path, int startIndex) { // If the selector is longer than the given path, it will never // match the path. if (Levels.Count > path.Levels.Count) { return false; } // Go through all the elements of the selector. for (int selectorIndex = 0, pathIndex = startIndex; selectorIndex < Levels.Count; selectorIndex++) { // Check to see if we have a special function for the selector's // level. object operation = operations[selectorIndex]; string pathLevel = path.Levels[pathIndex]; if (operation == null) { // Just do a string comparison between the two levels. if (Levels[selectorIndex] != pathLevel) { // It doesn't match, so return false. return false; } pathIndex++; continue; } // If we are regular expression, then use that. if (operation is Regex) { if (!((Regex) operation).IsMatch(pathLevel)) { // It doesn't match the regular expression. return false; } pathIndex++; continue; } // If we got this far, we are a double-star match. If this is // the last index in the selector, it will match everything. if (selectorIndex == Levels.Count - 1) { return true; } // Loop through the remaing elements of the path and see if // we can find a subtree match. var subSelector = (HierarchicalSelector) operation; for (int starIndex = pathIndex; starIndex < path.Levels.Count; starIndex++) { // If this is a match, use it. if (subSelector.IsMatch(path, starIndex)) { return true; } } // We couldn't find a subtree match. return false; } // If we got through the entire loop, we have a match. return true; }
/// <summary> /// Determines whether the specified path is match for the selector. /// </summary> /// <param name="path">The path.</param> /// <returns> /// <c>true</c> if the specified path is match; otherwise, <c>false</c>. /// </returns> public bool IsMatch(string path) { var hierarchicalPath = new HierarchicalPath(path); return IsMatch(hierarchicalPath); }
/// <summary> /// Adds the deltas for the various counters underneath the given path. /// </summary> /// <param name="deltas">The deltas.</param> /// <param name="rootPath">The root path.</param> /// <param name="delta"></param> /// <param name="wordDelta">The word delta.</param> /// <param name="characterDelta">The character delta.</param> /// <param name="nonWhitespaceDelta">The non whitespace delta.</param> private static void AddDeltas( IDictionary<HierarchicalPath, int> deltas, HierarchicalPath rootPath, int delta, int wordDelta, int characterDelta, int nonWhitespaceDelta) { AddDeltas(deltas, rootPath, CountType, delta); AddDeltas(deltas, rootPath, WordCountType, wordDelta); AddDeltas(deltas, rootPath, CharacterCountType, characterDelta); AddDeltas(deltas, rootPath, NonWhitespaceCountType, nonWhitespaceDelta); AddDeltas( deltas, rootPath, WhitespaceCountType, characterDelta - nonWhitespaceDelta); }
/// <summary> /// Adds a delta for a given path. /// </summary> /// <param name="deltas">The deltas.</param> /// <param name="rootPath">The root path.</param> /// <param name="type">The type.</param> /// <param name="delta">The delta.</param> private static void AddDeltas( IDictionary<HierarchicalPath, int> deltas, HierarchicalPath rootPath, string type, int delta) { var path = new HierarchicalPath(type, rootPath); if (deltas.ContainsKey(path)) { deltas[path] += delta; } else { deltas[path] = delta; } }