예제 #1
0
        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);
        }
예제 #2
0
        /// <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;
        }
예제 #3
0
        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);
            }
        }
예제 #4
0
        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);
        }
예제 #5
0
        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);
        }
예제 #6
0
        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);
        }
예제 #7
0
        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);
        }
예제 #8
0
        /// <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));
        }
예제 #9
0
        private NoteNode ReadTag(StreamReader stream, NoteNode parent)
        {
            var node = GetNodeTagFromStream(stream, parent);

            return(GetNodeContentFromStream(stream, node));
        }
예제 #10
0
        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);
        }