Beispiel #1
0
        /// <summary>
        /// Writes converted reports into a .flxr file.
        /// </summary>
        /// <param name="cfi">The reports to be written.</param>
        /// <param name="abort">OUT: if true, the user aborted operation.</param>
        private void WriteFile(ConvertFileItem cfi, out bool abort)
        {
            abort = false;
confirm_file:
            // Ask the user if a file with the target name already exists.
            if (File.Exists(cfi.OutputFile))
            {
                string msg = string.Format("File \"{0}\" exists. Overwrite?", cfi.OutputFile);
                switch (MessageBox.Show(this, msg, "Xml2FlxrConverter", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question))
                {
                case DialogResult.Yes:
                    abort = false;
                    break;

                case DialogResult.No:
                    using (var sfd = new SaveFileDialog())
                    {
                        sfd.Filter          = "FlexReport Report Definition Files (*.flxr)|*.flxr|All Files (*.*)|*.*";
                        sfd.OverwritePrompt = false;
                        sfd.FileName        = cfi.OutputFile;
                        if (sfd.ShowDialog(this) == DialogResult.Cancel)
                        {
                            abort = true;
                            return;
                        }
                        cfi.OutputFile = sfd.FileName;
                        goto confirm_file;
                    }

                case DialogResult.Cancel:
                    abort = true;
                    return;

                default:
                    System.Diagnostics.Debug.Assert(false);
                    abort = true;
                    return;
                }
            }

            // Write the .flxr file.
            // We first write the reports into a memory stream,
            // then if successfull write the stream into the disk file.
            // This minimizes the chance of corrupted files.
            try
            {
                using (MemoryStream ms = new MemoryStream())
                    using (XmlTextWriter w = new XmlTextWriter(ms, null)) // (null encoding uses utf-8 and suppresses it from xml header)
                    {
                        w.Formatting  = Formatting.Indented;
                        w.Indentation = 2;
                        w.WriteStartDocument();
                        // write all reports to it
                        w.WriteStartElement("Reports");
                        w.WriteStartElement("FormatVersion");
                        w.WriteString(C1FlexReport.FormatVersion.ToString());
                        w.WriteEndElement();
                        foreach (var cr in cfi.ConvertReports)
                        {
                            if (cr.Report != null)
                            {
                                cr.Report.Save(w, _embedImages, false);
                            }
                            else
                            {
                                LogLine(string.Format("Skipped failed conversion."));
                            }
                        }
                        w.WriteEndElement();
                        w.Flush();
                        // Copy content from memory stream to physical file:
                        using (FileStream fs = new FileStream(cfi.OutputFile, FileMode.Create))
                        {
                            ms.Seek(0, SeekOrigin.Begin);
                            ms.WriteTo(fs);
                            fs.Flush();
                            fs.Close();
                        }
                        w.Close();
                        ms.Close();
                    }
                LogLine(string.Format("Written \"{0}\"", cfi.OutputFile));
            }
            catch (Exception ex)
            {
                string msg = string.Format("Failed to write \"{0}\": {1}", cfi.OutputFile, ex.Message);
                LogLine(msg);
                cfi.Error = msg;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Converts a single file which may contain multiple reports.
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        private ConvertFileItem ConvertFile(string fileName)
        {
            // Create a ConvertFileItem object which will hold the info about the file being converted.
            // If successuflly converted, that object will be passed to the method writing the .flxr file.
            ConvertFileItem convertFile = new ConvertFileItem();

            convertFile.InputFile  = fileName;
            convertFile.OutputFile = Path.ChangeExtension(fileName, ".flxr");
            if (!string.IsNullOrEmpty(_txtOutputFolder.Text))
            {
                convertFile.OutputFile = Path.Combine(_txtOutputFolder.Text, Path.GetFileName(convertFile.OutputFile));
            }

            // load XML document
            XmlDocument doc = new XmlDocument();

            doc.PreserveWhitespace = true;
            try
            {
                LogLine("Converting " + fileName + "...");
                doc.Load(fileName);
            }
            catch (Exception ex)
            {
                string msg = string.Format("Could not load file: {0}.", ex.Message);
                LogLine(msg);
                convertFile.Error = msg;
                LogLine("Converting " + fileName + " aborted.");
                return(convertFile);
            }

            // Get the list of reports in the file.
            string[] reports = C1FlexReport.GetReportList(doc);
            if (reports == null || reports.Length == 0)
            {
                string msg = "No reports found in file.";
                LogLine(msg);
                convertFile.Error = msg;
                LogLine("Converting " + fileName + " aborted.");
                return(convertFile);
            }

            // Loop through reports, loading them into C1FlexReport instances, thus performing the conversion.
            int cnt = reports.Length;

            for (int i = 0; i < cnt; ++i)
            {
                ConvertReportItem convertReport = new ConvertReportItem();
                C1FlexReport      flexReport    = new C1FlexReport();
                try
                {
                    bool converted;
                    flexReport.Load(doc, reports[i], out converted);
                    convertReport.Report = flexReport;
                    LogLine(string.Format("    Converted {0}", flexReport.ReportName));
                }
                catch (Exception x)
                {
                    string msg = string.Format("Failed to convert \"{0}\": {1}", reports[i], x.Message);
                    LogLine("    " + msg);
                    convertReport.Error = msg;
                }
                convertFile.ConvertReports.Add(convertReport);
            }

            // Done. The convertFile object contains the list of converted reports.
            LogLine(fileName + " done.");
            return(convertFile);
        }