//Search with "" parameter to traverse tree (DFS) and to write in console public TagItem Search(string expr) { TagItem legacy = null; if (_nodeName == expr) { return(this); } foreach (TagItem item in childList) { if (expr == "") { item._Print(); } if (item.fullName == expr) { return(item); } legacy = item.Search(expr); if (legacy != null) { return(legacy); } } return(null); }
static public void Remove() { TagItem root = storage.root; Console.WriteLine("What tag you want to remove?\n" + "Name of tag with path require"); string expr = Console.ReadLine(); //Get wanted tag TagItem tag = root.Search(expr); if (tag != null) { if (tag.parent != null) { tag.parent.Remove(tag); } else { //Attempt to remove a structure's root storage = new TagStorage(FILE_NAME); } Console.WriteLine("Deleted"); return; } Console.WriteLine("Tag is absent. Nothing delete"); }
static public void SetContent() { TagItem root = storage.root; Console.WriteLine("What\'s tag content changing?"); string tag = Console.ReadLine(); TagItem item = root.Search(tag); if (item != null) { Console.WriteLine("Put something"); string val = Console.ReadLine(); //Cause dataType figure at instance initiation time //swap TagItem's items with matching name but different type //all childs renaming tag get lost TagItem newTag = new TagItem(item.NodeName, val, item.parent); if (item == root) { Console.WriteLine("Root of tree is stricly \"none\" type in terms of a test exercise"); return; } item.childList.Clear(); item.parent.Remove(item); item.parent.Add(newTag); Console.WriteLine("{0} is set. Data type : {1}. Value : {2}", newTag.fullName, newTag.dataType, newTag.content); return; } Console.WriteLine("Tag is absent."); }
//Move tag along a tree static public void Transfer() { TagItem root = storage.root; Console.WriteLine("What\'s tag will be NEW parent?"); string parent = Console.ReadLine(); TagItem parentItem = root.Search(parent); //Statement of existing both nodes in tree //and is moving node isn't parent relative to other if (parentItem != null) { Console.WriteLine("What\'s tag is moving?"); string tag = Console.ReadLine(); TagItem tagItem = root.Search(tag); if (tagItem != null) { tagItem.Transfer(parentItem); return; } Console.WriteLine("Transfer is aborted : There is no {0}", tag); return; } Console.WriteLine("Transfer is aborted : There is no {0}", parent); }
static public void Print() { TagItem tag = storage.root; Console.WriteLine("{0} Level={1} {2} {3}", tag.fullName, tag.level, tag.dataType, tag.content); tag.Search(""); }
private void _InitTree(XmlNode node, TagItem tagItem) { XmlNodeList childs = node.ChildNodes; XmlNode child; TagItem tI; //Node isn't content a content and other node at the same time //One node in list is empty or string including node if (childs.Count > 0) { for (int i = 0; i < childs.Count; i++) { child = childs.Item(i); //Checking it's child no more include tag if (child.LocalName == "#text") { tI = new TagItem(tagItem.NodeName, child.Value, tagItem.parent); try{ tagItem.parent.Add(tI); tagItem.parent.Remove(tagItem); } catch (NullReferenceException) { Console.WriteLine("In terms of a task a root " + "node have to a \"none\" type cause " + "it doesn\'t read xml files contaning" + " numeric root"); //System.Environment.Exit(0); return; } continue; } //Assign tag name for TagItem's item with empty content tI = new TagItem(child.Name, "", tagItem); tagItem.Add(tI); //go in depth _InitTree(child, tI); } } else { //There isn't tag anymore. Assign content tagItem.content = node.Value; return; } }
public TagItem(string name, string number, TagItem parentNode, string type = "none") { _nodeName = name; content = number; parent = parentNode; childList = new List <TagItem>(); //Content initiat at instance creating dataType = type; if (type == "none") { _DataTypeReview(); } //Figure out attributes: level, pathNode, fullName _LevelCount(); _AbsolutePath(); _FullName(); }
public void Transfer(TagItem newParent) { //Checking newParent is not for child of moving node TagItem tag = Search(newParent.fullName); if (tag != null) { Console.WriteLine("Transfer aborted. {0} is parent for {1}", fullName, newParent.fullName); return; } newParent.Add(this); parent.Remove(this); parent = newParent; _Refresh(); Console.WriteLine("Transfer done."); }
static public void Add() { TagItem root = storage.root; Console.WriteLine("Choose parent tag"); string path = Console.ReadLine(); TagItem parent = root.Search(path); if (parent != null) { Console.WriteLine("Suggest name of tag"); string name = Console.ReadLine(); Console.WriteLine("What\'s data type it will be?"); string type = Console.ReadLine(); TagItem tag; switch (type) { case "int": tag = new TagItem(name, "", parent, type); break; case "float": tag = new TagItem(name, "", parent, type); break; case "bool": tag = new TagItem(name, "", parent, type); break; default: tag = new TagItem(name, "", parent, "none"); break; } parent.Add(tag); Console.WriteLine("{0} is added to tree", tag.fullName); return; } Console.WriteLine("Tag {0} is absent. Nothing add", path); }
static public void Rename() { TagItem root = storage.root; Console.WriteLine("What\'s tag rename?"); string name = Console.ReadLine(); TagItem tag = root.Search(name); if (tag != null) { Console.WriteLine("What\'s new name?"); string newName = Console.ReadLine(); Console.WriteLine("{0} is renamed to {1}", tag.NodeName, newName); tag.NodeName = newName; return; } Console.WriteLine("Tag is absent. Nothing rename"); }
public TagStorage(string fileName) { root = new TagItem("Root", "", null); //For LoadXml method this.fileName = fileName; }
public void Remove(TagItem item) { childList.Remove(item); }
public void Add(TagItem item) { childList.Add(item); }