Ejemplo n.º 1
0
        private T?GetFirstDescendantInternal <T>(Func <Fb2Node, bool>?predicate = null)
            where T : Fb2Node
        {
            if (IsEmpty)
            {
                return(null);
            }

            if (predicate == null)
            {
                predicate = PredicateResolver.GetPredicate <T>();
            }

            for (int i = 0; i < content.Count; i++)
            {
                var element = content[i];

                if (predicate(element))
                {
                    return((T)element);
                }

                if (element is Fb2Container containerElement)
                {
                    var desc = containerElement.GetFirstDescendantInternal <T>(predicate);
                    if (desc != null)
                    {
                        return(desc);
                    }
                }
            }

            return(null);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets children of element by given node type (Fb2Node-based nodes).
        /// </summary>
        /// <typeparam name="T">Node type to select elements by.</typeparam>
        /// <returns>List of found child elements, if any.</returns>
        public IEnumerable <T> GetChildren <T>() where T : Fb2Node
        {
            if (IsEmpty)
            {
                return(Enumerable.Empty <T>());
            }

            var predicate = PredicateResolver.GetPredicate <T>();
            var result    = content.Where(predicate);

            return(result.Any() ? result.Cast <T>() : Enumerable.Empty <T>());
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Gets first matching child of element by given node type (Fb2Node-based nodes).
        /// </summary>
        /// <param name="name">Node type to select child element by.</param>
        /// <returns>First matched child node</returns>
        public T?GetFirstChild <T>() where T : Fb2Node
        {
            if (IsEmpty)
            {
                return(null);
            }

            var predicate = PredicateResolver.GetPredicate <T>();
            var result    = content.FirstOrDefault(predicate);

            if (result == null)
            {
                return(null);
            }

            return(result as T);
        }
Ejemplo n.º 4
0
        private IEnumerable <T> GetDescendantsInternal <T>(Func <Fb2Node, bool>?predicate = null)
            where T : Fb2Node
        {
            if (IsEmpty)
            {
                return(Enumerable.Empty <T>());
            }

            var result = new List <Fb2Node>();

            if (predicate == null)
            {
                predicate = PredicateResolver.GetPredicate <T>();
            }

            for (int i = 0; i < content.Count; i++)
            {
                var element = content[i];

                if (predicate(element))
                {
                    result.Add(element);
                }

                if (element is Fb2Container containerElement)
                {
                    var desc = containerElement.GetDescendantsInternal <T>(predicate);
                    if (desc.Any())
                    {
                        result.AddRange(desc);
                    }
                }
            }

            return(result.Cast <T>());
        }