示例#1
0
        public void GetDesktop()
        {
            Properties = null;
            Patterns   = null;
            Image      = null;
            Elements   = new ObservableCollection <ElementBO>();
            var item = _automation.GetDesktop();

            SearchResults      = new ISHAutomationElement[0];
            CurrentSearchIndex = 0;
            _treeWalker        = _automation.TreeWalkerFactory.GetControlViewWalker();
            DesktopItem        = item != null ? item : null;
            var items = DesktopItem.FindAllByXPath("*").Select(x => new ElementBO(new SHAutomationElement(x.FrameworkAutomationElement)));

            foreach (var win in items)
            {
                if (SavedSettingsWindows.Any(x => (!string.IsNullOrEmpty(win.AutomationId?.Trim()) && win.AutomationId.StartsWith(x.Identifier)) || (!string.IsNullOrEmpty(win.Name?.Trim()) && win.Name.StartsWith(x.Identifier))))
                {
                    if (!Elements.Any(x => x.AutomationElement.Equals(win.AutomationElement)))
                    {
                        Elements.Add(win);
                        Elements.ToList().ForEach(x => x.IsExpanded = true);
                    }
                }
            }
            if (Elements.Any())
            {
                SelectedItem = Elements.First();
            }

            _dispatcherTimer          = new DispatcherTimer();
            _dispatcherTimer.Tick    += DispatcherTimerTick;
            _dispatcherTimer.Interval = TimeSpan.FromMilliseconds(1);
            _dispatcherTimer.Start();
        }
示例#2
0
        /// <summary>
        /// Gets children based on a predicate.
        /// </summary>
        /// <typeparam name="T">The type of elements in the tree.</typeparam>
        /// <param name="walker">
        /// The <see cref="ITreeWalker&lt;T&gt;"/> that knows how to find the parent and child
        /// nodes.
        /// </param>
        /// <param name="node">
        /// The root node to be queried.
        /// </param>
        /// <param name="predicate">
        /// A predicate to test each child for selection.
        /// </param>
        /// <returns>
        /// An <see cref="System.Collections.Generic.IEnumerable&lt;T&gt;"/> that contains all the
        /// matching children.
        /// </returns>
        public static IEnumerable <T> GetChildren <T>(
            this ITreeWalker <T> walker,
            T node,
            Func <T, bool> predicate)
        {
            // Validate parameters.
            if (walker == null)
            {
                throw new ArgumentNullException("walker");
            }

            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            if (predicate == null)
            {
                throw new ArgumentNullException("predicate");
            }

            return
                (walker
                 .GetChildren(node)
                 .Where(predicate));
        }
        /// <summary>
        /// Gets the child at the specified index or the default value of the specified type.
        /// </summary>
        /// <typeparam name="T">The type of elements in the tree.</typeparam>
        /// <param name="walker">
        /// The <see cref="ITreeWalker&lt;T&gt;"/> that knows how to find the parent and child
        /// nodes.
        /// </param>
        /// <param name="node">The node whose child is to be returned.</param>
        /// <param name="index">The index of the child to retrieve.</param>
        /// <returns>The child node at the specified index or the default value.</returns>
        /// /// <exception cref="ArgumentNullException">
        /// If <paramref name="walker"/> or <paramref name="node"/> is null.
        /// </exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// If <paramref name="index"/> is less than zero.
        /// </exception>
        public static T GetChildAtOrDefault <T>(
            this ITreeWalker <T> walker,
            T node,
            int index)
        {
            // Validate parameters.
            if (walker == null)
            {
                throw new ArgumentNullException("walker");
            }

            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            if (index < 0)
            {
                throw new ArgumentOutOfRangeException("index");
            }

            return
                (walker
                 .GetChildren(node)
                 .ElementAtOrDefault(index));
        }
