Пример #1
0
        private void OnDoSomeWork(object sender, DoWorkEventArgs e)
        {
            ProgressState state = (ProgressState)e.Argument;
            WorkArguments args  = state.Arguments as WorkArguments;

            if (args == null)
            {
                args = new WorkArguments();               //use defaults
            }
            state.TotalNumberOfSteps = 10000;             //we actually have no idea, but let it be large
            try
            {
                _argumentReceivedFromProgressState = args.dummy;
                if (args.secondsToUseUp > 0)
                {
                    DateTime end = DateTime.Now.AddSeconds(args.secondsToUseUp);
                    while (DateTime.Now < end)
                    {
                        state.WriteToLog(_countForWork.ToString());
                        Thread.Sleep(10);
                        if (args.doMakeProgressCalls)
                        {
                            state.NumberOfStepsCompleted++;
                        }
                    }
                }
                else                  //seeing how long this takes
                {
                    state.TotalNumberOfSteps = args.iterationsToDo;
                    double a = 0;
                    for (int i = 0; i < state.TotalNumberOfSteps; i++)
                    {
                        //adding this makes freeze not happen Thread.Sleep(1);
                        a += Math.Sqrt(987987 + i);
                        if (args.doMakeProgressCalls)
                        {
                            state.NumberOfStepsCompleted++;
                        }
                    }
                }
                if (args.doClaimExtraSteps)
                {
                    state.NumberOfStepsCompleted = state.TotalNumberOfSteps + 5;
                }
                e.Result = "all done";
            }
            catch (Exception err)
            {
                //currently, error reporter can choke because this is
                //being called from a non sta thread.
                //so let's leave it to the progress dialog to report the error
                //                Reporting.ErrorReporter.ReportException(e,null, false);
                state.ExceptionThatWasEncountered = err;
                state.WriteToLog(err.Message);
                state.State = ProgressState.StateValue.StoppedWithError;
            }
        }
Пример #2
0
        private void OnDoBackgroundWorkerWork(object sender, DoWorkEventArgs e)
        {
            ProgressState state = (ProgressState)e.Argument;

            state.StatusLabel = "working hard...";
            while (_countForWork < 100)
            {
                DoPretendWork();
                state.WriteToLog(_countForWork.ToString());
                _countForWork++;
                state.NumberOfStepsCompleted = _countForWork;
            }
            e.Result = ((ProgressState)e.Argument).State = ProgressState.StateValue.Finished;
        }
Пример #3
0
        public void MigrateLiftFile(ProgressState state)
        {
            try
            {
                string oldVersion = Validator.GetLiftVersion(_liftFilePath);
                string status     = String.Format("Migrating from {0} to {1}",
                                                  oldVersion,
                                                  Validator.LiftVersion);
                Logger.WriteEvent(status);
                state.StatusLabel = status;
                string migratedFile = Migrator.MigrateToLatestVersion(_liftFilePath);
                //this was hard on test, as it would fail if the file ended in ".tmp"
                //      string nameForOldFile = _liftFilePath.Replace(".lift", "." + oldVersion + ".lift");
                var    extension      = Path.GetExtension(_liftFilePath);
                string nameForOldFile = _liftFilePath.Replace(extension, "." + oldVersion + extension);

                if (File.Exists(nameForOldFile))
                // like, if we tried to convert it before and for some reason want to do it again
                {
                    File.Delete(nameForOldFile);
                }
                File.Move(_liftFilePath, nameForOldFile);
                File.Move(migratedFile, _liftFilePath);

                //review: CJP asks I'm not sure why this is required to be passed back via results. ???
                //args.Result = args.Argument as ProgressState;
            }
            catch (Exception e)
            {
                //currently, error reporter can choke because this is
                //being called from a non sta thread.
                //so let's leave it to the progress dialog to report the error
                //                Reporting.ErrorReporter.ReportException(e,null, false);
                state.ExceptionThatWasEncountered = e;
                state.WriteToLog(e.Message);
                state.State = ProgressState.StateValue.StoppedWithError;
            }
        }
