public XslTransformWithProgress(string inputFilePath, string outputFilePath, Stream xsltStream, string xpathToCountSteps) { _inputFilePath = inputFilePath; _outputFilePath = outputFilePath; _xsltStream = xsltStream; _xpathToCountSteps = xpathToCountSteps; _progressState = null; }
private void WaitForFinish(ConsoleProgressState progress, ProgressState.StateValue expectedResult) { DateTime giveUpTime = DateTime.Now.AddSeconds(5); while (progress.State != expectedResult) { if (DateTime.Now > giveUpTime) { Assert.Fail("Didn't get expected result"); } Thread.Sleep(10); } }
public bool BeginInvoke(ProgressState progress) { if( !enabled ) { return false; } // Disable the operation while it is started, notifying everyone // of that fact Enabled = false; _canceling = false; BeginInvokeCore2(progress); return true; }
public void Setup() { _dummyParentForm = new Form(); _dummyParentForm.Name = "dummy form"; _dummyParentForm.Text = "Dummy Form"; _dummyParentForm.Show(); Application.DoEvents(); _command = new TestCommand(); //_progressLog = ""; _progressHandler = new ProgressDialogHandler(_dummyParentForm, _command); _progressState = new ProgressDialogProgressState(_progressHandler); _commandFinishedCalled = false; }
public bool BeginInvoke(ProgressState progress) { if (!enabled) { return(false); } // Disable the operation while it is started, notifying everyone // of that fact Enabled = false; _canceling = false; BeginInvokeCore2(progress); return(true); }
public void PopulateDefinitions(ProgressState state) { state.StatusLabel = "Updating Lift File..."; try { string pathToLift = _liftFilePath; string temp1 = Utilities.ProcessLiftForLaterMerging(pathToLift); // int liftProducerVersion = GetLiftProducerVersion(pathToLift); string outputPath = Path.Combine(Path.GetTempPath(), Path.GetTempFileName()); XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.NewLineOnAttributes = true; using ( Stream xsltStream = Assembly.GetExecutingAssembly().GetManifestResourceStream( "WeSay.LexicalModel.Migration.populateDefinitionFromGloss.xslt") ) { XslTransformWithProgress transformer = new XslTransformWithProgress( temp1, outputPath, xsltStream, "//sense"); state.StatusLabel = "Populating Definitions from Glosses"; transformer.Transform(state); } MoveTempOverRealAndBackup(pathToLift, outputPath); } catch (Exception error) { state.ExceptionThatWasEncountered = error; state.State = ProgressState.StateValue.StoppedWithError; throw; // this will put the exception in the e.Error arg of the RunWorkerCompletedEventArgs } }
protected abstract void DoWork2(ProgressState progress);
protected override void BeginInvokeCore2(ProgressState progress) { WorkInvoker2 worker = new WorkInvoker2(DoWork2); worker.BeginInvoke(progress, new AsyncCallback(EndWork2), null); }
private static void progress_Log(object sender, ProgressState.LogEvent e) { Console.Error.WriteLine(e.message); }
protected abstract void BeginInvokeCore2(ProgressState progress);
public void Transform(ProgressState progressState) { _progressState = progressState; XslCompiledTransform transform = null; using (Stream outputStream = File.Create(OutputFilePath)) { XmlDocument inputDocument = new XmlDocument(); inputDocument.PreserveWhitespace = true; inputDocument.Load(_inputFilePath); try { 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 = _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 = "Processing..."; int entriesCount = 1; XmlNodeList nodeCountList = inputDocument.SelectNodes(_xpathToCountSteps); if (nodeCountList != null) { entriesCount = nodeCountList.Count; } _progressState.TotalNumberOfSteps = entriesCount; if (_xsltArguments == null) { _xsltArguments = new XsltArgumentList(); } _xsltArguments.XsltMessageEncountered += OnXsltMessageEncountered; transform.Transform(inputDocument, _xsltArguments, outputStream); 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; _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(); } } } } }
/// <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 = 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 = new XmlWriterSettings(); writerSettings.Encoding = new UTF8Encoding(false); using ( XmlWriter 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(); } } } }
protected override void DoWork2(ProgressState progress) { int countForWork = 0; progress.TotalNumberOfSteps = 100; while (countForWork < 100) { DoPretendWork(); countForWork++; progress.NumberOfStepsCompleted = countForWork; } progress.State = ProgressState.StateValue.Finished; }
private static void WaitOnProgressState(ref ProgressState _progressState, ProgressState.StateValue expectedState) { DateTime giveUpTime = DateTime.Now.AddSeconds(5); while (_progressState.State != expectedState) { if (DateTime.Now > giveUpTime) { Assert.AreEqual(expectedState, _progressState.State); } // Thread.Sleep(10); Application.DoEvents(); } }
private void OnProgressStateLog(object sender, ProgressState.LogEvent e) { _logBuilder.AppendLine(e.message); }
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); string nameForOldFile = _liftFilePath.Replace(".lift", "." + oldVersion + ".lift"); 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; } }