示例#4
0
 public OrderChildrenByTreeWalker(
     ITreeWalker <T> walker,
     CompoundComparer <T> comparers)
 {
     this._Walker    = walker;
     this._Comparers = comparers;
 }
        /// <summary>
        /// Gets the child at the specified index.
        /// </summary>
        /// <typeparam name="T">The type of elements in the tree.</typeparam>
        /// <param name="walker">
        /// The <see cref="ITreeWalker&lt;T&gt;"/> that knows how to find the parent and child
        /// nodes.
        /// </param>
        /// <param name="node">The node whose child is to be returned.</param>
        /// <param name="index">The index of the child to retrieve.</param>
        /// <returns>The child node at the specified index.</returns>
        /// <exception cref="ArgumentOutOfRangeException">
        /// Thrown if the index is less than 0 or greater than or equal to the number of children.
        /// </exception>
        public static T GetChildAt <T>(this ITreeWalker <T> walker, T node, int index)
        {
            if (walker == null)
            {
                throw new ArgumentNullException("walker");
            }

            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            if (index < 0)
            {
                throw new ArgumentOutOfRangeException("index");
            }

            T child;

            if (walker.TryGetChildAt(node, index, out child))
            {
                return(child);
            }
            else
            {
                throw new ArgumentOutOfRangeException("index");
            }
        }
        /// <summary>
        /// Gets the child at the specified index or the default value if the index is out of
        /// range.
        /// </summary>
        /// <typeparam name="T">The type of elements in the tree.</typeparam>
        /// <param name="walker">
        /// The <see cref="ITreeWalker&lt;T&gt;"/> that knows how to find the parent and child
        /// nodes.
        /// </param>
        /// <param name="nodes">The nodes whose children are to be returned.</param>
        /// <param name="index">The index of the child to retrieve.</param>
        /// <returns>
        /// The child node at the specified index or default values for each of the nodes in
        /// <see cref="nodes"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// If <paramref name="walker"/> or <paramref name="nodes"/> is null.
        /// </exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// If <paramref name="index"/> is less than zero.
        /// </exception>
        public static IEnumerable <T> GetChildAtOrDefault <T>(
            this ITreeWalker <T> walker,
            IEnumerable <T> nodes,
            int index)
        {
            // Validate parameters.
            if (walker == null)
            {
                throw new ArgumentNullException("walker");
            }

            if (nodes == null)
            {
                throw new ArgumentNullException("nodes");
            }

            if (index < 0)
            {
                throw new ArgumentOutOfRangeException("index");
            }

            return
                (nodes
                 .Select(n =>
                         walker
                         .GetChildren(n)
                         .ElementAtOrDefault(index)));
        }
        /// <summary>
        /// Gets the child at the specified index.
        /// </summary>
        /// <typeparam name="T">The type of elements in the tree.</typeparam>
        /// <param name="walker">
        /// The <see cref="ITreeWalker&lt;T&gt;"/> that knows how to find the parent and child
        /// nodes.
        /// </param>
        /// <param name="nodes">The nodes whose children are to be returned.</param>
        /// <param name="index">The index of the child to retrieve.</param>
        /// <returns>
        /// The child node at the specified index for each of the nodes in <see cref="nodes"/>.
        /// </returns>
        /// <exception cref="ArgumentOutOfRangeException">
        /// Thrown if the index is less than 0 or greater than or equal to the number of children.
        /// </exception>
        public static IEnumerable <T> GetChildAt <T>(this ITreeWalker <T> walker, IEnumerable <T> nodes, int index)
        {
            if (walker == null)
            {
                throw new ArgumentNullException("walker");
            }

            if (nodes == null)
            {
                throw new ArgumentNullException("nodes");
            }

            if (index < 0)
            {
                throw new ArgumentOutOfRangeException("index");
            }

            foreach (T node in nodes)
            {
                T child;
                if (walker.TryGetChildAt(node, index, out child))
                {
                    yield return(child);
                }
            }
        }