Пример #4
0
        /// <summary>
        /// this runs in a worker thread
        /// </summary>
        private static void OnDoTransformWork(object sender, DoWorkEventArgs args)
        {
            ProgressState        progressState = (ProgressState)args.Argument;
            XslCompiledTransform transform     = null;

            try
            {
                TransformWorkerArguments workerArguments =
                    (TransformWorkerArguments)progressState.Arguments;

                transform = new XslCompiledTransform();

                //all this just to allow a DTD statement in the source xslt
                XmlReaderSettings readerSettings = new XmlReaderSettings();
                readerSettings.ProhibitDtd = false;

                progressState.StatusLabel = "Preparing...";
                using (Stream stream = workerArguments.xsltStream)
                {
                    using (XmlReader xsltReader = XmlReader.Create(stream, readerSettings))
                    {
                        XsltSettings settings = new XsltSettings(true, true);
                        transform.Load(xsltReader, settings, new XmlUrlResolver());
                        xsltReader.Close();
                    }
                    stream.Close();
                }

                progressState.StatusLabel = "Transforming...";
                int entriesCount = workerArguments.inputDocument.SelectNodes("//entry").Count;
                progressState.TotalNumberOfSteps = 2 * (entriesCount) + workerArguments.postTransformSteps;
                _staticProgressStateForWorker    = progressState;
                workerArguments.xsltArguments.XsltMessageEncountered += OnXsltMessageEncountered;

                if (!workerArguments.outputToXml)
                {
                    transform.Transform(workerArguments.inputDocument,
                                        workerArguments.xsltArguments,
                                        workerArguments.outputStream);
                }
                else
                {
                    //all this is to stop sticking on the BOM, which trips up princeXML
                    XmlWriterSettings writerSettings = CanonicalXmlSettings.CreateXmlWriterSettings();
                    writerSettings.Encoding = new UTF8Encoding(false);

                    using (var writer = XmlWriter.Create(workerArguments.outputStream, writerSettings))
                    {
                        transform.Transform(workerArguments.inputDocument,
                                            workerArguments.xsltArguments,
                                            writer);
                    }
                }

                workerArguments.outputStream.Close();                 //let the next guy get at the file
                Debug.Assert(progressState.NumberOfStepsCompleted <= entriesCount,
                             "Should use up more than we reserved for ourselves");
                progressState.NumberOfStepsCompleted = entriesCount;
                if (workerArguments.postTransformMethod != null)
                {
                    workerArguments.postTransformMethod.Invoke(sender, args);
                }
                progressState.State = ProgressState.StateValue.Finished;
            }
            catch (CancelingException)             // not an error
            {
                progressState.State = ProgressState.StateValue.Finished;
            }
            catch (Exception err)
            {
                //currently, error reporter can choke because this is
                //being called from a non sta thread.
                //so let's leave it to the progress dialog to report the error
                //                Reporting.ErrorReporter.ReportException(args,null, false);
                progressState.ExceptionThatWasEncountered = err;
                progressState.WriteToLog(err.Message);
                progressState.State = ProgressState.StateValue.StoppedWithError;
            }
            finally
            {
                if (transform != null)
                {
                    progressState.StatusLabel = "Cleaning up...";
                    TempFileCollection tempfiles = transform.TemporaryFiles;
                    if (tempfiles != null)                     // tempfiles will be null when debugging is not enabled
                    {
                        tempfiles.Delete();
                    }
                }
            }
        }
