Пример #1
0
        /*-----------------------------------------------------------------------------------------*/
        /* Background worker                                                                       */
        /*-----------------------------------------------------------------------------------------*/
        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            OxmlDocument doc = new OxmlDocument(this.GetOXML());
            Scope scope = new Scope(Properties.Settings.Default.ScopeSize);
            scope.MinimumTokenLength = Properties.Settings.Default.MinimumTokenLength;

            doc.PurgeShading();
            doc.PurgeBookmarks();

            // Load ignore list

            if (!String.IsNullOrEmpty(_ignoreListName))
            {
                IgnoreList IgnoreList = new IgnoreList(GetListPath(_ignoreListName));
                scope.IgnoreWords(IgnoreList.LoadWords(_language));
            }

            // Analyse document

            int blockCount = 0;
            float numberOfBlocks = (float)doc.GetNumberOfBlocks();
            var block = doc.GetNextOxmlBlock();

            while (block != null)
            {
                var tokens = block.GenerateOxmlTokens();
                foreach (var token in tokens)
                {
                    scope.FeedToken(token);
                }
                block = doc.GetNextOxmlBlock();

                if (worker.CancellationPending == true)
                {
                    e.Cancel = true;
                    break;
                }

                int progress = (int)(++blockCount / numberOfBlocks * 100);
                worker.ReportProgress(progress);
            }

            // Add bookmarks to document

            var conflicts = scope.GetConflicts();
            foreach (var root in conflicts.Keys)
            {
                foreach (var conflict in conflicts[root])
                {
                    foreach (var token in conflict)
                    {
                        Debug.WriteLine(String.Format("Adding bookmark {0} for root '{1}' with text '{2}' and range {3}",
                            token.BookmarkName, token.Root, token.Text, token.Range.ToString()));
                        doc.BookmarkRange(token.Range, token.BookmarkName);
                    }
                }
            }

            // Update OXML only if it is valid

            string oxml = doc.ToOxml();

            if (oxml != null)
            {
                this._conflicts = conflicts;
                this.SetOXML(doc.ToOxml());
            }
            else
            {
                MessageBox.Show("An error occured while processing this document.", "Eagle Add-In");
            }
        }