示例#8
0
        /// <summary>
        /// Gets the nearest descendants that match the <paramref name="key"/>.
        /// </summary>
        /// <typeparam name="T">The type of elements in the tree.</typeparam>
        /// <param name="walker">
        /// The <see cref="ITreeWalker&lt;T&gt;"/> that knows how to find the parent and child
        /// nodes.
        /// </param>
        /// <param name="node">
        /// The root node to be queried.
        /// </param>
        /// <param name="key">
        /// The key each node will be compared to.
        /// </param>
        /// <param name="comparer">
        /// The <see cref="IEqualityComparer&lt;T&gt;"/> used to compare the key and the node.  If
        /// this is null then the default <see cref="EqualityComparer&lt;T&gt;.Default"/> will be
        /// used.
        /// </param>
        /// <returns>
        /// An <see cref="System.Collections.Generic.IEnumerable&lt;T&gt;"/> that contains all the
        /// matching nodes in the tree ordered based on a pre-order traversal.
        /// </returns>
        public static IEnumerable <T> GetDescendants <T>(
            this ITreeWalker <T> walker,
            T node,
            T key,
            IEqualityComparer <T> comparer)
        {
            // Validate parameters.
            if (walker == null)
            {
                throw new ArgumentNullException("walker");
            }

            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            // If the comparer is null then use the defautl comparer for that type.
            comparer = comparer ?? EqualityComparer <T> .Default;

            return(walker.GetDescendants(node, n => comparer.Equals(n, key)));
        }
示例#9
0
        /// <summary>
        /// Tries to get a node's parent.
        /// </summary>
        /// <typeparam name="T">The type of elements in the tree.</typeparam>
        /// <param name="walker">
        /// The <see cref="ITreeWalker&lt;T&gt;"/> that knows how to find the parent and child
        /// nodes.
        /// </param>
        /// <param name="node">
        /// The node whose parent is to be returned.
        /// </param>
        /// <param name="parent">
        /// A reference to the resulting parent node.  This will be a default value of type
        /// <typeparamref name="T"/> if we fail to get a the parent node.
        /// </param>
        /// <returns>
        /// True if a parent node is found, false, otherwise.
        /// </returns>
        public static bool TryGetParent <T>(this ITreeWalker <T> walker, T node, out T parent)
        {
            // Validate parameters.
            if (walker == null)
            {
                throw new ArgumentNullException("walker");
            }

            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            // Get the ancestors enumerator and attempt to move to the first node.
            // If we get the first node output it and return true.  Otherwise, output a default
            // node and return false.
            using (IEnumerator <T> enumerator = walker.GetAncestors(node).GetEnumerator())
            {
                if (enumerator.MoveNext())
                {
                    parent = enumerator.Current;
                    return(true);
                }
                else
                {
                    parent = default(T);
                    return(false);
                }
            }
        }
        /// <summary>
        /// Returns all nodes at a depth relative to the specified node.
        /// </summary>
        /// <typeparam name="T">The type of elements in the tree.</typeparam>
        /// <param name="walker">
        /// The <see cref="ITreeWalker&lt;T&gt;"/> that knows how to find the parent and child nodes.
        /// </param>
        /// <param name="node">
        /// The node relative to which the level is returned.
        /// </param>
        /// <param name="depth">
        /// The depth of the level to be returned relative to <paramref name="node"/>.  This must
        /// be a nonnegative number.
        /// </param>
        /// <returns>
        /// An <see cref="System.Collections.Generic.IEnumerable&lt;T&gt;"/> that contains all the nodes
        /// nodes at a depth relative to the specified node.  A depth of 0 will return the node
        /// itself, 1 all children of the node, etc...
        /// </returns>
        public static IEnumerable <T> GetLevel <T>(this ITreeWalker <T> walker, T node, int depth)
        {
            // Validate parameters.
            if (walker == null)
            {
                throw new ArgumentNullException("walker");
            }

            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            if (depth < 0)
            {
                throw new ArgumentException("Depth must be nonnegative.", "depth");
            }

            // If 'depth' is zero then return the node.
            // Otherwise, use GetDescendants to return all nodes at 'depth'.
            if (depth == 0)
            {
                return(Enumerable.Repeat(node, 1));
            }
            else
            {
                return
                    (walker
                     .GetDescendants(node, (x, i) => i == depth));
            }
        }
