Exemplo n.º 1
0
        private async Task GenerateXml()
        {
            using (new CursorWait(this))
            {
                await cmdOK.DoThreadSafeAsync(() => cmdOK.Enabled = false);

                try
                {
                    string exportSheetPath = Path.Combine(Utils.GetStartupPath, "export", _strXslt + ".xsl");

                    XslCompiledTransform objXslTransform;
                    try
                    {
                        objXslTransform = XslManager.GetTransformForFile(exportSheetPath); // Use the path for the export sheet.
                    }
                    catch (ArgumentException)
                    {
                        string strReturn = "Last write time could not be fetched when attempting to load " + _strXslt +
                                           Environment.NewLine;
                        Log.Debug(strReturn);
                        SetTextToWorkerResult(strReturn);
                        return;
                    }
                    catch (PathTooLongException)
                    {
                        string strReturn = "Last write time could not be fetched when attempting to load " + _strXslt +
                                           Environment.NewLine;
                        Log.Debug(strReturn);
                        SetTextToWorkerResult(strReturn);
                        return;
                    }
                    catch (UnauthorizedAccessException)
                    {
                        string strReturn = "Last write time could not be fetched when attempting to load " + _strXslt +
                                           Environment.NewLine;
                        Log.Debug(strReturn);
                        SetTextToWorkerResult(strReturn);
                        return;
                    }
                    catch (XsltException ex)
                    {
                        string strReturn = "Error attempting to load " + _strXslt + Environment.NewLine;
                        Log.Debug(strReturn);
                        Log.Error("ERROR Message = " + ex.Message);
                        strReturn += ex.Message;
                        SetTextToWorkerResult(strReturn);
                        return;
                    }

                    if (_objXmlGeneratorCancellationTokenSource.IsCancellationRequested)
                    {
                        return;
                    }

                    XmlWriterSettings objSettings = objXslTransform.OutputSettings.Clone();
                    objSettings.CheckCharacters  = false;
                    objSettings.ConformanceLevel = ConformanceLevel.Fragment;

                    string strText;
                    using (MemoryStream objStream = new MemoryStream())
                    {
                        using (XmlWriter objWriter = XmlWriter.Create(objStream, objSettings))
                            objXslTransform.Transform(_objCharacterXml, null, objWriter);
                        if (_objXmlGeneratorCancellationTokenSource.IsCancellationRequested)
                        {
                            return;
                        }
                        objStream.Position = 0;

                        // Read in the resulting code and pass it to the browser.
                        using (StreamReader objReader = new StreamReader(objStream, Encoding.UTF8, true))
                            strText = await objReader.ReadToEndAsync();
                    }

                    SetTextToWorkerResult(strText);
                }
                finally
                {
                    await cmdOK.DoThreadSafeAsync(() => cmdOK.Enabled = true);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Run the generated XML file through the XSL transformation engine to create the file output.
        /// </summary>
        private async Task AsyncGenerateOutput()
        {
            using (new CursorWait(this))
            {
                await Task.WhenAll(this.DoThreadSafeAsync(() =>
                {
                    tsPrintPreview.Enabled = false;
                    tsSaveAsHtml.Enabled = false;
                }),
                                   cmdPrint.DoThreadSafeAsync(() => cmdPrint.Enabled         = false),
                                   cmdSaveAsPdf.DoThreadSafeAsync(() => cmdSaveAsPdf.Enabled = false));
                await SetDocumentText(await LanguageManager.GetStringAsync("String_Generating_Sheet"));

                string strXslPath = Path.Combine(Utils.GetStartupPath, "sheets", _strSelectedSheet + ".xsl");
                if (!File.Exists(strXslPath))
                {
                    string strReturn = "File not found when attempting to load " + _strSelectedSheet +
                                       Environment.NewLine;
                    Log.Debug(strReturn);
                    Program.MainForm.ShowMessageBox(this, strReturn);
                    return;
                }

                XslCompiledTransform objXslTransform;
                try
                {
                    objXslTransform = XslManager.GetTransformForFile(strXslPath);
                }
                catch (ArgumentException)
                {
                    string strReturn = "Last write time could not be fetched when attempting to load " + _strSelectedSheet +
                                       Environment.NewLine;
                    Log.Debug(strReturn);
                    Program.MainForm.ShowMessageBox(this, strReturn);
                    return;
                }
                catch (PathTooLongException)
                {
                    string strReturn = "Last write time could not be fetched when attempting to load " + _strSelectedSheet +
                                       Environment.NewLine;
                    Log.Debug(strReturn);
                    Program.MainForm.ShowMessageBox(this, strReturn);
                    return;
                }
                catch (UnauthorizedAccessException)
                {
                    string strReturn = "Last write time could not be fetched when attempting to load " + _strSelectedSheet +
                                       Environment.NewLine;
                    Log.Debug(strReturn);
                    Program.MainForm.ShowMessageBox(this, strReturn);
                    return;
                }
                catch (XsltException ex)
                {
                    string strReturn = "Error attempting to load " + _strSelectedSheet + Environment.NewLine;
                    Log.Debug(strReturn);
                    Log.Error("ERROR Message = " + ex.Message);
                    strReturn += ex.Message;
                    Program.MainForm.ShowMessageBox(this, strReturn);
                    return;
                }

                if (_objOutputGeneratorCancellationTokenSource.IsCancellationRequested)
                {
                    return;
                }

                using (MemoryStream objStream = new MemoryStream())
                {
                    using (XmlTextWriter objWriter = new XmlTextWriter(objStream, Encoding.UTF8))
                    {
                        objXslTransform.Transform(_objCharacterXml, objWriter);
                        if (_objOutputGeneratorCancellationTokenSource.IsCancellationRequested)
                        {
                            return;
                        }

                        objStream.Position = 0;

                        // This reads from a static file, outputs to an HTML file, then has the browser read from that file. For debugging purposes.
                        //objXSLTransform.Transform("D:\\temp\\print.xml", "D:\\temp\\output.htm");
                        //webBrowser1.Navigate("D:\\temp\\output.htm");

                        if (GlobalSettings.PrintToFileFirst)
                        {
                            // The DocumentStream method fails when using Wine, so we'll instead dump everything out a temporary HTML file, have the WebBrowser load that, then delete the temporary file.

                            // Delete any old versions of the file
                            if (!await Utils.SafeDeleteFileAsync(_strTempSheetFilePath, true))
                            {
                                return;
                            }

                            // Read in the resulting code and pass it to the browser.
                            using (StreamReader objReader = new StreamReader(objStream, Encoding.UTF8, true))
                            {
                                string strOutput = await objReader.ReadToEndAsync();

                                File.WriteAllText(_strTempSheetFilePath, strOutput);
                            }

                            await this.DoThreadSafeAsync(() => UseWaitCursor = true);

                            await webViewer.DoThreadSafeAsync(
                                () => webViewer.Url = new Uri("file:///" + _strTempSheetFilePath));
                        }
                        else
                        {
                            // Populate the browser using DocumentText (DocumentStream would cause issues due to stream disposal).
                            using (StreamReader objReader = new StreamReader(objStream, Encoding.UTF8, true))
                            {
                                string strOutput = await objReader.ReadToEndAsync();

                                await this.DoThreadSafeAsync(() => UseWaitCursor = true);

                                await webViewer.DoThreadSafeAsync(() => webViewer.DocumentText = strOutput);
                            }
                        }
                    }
                }
            }
        }