Beispiel #1
0
        IEnumerable <FarFile> DoInvokeXPath(ProgressBox progress)
        {
            // object context
            var objectContext = new XPathObjectContext()
            {
                Filter = this.Filter,
                IncrementDirectoryCount = delegate(int count)
                {
                    ProcessedDirectoryCount += count;
                    if (progress == null)
                    {
                        return;
                    }

                    var directoryPerSecond = ProcessedDirectoryCount / progress.ElapsedFromStart.TotalSeconds;
                    progress.Activity = string.Format(null, Res.SearchActivityDeep,
                                                      FoundFileCount, ProcessedDirectoryCount, directoryPerSecond);
                    progress.ShowProgress();
                },
                Stopping = delegate
                {
                    return(Stopping || progress != null && UIUserStop());
                }
            };

            var xsltContext = new XPathXsltContext(objectContext.NameTable);

            if (_XVariables != null)
            {
                foreach (var kv in _XVariables)
                {
                    xsltContext.AddVariable(kv.Key, kv.Value);
                }
            }

            // XPath text
            string xpath;

            if (string.IsNullOrEmpty(XFile))
            {
                xpath = XPath;
            }
            else
            {
                var input = XPathInput.ParseFile(XFile);
                xpath = input.Expression;
                foreach (var kv in input.Variables)
                {
                    xsltContext.AddVariable(kv.Key, kv.Value);
                }
            }

            var expression = XPathExpression.Compile(xpath);

            if (expression.ReturnType != XPathResultType.NodeSet)
            {
                throw new InvalidOperationException("Invalid expression return type.");
            }
            expression.SetContext(xsltContext);

            ++ProcessedDirectoryCount;
            var args = new GetFilesEventArgs(ExplorerModes.Find);

            foreach (var file in _RootExplorer.GetFiles(args))
            {
                // stop?
                if (Stopping || progress != null && UIUserStop())                 //???? progress to navigator
                {
                    break;
                }

                // filter out a leaf
                if (Filter != null && !file.IsDirectory && !Filter(_RootExplorer, file))
                {
                    continue;
                }

                var xfile     = new SuperFile(_RootExplorer, file);
                var navigator = new XPathObjectNavigator(xfile, objectContext);
                var iterator  = navigator.Select(expression);
                while (iterator.MoveNext())
                {
                    // stop?
                    if (Stopping || progress != null && UIUserStop())                     //???? progress to navigator
                    {
                        break;
                    }

                    // found file or directory, ignore anything else
                    if (!(iterator.Current.UnderlyingObject is SuperFile currentFile))
                    {
                        continue;
                    }

                    // filter out directory, it is already done for files
                    if (Filter != null && currentFile.IsDirectory && (!Directory || !Filter(currentFile.Explorer, currentFile.File)))
                    {
                        continue;
                    }

                    // add
                    yield return(currentFile);

                    ++FoundFileCount;
                }
            }
        }
Beispiel #2
0
        IEnumerable<FarFile> DoInvokeXPath(ProgressBox progress)
        {
            // object context
            var objectContext = new XPathObjectContext()
            {
                Filter = this.Filter,
                IncrementDirectoryCount = delegate(int count)
                {
                    ProcessedDirectoryCount += count;
                    if (progress == null)
                        return;

                    var directoryPerSecond = ProcessedDirectoryCount / progress.ElapsedFromStart.TotalSeconds;
                    progress.Activity = string.Format(null, Res.SearchActivityDeep,
                        FoundFileCount, ProcessedDirectoryCount, directoryPerSecond);
                    progress.ShowProgress();
                },
                Stopping = delegate
                {
                    return Stopping || progress != null && UIUserStop();
                }
            };

            var xsltContext = new XPathXsltContext(objectContext.NameTable);
            if (_XVariables != null)
            {
                foreach (var kv in _XVariables)
                    xsltContext.AddVariable(kv.Key, kv.Value);
            }

            // XPath text
            string xpath;
            if (string.IsNullOrEmpty(XFile))
            {
                xpath = XPath;
            }
            else
            {
                var input = XPathInput.ParseFile(XFile);
                xpath = input.Expression;
                foreach (var kv in input.Variables)
                    xsltContext.AddVariable(kv.Key, kv.Value);
            }

            var expression = XPathExpression.Compile(xpath);
            if (expression.ReturnType != XPathResultType.NodeSet)
                throw new InvalidOperationException("Invalid expression return type.");
            expression.SetContext(xsltContext);

            ++ProcessedDirectoryCount;
            var args = new GetFilesEventArgs(ExplorerModes.Find);
            foreach (var file in _RootExplorer.GetFiles(args))
            {
                // stop?
                if (Stopping || progress != null && UIUserStop()) //???? progress to navigator
                    break;

                // filter out a leaf
                if (Filter != null && !file.IsDirectory && !Filter(_RootExplorer, file))
                    continue;

                var xfile = new SuperFile(_RootExplorer, file);
                var navigator = new XPathObjectNavigator(xfile, objectContext);
                var iterator = navigator.Select(expression);
                while (iterator.MoveNext())
                {
                    // stop?
                    if (Stopping || progress != null && UIUserStop()) //???? progress to navigator
                        break;

                    // found file or directory, ignore anything else
                    var currentFile = iterator.Current.UnderlyingObject as SuperFile;
                    if (currentFile == null)
                        continue;

                    // filter out directory, it is already done for files
                    if (Filter != null && currentFile.IsDirectory && (!Directory || !Filter(currentFile.Explorer, currentFile.File)))
                        continue;

                    // add
                    yield return currentFile;
                    ++FoundFileCount;
                }
            }
        }