Пример #5
0
        /// <summary>
        /// this runs in a worker thread
        /// </summary>
        private static void OnDoTransformWork(object sender, DoWorkEventArgs args)
        {
            ProgressState progressState = (ProgressState)args.Argument;

            if (progressState == null)
            {
                return;
            }
            TransformWorkerArguments arguments =
                (TransformWorkerArguments)progressState.Arguments;

            //ProjectInfo projectInfo = arguments.projectInfo;

            if (File.Exists(arguments.odtPath))
            {
                bool fileDeleted = false;
                while (!fileDeleted)
                {
                    try
                    {
                        File.Delete(arguments.odtPath);
                        fileDeleted = !File.Exists(arguments.odtPath);
                    }
                    catch (IOException)
                    {
                        var dialogResult =
                            MessageBox.Show(
                                StringCatalog.Get("WeSay was unable to remove the old .odt file. This may be because it is open in another program. Please close the file before clicking OK."),
                                StringCatalog.Get("Unable to delete old file."), MessageBoxButtons.OKCancel);
                        if (dialogResult == DialogResult.Cancel)
                        {
                            return;
                        }
                    }
                }
            }

            string liftPath = Path.Combine(arguments.exportDir,
                                           arguments.name + ".lift");

            progressState.StatusLabel = "Sorting Lift";
            SortLift(liftPath, arguments.lexEntryRepository, arguments.viewTemplate);
            //var maker = new PLiftMaker();
            //maker.MakePLiftTempFile(pliftPath, arguments.lexEntryRepository, arguments.viewTemplate);
            String odfTemplate   = arguments.odfTemplate;
            String contentPath   = Path.Combine(arguments.exportDir, "content.xml");
            String stylesPath    = Path.Combine(arguments.exportDir, "styles.xml");
            Stream stylesOutput  = new FileStream(stylesPath, FileMode.Create);
            Stream contentOutput = new FileStream(contentPath, FileMode.Create);

            progressState.StatusLabel = "Preparing ODT content";
            XslCompiledTransform transform      = null;
            XmlReaderSettings    readerSettings = new XmlReaderSettings();

            try
            {
                transform = new XslCompiledTransform();

                System.IO.StringReader sr =
                    new System.IO.StringReader(Resources.lift2odfContent);

                XmlReader    xsltReader = XmlReader.Create(sr, readerSettings);
                XsltSettings settings   = new XsltSettings(true, true);
                transform.Load(xsltReader, settings, new XmlUrlResolver());
                xsltReader.Close();

                XsltArgumentList xsltArgs = new XsltArgumentList();
                xsltArgs.AddParam("title", "", arguments.name);
                // TODO what is the correct url base path for illustrations?
                // It seems one level up just gets out of the zip file, so use 2 levels here
                xsltArgs.AddParam("urlBase", "", "../../");

                transform.Transform(liftPath, xsltArgs, contentOutput);
                contentOutput.Close();

                progressState.StatusLabel = "Preparing ODT styles";

                transform = new XslCompiledTransform();

                StringReader srStyles         = new StringReader(Resources.ldml2odfStyles);
                XmlReader    xsltReaderStyles = XmlReader.Create(srStyles, readerSettings);
                XsltSettings stylesSettings   = new XsltSettings(true, true);
                transform.Load(xsltReaderStyles, stylesSettings, new XmlUrlResolver());
                xsltReaderStyles.Close();

                xsltArgs = new XsltArgumentList();
                xsltArgs.AddParam("primaryLangCode", "", GetHeadwordWritingSystemId(arguments.viewTemplate));

                string pathToTempFile = CreateSingleWritingsystemsFileForEasyXslProcessing();
                transform.Transform(pathToTempFile, xsltArgs, stylesOutput);
                File.Delete(pathToTempFile);

                stylesOutput.Close();
                progressState.StatusLabel = "Creating ODT file";
                ZipFile zipFile = ZipFile.Create(arguments.odtPath);
                zipFile.BeginUpdate();
                zipFile.Add(stylesPath, "styles.xml");
                zipFile.Add(contentPath, "content.xml");

                // add files from template
                if (Directory.Exists(odfTemplate))
                {
                    addDirectoryFilesToZip(zipFile, odfTemplate, "");
                }
                else
                {
                    progressState.WriteToLog("Directory not found: " + odfTemplate);
                }
                zipFile.CommitUpdate();
                zipFile.Close();
                progressState.StatusLabel = "Openning in OpenOffice";
            }
            catch (Exception e)
            {
                progressState.ExceptionThatWasEncountered = e;
                progressState.WriteToLog(e.Message);
            }
        }