A node representing a graph root.
Esempio n. 1
0
        /// <summary>
        /// Initializes a new instance of <see cref="NodeSnippet"/>.
        /// </summary>
        /// <param name="node">Initializes the required <see cref="Node"/>.</param>
        public NodeSnippet(Node node)
        {
            // Data validation
            if (null == node)
                throw new ArgumentNullException("node");

            // Initialize
            this.Node = node;
        }
        /// <summary>
        /// Builds a snippet from a directory info.
        /// </summary>
        /// <param name="rootDirectory">The root directory.</param>
        /// <param name="directoryInfo">The directory info.</param>
        /// <returns>The built snippet.</returns>
        private Snippet BuildSnippet(DirectoryInfoBase rootDirectory, FileSystemInfoBase[] directoryInfo)
        {
            // Compute root directory name
            string rootDirectoryName = rootDirectory.Name;
            if (string.IsNullOrWhiteSpace(rootDirectoryName))
            {
                rootDirectoryName = Path.GetFileName(rootDirectory.FullName);
            }

            // Create root node
            Node root = new Node(rootDirectoryName, false);

            // Browse all file system info matching the search pattern
            foreach (FileSystemInfoBase fileSystemInfo in directoryInfo.Where(x => rootDirectory.FullName != x.FullName))
            {
                // Compute relative path
                string relativePath = fileSystemInfo.FullName.Substring(1 + rootDirectory.FullName.Length);

                // Initialize the current node to root and process each search result
                Node currentNode = root;
                while (relativePath.Contains(Path.DirectorySeparatorChar))
                {
                    // Compute current node name from node path
                    int pos = relativePath.IndexOf(Path.DirectorySeparatorChar);
                    string nodeName = relativePath.Substring(0, pos);

                    // In the node already exists, just flip the current node
                    if (currentNode.Children.ContainsKey(nodeName))
                    {
                        currentNode = currentNode.Children[nodeName];
                    }

                    // Otherwise create a new child and flip the current node
                    else
                    {
                        Node tmpNode = new Node(nodeName, false);
                        currentNode.Children[nodeName] = tmpNode;
                        currentNode = tmpNode;
                    }

                    // Update the current relative path
                    relativePath = relativePath.Substring(1 + pos);
                }

                // Compute last filesystem info name
                string fileSystemInfoName = fileSystemInfo.Name;
                if (string.IsNullOrWhiteSpace(fileSystemInfoName))
                {
                    fileSystemInfoName = Path.GetFileName(fileSystemInfo.FullName);
                }

                // Add the remaining relative path as node
                currentNode.Children[relativePath] = new Node(fileSystemInfoName, fileSystemInfo is FileInfoBase);
            }

            // Return root as node snippet
            return new NodeSnippet(root);
        }