Esempio n. 1
0
        public void ConvertXml(XDocument doc, string outFileName, IProgressUpdateInterface progress)
        {
            LoadSettings();
            _processorSettings.DeleteSource = false;

            var progressReporter = new ProgressUpdateWrapper(_processorSettings.ProgressCallbacks);

            progressReporter.ConvertStarted(1);


            IFb2EPubConverterEngine converter = CreateConverterEngine(_processorSettings.Settings);

            try
            {
                progressReporter.ProcessingStarted(outFileName);
                converter.LoadFB2FileFromXML(doc);
            }
            catch (Exception ex)
            {
                Logger.Log.Error(ex);
                return;
            }
            progressReporter.ProcessingSaving(outFileName);
            SaveAndCleanUp(converter, outFileName, "");
            progressReporter.Processed(outFileName);
            progressReporter.ConvertFinished(0);
        }
Esempio n. 2
0
        /// <summary>
        /// Performs conversion of list of files 
        /// </summary>
        /// <param name="filesInMask">list of files to convert</param>
        /// <param name="outputFileName">output file name in case provided and single file is to be converted </param>
        public bool PerformConvertOperation(List<string> filesInMask, string outputFileName)
        {
            int filesCount = filesInMask.Count;

            Logger.Log.InfoFormat("Using resources from: {0}",_processorSettings.Settings.ResourcesPath);
            var progressReporter = new ProgressUpdateWrapper(_processorSettings.ProgressCallbacks);
            progressReporter.ConvertStarted(filesCount);
            Logger.Log.InfoFormat("Conversion process started at {0}",DateTime.Now.ToString("f"));

            var po = new ParallelOptions
            {
                CancellationToken = _cts.Token,
                MaxDegreeOfParallelism = Environment.ProcessorCount
            };

            bool conversionResult = true;

            try
            {
                Parallel.ForEach(filesInMask, po, file =>
                {
                    po.CancellationToken.ThrowIfCancellationRequested();

                    IFb2EPubConverterEngine converter = CreateConverterEngine(_processorSettings.Settings);

                    try
                    {
                        progressReporter.ProcessingStarted(file);
                        if (!converter.LoadAndCheckFB2Files(file))
                        {
                            string error = string.Format("Conversion of a file {0} failed", file);
                            Logger.Log.Error(error);
                            progressReporter.SkippedDueError(file);
                            if (_processorSettings.SingleFile)
                            {
                                throw new Exception(error);
                            }
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        const string error = "Conversion error";
                        Logger.Log.Error(error, ex);
                        progressReporter.SkippedDueError(file);
                        if (_processorSettings.SingleFile)
                        {
                            throw new Exception(error);
                        }
                        return;
                    }
                    string fileName = BuildNewFileName(file, outputFileName);
                    Logger.Log.InfoFormat("Saving {0}...", fileName);
                    progressReporter.ProcessingSaving(fileName);
                    SaveAndCleanUp(converter, fileName, file);
                    progressReporter.Processed(fileName);
                });
            }
            catch (OperationCanceledException )
            {
                conversionResult = false;
                Logger.Log.Info("Operation was canceled");
                _cts = new CancellationTokenSource();
            }
            catch(Exception ex)
            {
                conversionResult = false;
                Logger.Log.Error("Error converting file, exception: ",ex);
            }
            finally
            {
                progressReporter.ConvertFinished(0);
                Logger.Log.Info("Conversion process finished");
            }

            return conversionResult;
        }
Esempio n. 3
0
        public void ConvertXml(XDocument doc, string outFileName, IProgressUpdateInterface progress)
        {
            LoadSettings();
            _processorSettings.DeleteSource = false;

            var progressReporter = new ProgressUpdateWrapper(_processorSettings.ProgressCallbacks);
            progressReporter.ConvertStarted(1);


            IFb2EPubConverterEngine converter = CreateConverterEngine(_processorSettings.Settings);

            try
            {
                progressReporter.ProcessingStarted(outFileName);
                converter.LoadFB2FileFromXML(doc);
            }
            catch (Exception ex)
            {
                Logger.Log.Error(ex);
                return;
            }
            progressReporter.ProcessingSaving(outFileName);
            SaveAndCleanUp(converter, outFileName, "");
            progressReporter.Processed(outFileName);
            progressReporter.ConvertFinished(0);
        }
Esempio n. 4
0
        /// <summary>
        /// Performs conversion of list of files
        /// </summary>
        /// <param name="filesInMask">list of files to convert</param>
        /// <param name="outputFileName">output file name in case provided and single file is to be converted </param>
        public bool PerformConvertOperation(List <string> filesInMask, string outputFileName)
        {
            int filesCount = filesInMask.Count;

            Logger.Log.InfoFormat("Using resources from: {0}", _processorSettings.Settings.ResourcesPath);
            var progressReporter = new ProgressUpdateWrapper(_processorSettings.ProgressCallbacks);

            progressReporter.ConvertStarted(filesCount);
            Logger.Log.InfoFormat("Conversion process started at {0}", DateTime.Now.ToString("f"));

            var po = new ParallelOptions
            {
                CancellationToken      = _cts.Token,
                MaxDegreeOfParallelism = Environment.ProcessorCount
            };

            bool conversionResult = true;

            try
            {
                Parallel.ForEach(filesInMask, po, file =>
                {
                    po.CancellationToken.ThrowIfCancellationRequested();

                    IFb2EPubConverterEngine converter = CreateConverterEngine(_processorSettings.Settings);

                    try
                    {
                        progressReporter.ProcessingStarted(file);
                        if (!converter.LoadAndCheckFB2Files(file))
                        {
                            string error = string.Format("Conversion of a file {0} failed", file);
                            Logger.Log.Error(error);
                            progressReporter.SkippedDueError(file);
                            if (_processorSettings.SingleFile)
                            {
                                throw new Exception(error);
                            }
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        const string error = "Conversion error";
                        Logger.Log.Error(error, ex);
                        progressReporter.SkippedDueError(file);
                        if (_processorSettings.SingleFile)
                        {
                            throw new Exception(error);
                        }
                        return;
                    }
                    string fileName = BuildNewFileName(file, outputFileName);
                    Logger.Log.InfoFormat("Saving {0}...", fileName);
                    progressReporter.ProcessingSaving(fileName);
                    SaveAndCleanUp(converter, fileName, file);
                    progressReporter.Processed(fileName);
                });
            }
            catch (OperationCanceledException)
            {
                conversionResult = false;
                Logger.Log.Info("Operation was canceled");
                _cts = new CancellationTokenSource();
            }
            catch (Exception ex)
            {
                conversionResult = false;
                Logger.Log.Error("Error converting file, exception: ", ex);
            }
            finally
            {
                progressReporter.ConvertFinished(0);
                Logger.Log.Info("Conversion process finished");
            }

            return(conversionResult);
        }