private CommandNode SearchDeeper(CommandNode node, ref String line, String command) { // We try to find maximum deep for typed command. var nodes = new List <CommandNode>(); var result = node; var splitted = line.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries); if (splitted.Length > 0) { command += " " + splitted[0]; String[] cs = command.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries); nodes = node.Nodes.Where(p => p.Key == cs[cs.Length - 1]).Select(p => p.Value).ToList(); if (nodes.Count == 1) { // Remove only the first entrance of line! int index = line.IndexOf(cs[cs.Length - 1]); if (index != -1) { line = line.Substring(cs[cs.Length - 1].Length).Trim(); } result = SearchDeeper(nodes.First(), ref line, command); } } return(result); }
private String GetFullName(String fullName, CommandNode parent) { if (parent.Parent != null) { fullName = GetFullName(fullName, parent.Parent); } return(fullName + " " + parent.Name); }
private List <CommandNode> GetNodes(List <CommandNode> collection, CommandNode parent) { foreach (KeyValuePair <string, CommandNode> pair in parent.Nodes) { if (pair.Value.Command != null) { collection.Add(pair.Value); } GetNodes(collection, pair.Value); } return(collection); }
public void CreationOneLevelCommandTest() { var node = new CommandNode(); var command = new Command() {Name = "show"}; node.Add(command); Assert.IsTrue(node.Parent == null); Assert.IsNull(node.Command); Assert.IsTrue(node.Nodes.ContainsKey("show")); Assert.IsTrue(node.Nodes["show"].Command == command); // Assert.IsTrue(node.Nodes.First().Value.First().Command == "show"); }
/// <summary> /// Searches the specified node. /// </summary> /// <param name="node">The node.</param> /// <param name="line">The line.</param> /// <returns></returns> public List <CommandNode> Search(CommandNode node, String line) { var splitted = line.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries); if (splitted.Length > 0) { if (splitted.Length == 1) { return((from pair in node.Nodes where pair.Key.IndexOf(splitted[0]) == 0 select pair.Value).ToList()); } if (node.Nodes.ContainsKey(splitted[0])) { return(Search(node.Nodes[splitted[0]], line.Replace(splitted[0], String.Empty).Trim())); } } return(new List <CommandNode>()); }
public void CreationThreeLevelCommandTest2() { var node = new CommandNode(); var command1 = new Command { Name = "show config" }; var command2 = new Command { Name = "show" }; node.Add(command1); node.Add(command2); Assert.IsTrue(node.Parent == null); Assert.IsNull(node.Command); Assert.IsTrue(node.Nodes.ContainsKey("show")); Assert.IsTrue(node.Nodes["show"].Command == command2); Assert.IsTrue(node.Nodes["show"].Nodes.ContainsKey("config")); Assert.IsTrue(node.Nodes["show"].Nodes["config"].Command == command1); }
/// <summary> /// Creates the node. /// </summary> /// <param name="node">The node.</param> /// <param name="command">The command.</param> /// <param name="line">The line.</param> private void CreateNode(CommandNode node, Command command, String line) { string[] splitted = line.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries); if (splitted.Length > 0) { String key = splitted[0]; Command reference = splitted.Length == 1 ? command : null; CommandNode created = new CommandNode(key, reference, node); if (!node.Nodes.ContainsKey(key)) { node.Nodes.Add(key, created); } if (splitted.Length != 1) // it'sn't last node { CreateNode(node.Nodes[key], command, line.Replace(key, String.Empty).Trim()); } else // this is last node { // We have to set command value, because this node can be create before we // had command. node.Nodes[key].Command = reference; } } }
/// <summary> /// Initializes a new instance of the <see cref="CommandNode"/> class. /// </summary> /// <param name="name">The name.</param> /// <param name="command">The command.</param> /// <param name="parent">The parent.</param> public CommandNode(string name, Command command, CommandNode parent) { Name = name; Command = command; Parent = parent; }
private CommandNode SearchDeeper(CommandNode node, ref String line, String command) { // We try to find maximum deep for typed command. var nodes = new List<CommandNode>(); var result = node; var splitted = line.Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries); if (splitted.Length > 0) { command += " " + splitted[0]; String[] cs = command.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries); nodes = node.Nodes.Where(p => p.Key == cs[cs.Length - 1]).Select(p => p.Value).ToList(); if (nodes.Count == 1) { // Remove only the first entrance of line! int index = line.IndexOf(cs[cs.Length - 1]); if (index != -1) { line = line.Substring(cs[cs.Length - 1].Length).Trim(); } result = SearchDeeper(nodes.First(), ref line, command); } } return result; }
public void GetLinearNodesTest() { var node = new CommandNode(); var command1 = new Command { Name = "show config" }; var command2 = new Command { Name = "show" }; var command3 = new Command { Name = "show test" }; var command4 = new Command { Name = "linear" }; node.Add(command1); node.Add(command2); node.Add(command3); node.Add(command4); int count = 0; foreach (var n in node.LinearNodes) { count++; } Assert.IsTrue(count == 4); }
private String GetFullName(String fullName, CommandNode parent) { if (parent.Parent != null) { fullName = GetFullName(fullName, parent.Parent); } return fullName + " " + parent.Name; }
/// <summary> /// Searches the specified node. /// </summary> /// <param name="node">The node.</param> /// <param name="line">The line.</param> /// <returns></returns> public List<CommandNode> Search(CommandNode node, String line) { var splitted = line.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries); if (splitted.Length > 0) { if (splitted.Length == 1) { return (from pair in node.Nodes where pair.Key.IndexOf(splitted[0]) ==0 select pair.Value).ToList(); } if (node.Nodes.ContainsKey(splitted[0])) { return Search(node.Nodes[splitted[0]], line.Replace(splitted[0], String.Empty).Trim()); } } return new List<CommandNode>(); }
public void SearchThreeLevelCommandTest2() { var node = new CommandNode(); var command1 = new Command { Name = "show config" }; var command2 = new Command { Name = "show" }; node.Add(command1); node.Add(command2); List<CommandNode> cn1 = node.Search("show"); Assert.IsNotNull(cn1); Assert.IsTrue(cn1.Count == 1); Assert.IsTrue(cn1.First().Name == "show"); Assert.IsNotNull(cn1.First().Command); List<CommandNode> cn2 = node.Search("test"); Assert.IsNotNull(cn2); Assert.IsTrue(cn2.Count == 0); List<CommandNode> cn3 = node.Search("sh"); Assert.IsNotNull(cn3); Assert.IsTrue(cn3.First().Name == "show"); Assert.IsNotNull(cn3.Count == 1); List<CommandNode> cn4 = node.Search("show c"); Assert.IsNotNull(cn4); Assert.IsTrue(cn4.First().Name == "config"); Assert.IsNotNull(cn3.Count == 1); }
public void SearchDeeperTest4() { var node = new CommandNode(); var command1 = new Command { Name = "show config" }; var command2 = new Command { Name = "show" }; var command3 = new Command { Name = "show test" }; node.Add(command1); node.Add(command2); node.Add(command3); string line = "show"; Assert.IsTrue(node.SearchDeeper(ref line).FullName == "show"); Assert.IsTrue(String.IsNullOrEmpty(line)); }
private List<CommandNode> GetNodes(List<CommandNode> collection, CommandNode parent) { foreach (KeyValuePair<string, CommandNode> pair in parent.Nodes) { if (pair.Value.Command != null) { collection.Add(pair.Value); } GetNodes(collection, pair.Value); } return collection; }
public void GetFullNameTest() { var node = new CommandNode(); var command1 = new Command { Name = "show config" }; var command2 = new Command { Name = "show" }; var command3 = new Command { Name = "show test" }; node.Add(command1); node.Add(command2); node.Add(command3); Assert.IsTrue(node.Nodes["show"].FullName == "show"); Assert.IsTrue(node.Nodes["show"].Nodes["config"].FullName == "show config"); Assert.IsTrue(node.Nodes["show"].Nodes["test"].FullName == "show test"); }