private NoteNode GetNodeContentFromStream(StreamReader stream, NoteNode node) { var builder = new StringBuilder(); var count = 0; while (!stream.EndOfStream) { //read the tag until ">" then read in content until end tag "</>" or "<" and recruse var newchar = stream.Read(); if (newchar != '<') { builder.Append((char)newchar); } else { if (stream.Peek() == '/') { stream.Read(); if (stream.Peek() == '>') { stream.Read(); node.Content = builder.ToString(); return(node); } throw new Exception("Bad Formed tag"); } builder.Append("{" + count + "}"); count++; ReadTag(stream, node); } } return(node); }
/// <summary> /// Initializes a NoteTree given a root node. /// </summary> /// <param name="root">The NoteNode rooting the tree.</param> public NoteTree(NoteNode root) { if (null == root) { throw new ArgumentNullException("root"); } _root = root; }
public NoteNode(string tag, string content, NoteNode parent) { Tag = tag; Content = content; Parent = parent; Children = new List <NoteNode>(); if (parent != null) { parent.Children.Add(this); } }
private IEnumerable <string> RecurseGetTags(List <string> list, NoteNode node) { if (!list.Contains(node.Tag)) { list.Add(node.Tag); } foreach (var noteNode in node.Children) { RecurseGetTags(list, noteNode); } return(list); }
private IEnumerable <NoteNode> RecurseGetNotesByTag(List <NoteNode> list, NoteNode node) { if (!list.Contains(node)) { list.Add(node); } foreach (var noteNode in node.Children) { RecurseGetNotesByTag(list, noteNode); } return(list); }
private bool RecurseSearchTag(NoteNode node, string tag) { if (node.Tag == tag) { return(true); } foreach (var noteNode in node.Children) { if (RecurseSearchTag(noteNode, tag)) { return(true); } } return(false); }
private static NoteNode GetNodeTagFromStream(StreamReader stream, NoteNode parent) { var node = new NoteNode(null, null, parent); var builder = new StringBuilder(); while (!stream.EndOfStream) { var newChar = stream.Read(); if (newChar != '>') { builder.Append((char)newChar); } else { break; } } node.Tag = builder.ToString(); return(node); }
/// <summary> /// Parses the file contents and provides a tree structure of the input file. /// </summary> /// <returns>A <see cref="NoteTree" /> containg the contents of the ntf file.</returns> public NoteTree GetTree() { var stream = new MemoryStream(); NoteNode notes = null; using (var writer = new StreamWriter(stream)) { writer.AutoFlush = true; writer.Write(_noteContents); stream.Position = 0; using (var reader = new StreamReader(stream)) { //advance stream to beginning tag while (reader.Read() != '<' && !reader.EndOfStream) { ; } notes = ReadTag(reader, null); } } return(new NoteTree(notes)); }
private NoteNode ReadTag(StreamReader stream, NoteNode parent) { var node = GetNodeTagFromStream(stream, parent); return(GetNodeContentFromStream(stream, node)); }
private Dictionary <string, int> RecurseGetTags(Dictionary <string, int> list, NoteNode node) { if (!list.ContainsKey(node.Tag)) { list.Add(node.Tag, 1); } else { list[node.Tag]++; } foreach (var noteNode in node.Children) { RecurseGetTags(list, noteNode); } return(list); }