示例#11
0
 public static IVirtualForest <T> New <T>(
     ITreeWalker <T> treeWalker,
     IEqualityComparer <T> comparer,
     params T[] roots)
 {
     return(new VirtualForest <T>(treeWalker, comparer, roots));
 }
        /// <summary>
        /// Gets a node and the node's siblings, i.e. all nodes that share the same parent.
        /// </summary>
        /// <typeparam name="T">The type of elements in the tree.</typeparam>
        /// <param name="walker">
        /// The <see cref="ITreeWalker&lt;T&gt;"/> that knows how to find the parent and child
        /// nodes.
        /// </param>
        /// <param name="node">
        /// The node whose siblings are to be returned.
        /// </param>
        /// <returns>
        /// An <see cref="System.Collections.Generic.IEnumerable&lt;T&gt;"/> that contains the
        /// siblings.
        /// </returns>
        public static IEnumerable <T> GetSiblingsAndSelf <T>(this ITreeWalker <T> walker, T node)
        {
            // Validate parameters.
            if (walker == null)
            {
                throw new ArgumentNullException("walker");
            }

            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            // Get the node's parent.
            T parent;

            if (walker.TryGetParent(node, out parent))
            {
                // Return all of the parent's children with the exception of the original node.
                return
                    (walker
                     .GetChildren(parent));
            }
            else
            {
                // If the node does not have a parent then return the node.
                return(new T[] { node });
            }
        }
示例#13
0
        /// <summary>
        /// Gets the children that match the <paramref name="key"/>.
        /// </summary>
        /// <typeparam name="T">The type of elements in the tree.</typeparam>
        /// <param name="walker">
        /// The <see cref="ITreeWalker&lt;T&gt;"/> that knows how to find the parent and child
        /// nodes.
        /// </param>
        /// <param name="nodes">
        /// The root nodes to be queried.
        /// </param>
        /// <param name="key">
        /// The key that each child will be compared to.
        /// </param>
        /// <param name="comparer">
        /// The <see cref="IEqualityComparer&lt;T&gt;"/> used to compare the key and the child.  If
        /// this is null then the default <see cref="EqualityComparer&lt;T&gt;.Default"/> will be
        /// used.
        /// </param>
        /// <returns>
        /// An <see cref="System.Collections.Generic.IEnumerable&lt;T&gt;"/> that contains all the
        /// matching children.
        /// </returns>
        public static IEnumerable <T> GetChildren <T>(
            this ITreeWalker <T> walker,
            IEnumerable <T> nodes,
            T key,
            IEqualityComparer <T> comparer)
        {
            // Validate parameters.
            if (walker == null)
            {
                throw new ArgumentNullException("walker");
            }

            if (nodes == null)
            {
                throw new ArgumentNullException("nodes");
            }

            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            // If the comparer is null then use the defautl comparer for that type.
            comparer = comparer ?? EqualityComparer <T> .Default;

            return
                (nodes
                 .SelectMany(n => walker.GetChildren(n))
                 .Where(n => comparer.Equals(key, n)));
        }
示例#14
0
        /// <summary>
        /// Gets a node's parent node.
        /// </summary>
        /// <typeparam name="T">The type of elements in the tree.</typeparam>
        /// <param name="walker">
        /// The <see cref="ITreeWalker&lt;T&gt;"/> that knows how to find the parent and child
        /// nodes.
        /// </param>
        /// <param name="node">
        /// The node whose parent is to be returned.
        /// </param>
        /// <returns>
        /// The node's parent.
        /// </returns>
        /// <exception cref="InvalidOperationException">
        /// When the node does not have a parent.
        /// </exception>
        public static T GetParent <T>(this ITreeWalker <T> walker, T node)
        {
            // Validate parameters.
            if (walker == null)
            {
                throw new ArgumentNullException("walker");
            }

            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            // Return the node's parent or throw an exception if the parent does not exist.
            T parent;

            if (walker.TryGetParent(node, out parent))
            {
                return(parent);
            }
            else
            {
                throw new InvalidOperationException("The node does not have a parent.");
            }
        }
        /// <summary>
        /// Gets a node's siblings, i.e. all nodes that share the same parent.
        /// </summary>
        /// <typeparam name="T">The type of elements in the tree.</typeparam>
        /// <param name="walker">
        /// The <see cref="ITreeWalker&lt;T&gt;"/> that knows how to find the parent and child
        /// nodes.
        /// </param>
        /// <param name="node">
        /// The node whose siblings are to be returned.
        /// </param>
        /// <param name="comparer">
        /// An <see cref="System.Collections.Generic.IEqualityComparer&lt;T&gt;"/> that knows how
        /// to compare two nodes for equality.  This is used to make sure that
        /// <paramref name="node"/> is not returned in the resulting
        /// <see cref="System.Collections.Generic.IEnumerable&lt;T&gt;"/>.
        /// </param>
        /// <returns>
        /// An <see cref="System.Collections.Generic.IEnumerable&lt;T&gt;"/> that contains the
        /// siblings.
        /// </returns>
        public static IEnumerable <T> GetSiblings <T>(this ITreeWalker <T> walker, T node, IEqualityComparer <T> comparer)
        {
            // Validate parameters.
            if (walker == null)
            {
                throw new ArgumentNullException("walker");
            }

            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            // If the comparer is null then use the default comparer.
            comparer = comparer ?? EqualityComparer <T> .Default;

            // Get the node's parent.
            T parent;

            if (walker.TryGetParent(node, out parent))
            {
                // Return all of the parent's children with the exception of the original node.
                return
                    (walker
                     .GetChildren(parent)
                     .Where(x => !comparer.Equals(node, x)));
            }
            else
            {
                // If the node does not have a parent then return an empty IEnumerable.
                return(Enumerable.Empty <T>());
            }
        }
