Example #1
0
            /// <summary>
            /// Gets all headers of the given <paramref name="headerTypes"/>.
            /// </summary>
            /// <param name="root">the root node</param>
            /// <param name="headers">the list that receives the headers</param>
            /// <param name="headerTypes">the types of headers to include</param>
            /// <param name="includeChildren">whether to include the children</param>
            /// <param name="includeHeader">whether to include the header itself</param>
            private static void GetHeadersOfType(IResultNode root, List <IResultNode> headers, ICollection <string> headerTypes, bool includeChildren, bool includeHeader)
            {
                if (headerTypes.Contains(root.Name))
                {
                    if (includeChildren)
                    {
                        // Include the header as-is, including its children
                        if (includeHeader)
                        {
                            headers.Add(root);
                        }
                        else
                        {
                            headers.AddRange(root.Children);
                        }
                    }
                    else
                    {
                        // Make shallow copies (exclude children)
                        if (includeHeader)                         // Copy the header
                        {
                            headers.Add(root.ShallowCopy());

                            foreach (IResultNode child in root.Children)
                            {
                                GetHeadersOfType(child, headers, headerTypes, includeChildren, includeHeader);
                            }
                        }
                        else
                        {
                            foreach (IResultNode child in root.Children)
                            {
                                headers.Add(child.ShallowCopy());

                                foreach (IResultNode grandChild in child.Children)
                                {
                                    GetHeadersOfType(grandChild, headers, headerTypes, includeChildren, includeHeader);
                                }
                            }
                        }
                    }
                }
                else
                {
                    // Header not in listed types, check children
                    foreach (IResultNode childResultNode in root.Children)
                    {
                        GetHeadersOfType(childResultNode, headers, headerTypes, includeChildren, includeHeader);
                    }
                }
            }
        /// <summary>
        /// Creates a deep copy of <paramref name="result"/> and its children.
        /// </summary>
        /// <param name="result">the result to copy</param>
        /// <return>the deep copy</return>
        public static IResultNode DeepCopy(this IResultNode result)
        {
            if (result == null)
            {
                throw new ArgumentNullException("result");
            }

            IResultNode resultCopy = result.ShallowCopy();

            // Create copies of children
            foreach (IResultNode childResult in result.Children)
            {
                resultCopy.AddChild(childResult.DeepCopy());
            }
            return(resultCopy);
        }