示例#16
0
        private static string GetXPathToElement(SHAutomationElement element, ITreeWalker treeWalker, SHAutomationElement rootElement = null)
        {
            var parent = treeWalker.GetParent(element);

            if (parent == null || (rootElement != null && parent.Equals(_callingRoot.Parent)))
            {
                _callingRoot = null;
                return(string.Empty);
            }
            // Get the index
            var allChildren     = parent.FindAllChildren(cf => cf.ByControlType(element.Properties.ControlType));
            var currentItemText = $"{element.Properties.ControlType.Value}";

            if (allChildren.Length > 1)
            {
                // There is more than one matching child, find out the index
                var indexInParent = 1; // Index starts with 1
                foreach (var child in allChildren)
                {
                    if (child.Equals(element))
                    {
                        break;
                    }
                    indexInParent++;
                }
                currentItemText += $"[{indexInParent}]";
            }
            return($"{GetXPathToElement(parent, treeWalker, rootElement)}/{currentItemText}");
        }
示例#17
0
        public void Initialize(AutomationType selectedAutomationType)
        {
            SelectedAutomationType = selectedAutomationType;
            IsInitialized          = true;

            if (selectedAutomationType == AutomationType.UIA2)
            {
                _automation = new UIA2Automation();
            }
            else
            {
                _automation = new UIA3Automation();
            }
            _rootElement = _automation.GetDesktop();
            var desktopViewModel = new ElementViewModel(_rootElement);

            desktopViewModel.SelectionChanged += DesktopViewModel_SelectionChanged;
            desktopViewModel.LoadChildren(false);
            Elements.Add(desktopViewModel);

            // Initialize TreeWalker
            _treeWalker = _automation.TreeWalkerFactory.GetControlViewWalker();

            // Initialize hover
            _hoverMode = new HoverMode(_automation);
            _hoverMode.ElementHovered += ElementHovered;
        }
示例#18
0
        public void Initialize(AutomationType selectedAutomationType)
        {
            SelectedAutomationType = selectedAutomationType;
            IsInitialized          = true;

            _automation  = selectedAutomationType == AutomationType.UIA2 ? (AutomationBase) new UIA2Automation() : new UIA3Automation();
            _rootElement = _automation.GetDesktop();
            var desktopViewModel = new ElementViewModel(_rootElement);

            desktopViewModel.SelectionChanged += DesktopViewModel_SelectionChanged;
            desktopViewModel.LoadChildren(false);
            Elements.Add(desktopViewModel);
            Elements[0].IsExpanded = true;

            // Initialize TreeWalker
            _treeWalker = _automation.TreeWalkerFactory.GetControlViewWalker();

            // Initialize hover
            _hoverMode = new HoverMode(_automation);
            _hoverMode.ElementHovered += ElementToSelectChanged;

            // Initialize focus tracking
            _focusTrackingMode = new FocusTrackingMode(_automation);
            _focusTrackingMode.ElementFocused += ElementToSelectChanged;
        }
示例#19
0
        public ElementInfo GetElementParent(ElementInfo ElementInfo)
        {
            UIAElementInfo EI = (UIAElementInfo)ElementInfo;


            if (EI.ElementObject.Equals(CurrentWindow))
            {
                return(null);
            }

            ITreeWalker walker = ((AutomationElement)EI.ElementObject).Automation.TreeWalkerFactory.GetControlViewWalker();

            AutomationElement ParentAE = walker.GetParent((AutomationElement)EI.ElementObject);

            if (ParentAE.Equals(CurrentWindow))
            {
                return(null);                                // CurrentWindowRootElement;  // Since there are check done on root element we must return the same when found
            }
            if (ParentAE == null)
            {
                return(null);
            }

            UIAElementInfo RC = new UIAElementInfo()
            {
                ElementObject = ParentAE
            };                                                                        // return minimial EI

            return(RC);
        }
示例#20
0
 public static VirtualTree <T> New <T>(
     ITreeWalker <T> treeWalker,
     T root,
     IEqualityComparer <T> comparer)
 {
     return(new VirtualTree <T>(treeWalker, root, comparer));
 }
示例#21
0
        public WindowsTreeElement(treeelement parent, bool expanded, AutomationBase automation, AutomationElement element, ITreeWalker treeWalker) : base(parent)
        {
            Element         = new UIElement(element);
            _treeWalker     = treeWalker;
            this.automation = automation;
            RawElement      = element;
            IsExpanded      = expanded;

            string controltype  = "";
            string name         = "unknown";
            string automationid = "";

            ControlType = ControlType.Window;
            if (element.Properties.ControlType.IsSupported)
            {
                ControlType = element.Properties.ControlType.Value;
            }
            if (element.Properties.ControlType.IsSupported)
            {
                controltype = element.Properties.ControlType.Value.ToString();
            }
            if (element.Properties.Name.IsSupported)
            {
                name = element.Properties.Name.Value;
            }
            if (element.Properties.AutomationId.IsSupported)
            {
                automationid = element.Properties.AutomationId.Value;
            }
            Name = (controltype + " " + name + " " + automationid).Trim();
        }
        /// <summary>
        /// Gets a node's leaves, i.e. all descendants of that node that do not have children.  If
        /// the node has no children then the node itself is returned.
        /// </summary>
        /// <typeparam name="T">The type of elements in the tree.</typeparam>
        /// <param name="walker">
        /// The <see cref="ITreeWalker&lt;T&gt;"/> that knows how to find the parent and child nodes.
        /// </param>
        /// <param name="node">
        /// The node whose leaves are to be returned.
        /// </param>
        /// <returns>
        /// An <see cref="System.Collections.Generic.IEnumerable&lt;T&gt;"/> that contains all the node's
        /// leaves.
        /// </returns>
        public static IEnumerable <T> GetLeaves <T>(this ITreeWalker <T> walker, T node)
        {
            // Validate parameters.
            if (walker == null)
            {
                throw new ArgumentNullException("walker");
            }

            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            // Create a stack to hold enumerators and push the child enumerator of 'node'.
            Stack <IEnumerator <T> > stack = new Stack <IEnumerator <T> >();

            stack.Push(walker.GetChildren(node).GetEnumerator());

            if (!stack.Peek().MoveNext())
            {
                // If the enumerator on the stack has no items then 'node' is a leaf node.
                // Dispose of the enumerator and yield 'node' and we are done.
                stack.Pop().Dispose();
                yield return(node);
            }
            else
            {
                // The current node is now the first child of the 'node' parameter.
                // Continue looping as long as the stack has enumerators on it.
                while (stack.Count > 0)
                {
                    // Push the current node's enumerator on the stack.
                    stack.Push(walker.GetChildren(stack.Peek().Current).GetEnumerator());

                    // Continue pushing enumerators on the stack as long the enumerator on the top
                    // of the stack has items.
                    while (stack.Peek().MoveNext())
                    {
                        stack.Push(walker.GetChildren(stack.Peek().Current).GetEnumerator());
                    }

                    // Once we reach an enumerator that has no items pop that enumerator and
                    // dispose of it.
                    stack.Pop().Dispose();

                    // The 'Current' property of the enumerator on the top of the stack is a leaf
                    // node.  Return it.
                    yield return(stack.Peek().Current);

                    // Continue popping enumerators off of the stack and disposing of them until
                    // we reach an enumerator with another item.  That item become the current
                    // node.
                    while (stack.Count > 0 && !stack.Peek().MoveNext())
                    {
                        stack.Pop().Dispose();
                    }
                }
            }
        }
 public ScanTreeWalker(
     ITreeWalker <TBaseNode> walker,
     Func <TBaseNode, TValue, TValue> accumulator,
     Func <TBaseNode, TValue, TValue> deccumulator)
 {
     this._Walker       = walker;
     this._Accumulator  = accumulator;
     this._Deccumulator = deccumulator;
 }
 public PruneTreeWalker(
     ITreeWalker <T> walker,
     Func <T, bool> predicate,
     PruneOption pruneOption)
 {
     this._Walker      = walker;
     this._Predicate   = predicate;
     this._PruneOption = pruneOption;
 }
示例#25
0
 internal static IEnumerable <VirtualTree <T> > ToVirtualTrees <T>(
     this IEnumerable <T> source,
     ITreeWalker <T> walker)
 {
     foreach (T root in source)
     {
         yield return(new VirtualTree <T>(walker, root));
     }
 }
示例#26
0
        /// <summary>
        /// Enumerates a tree using the pre-order traversal method.
        /// </summary>
        /// <typeparam name="T">The type of elements in the tree.</typeparam>
        /// <param name="walker">
        /// The <see cref="ITreeWalker&lt;T&gt;"/> that knows how to find the parent and child nodes.
        /// </param>
        /// <param name="node">The root node of the tree that is to be traversed.</param>
        /// <param name="excludeSubtreePredicate">
        /// A <see cref="System.Func&lt;T, Int32, Boolean&gt;"/> that determines if the current node
        /// that is being evaluated (and all of its descendants) should be included in the
        /// traversal.  This allows for short-circuiting of the pre-order traversal by excluding
        /// particular subtrees from the traversal.  The first argument is the current node being
        /// evaluated and the second argument is the depth of the current node relative to the
        /// original node that the traversal began on.
        /// </param>
        /// <param name="excludeOption">
        /// Used in conjunction with the <paramref name="excludeSubtreePredicate"/>.  Determines
        /// if the entire subtree should be excluded or just its children.
        /// </param>
        /// <returns>
        /// An <see cref="System.Collections.Generic.IEnumerable&lt;T&gt;"/> that contains all the
        /// nodes in the tree ordered based on a pre-order traversal.
        /// </returns>
        public static IEnumerable <T> PreOrderTraversal <T>(
            this ITreeWalker <T> walker,
            T node,
            Func <T, int, bool> excludeSubtreePredicate,
            ExcludeOption excludeOption)
        {
            // Validate parameters.
            if (walker == null)
            {
                throw new ArgumentNullException("walker");
            }

            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            // Create a stack to keep track of the branches being traversed.
            Stack <IEnumerator <T> > enumerators = new Stack <IEnumerator <T> >();

            enumerators.Push(Enumerable.Repeat(node, 1).GetEnumerator());

            // Loop as long as we have an enumerator on the stack.  When we have popped the last
            // one off the stack the traversal is complete.
            while (enumerators.Count > 0)
            {
                if (enumerators.Peek().MoveNext())
                {
                    node = enumerators.Peek().Current;

                    // If the 'excludeSubtreePredicate' is not null and evaluates to true then
                    // yield the current node if 'excludeOption' is set to exclude the children.
                    // Otherwise, do not yield anything.
                    if (excludeSubtreePredicate != null &&
                        excludeSubtreePredicate.Invoke(node, enumerators.Count - 1))
                    {
                        if (excludeOption == ExcludeOption.ExcludeDescendants)
                        {
                            yield return(node);
                        }
                    }
                    else
                    {
                        // Yield the current node then push it and its children onto the
                        // stack.
                        yield return(node);

                        enumerators.Push(walker.GetChildren(node).GetEnumerator());
                    }
                }
                else
                {
                    // Pop the enumerator and dispose of it.
                    enumerators.Pop().Dispose();
                }
            }
        }
示例#27
0
        // Use this after attempting to find the excerpt element because it destroys the HTML document
        private string GetSeparatorExcerpt(IHtmlDocument htmlDocument)
        {
            if (_separators?.Length > 0)
            {
                ITreeWalker walker  = htmlDocument.CreateTreeWalker(htmlDocument.DocumentElement, FilterSettings.Comment);
                IComment    comment = (IComment)walker.ToFirst();
                while (comment != null && !_separators.Contains(comment.NodeValue.Trim(), StringComparer.OrdinalIgnoreCase))
                {
                    comment = (IComment)walker.ToNext();
                }

                // Found the first separator
                if (comment != null)
                {
                    // Get a clone of the parent element
                    IElement parent = comment.ParentElement;
                    if (parent.TagName.Equals("p", StringComparison.OrdinalIgnoreCase))
                    {
                        // If we were in a tag inside a paragraph, ascend to the paragraph's parent
                        parent = parent.ParentElement;
                    }

                    // Now remove everything after the separator
                    walker = htmlDocument.CreateTreeWalker(parent);
                    bool          remove      = false;
                    Stack <INode> removeStack = new Stack <INode>();
                    INode         node        = walker.ToFirst();
                    while (node != null)
                    {
                        if (node == comment)
                        {
                            remove = true;
                        }

                        // Also remove if it's a top-level element that doesn't match the query selector
                        if (remove ||
                            (node.Parent == parent &&
                             node is IElement &&
                             !string.IsNullOrEmpty(_querySelector) &&
                             !((IElement)node).Matches(_querySelector)))
                        {
                            removeStack.Push(node);
                        }
                        node = walker.ToNext();
                    }
                    while (removeStack.Count > 0)
                    {
                        node = removeStack.Pop();
                        node.Parent.RemoveChild(node);
                    }

                    return(parent.InnerHtml);
                }
            }
            return(null);
        }
示例#28
0
 internal static IVirtualForest <T> ShallowCopy <T>(
     this IVirtualForest <T> source,
     ITreeWalker <T> treeWalker,
     Func <IEnumerable <T>, IEnumerable <T> > rootsSelector)
 {
     return(new VirtualForest <T>(
                treeWalker,
                source.Comparer,
                rootsSelector(source.Roots)));
 }
示例#29
0
        private void Init()
        {
            _automation = new UIA3Automation();
            // Initialize TreeWalker
            _treeWalker  = _automation.TreeWalkerFactory.GetControlViewWalker();
            _rootElement = _automation.GetDesktop();

            _elementFinder = new AutomationHelper(_automation);
            _elementFinder.ControlReleasedOverElement += ControlReleasedOverElement;
        }
示例#30
0
        private void DisposeAutomationObjects()
        {
            _elementFinder.ControlReleasedOverElement += ControlReleasedOverElement;
            _elementFinder.ControlReleasedOverElement -= ControlReleasedOverElement;

            _rootElement   = null;
            _treeWalker    = null;
            _automation    = null;
            _elementFinder = null;
        }
示例#31
0
 public virtual void WalkTree(ITreeWalker treeWalker)
 {
     treeWalker.OnInner(this);
     foreach (var branch in Branches.Where(branch => branch != null))
     {
         if (branch.IsLeaf)
         {
             treeWalker.OnLeaf(branch.AsLeaf());
         }
         else if (branch.IsInner)
         {
             branch.AsInner().WalkTree(treeWalker);
         }
     }
 }
示例#32
0
 public ITreeWalker WalkTree( ITreeWalker walker )
 {
     walker.WalkTree( this );
     return walker;
 }
示例#33
0
 public ITreeWalker WalkTree( ITreeWalker walker, TreeWalkerMode mode )
 {
     walker.WalkTree( this, mode );
     return walker;
 }