예제 #1
0
        /// <summary>
        /// 変換を行います。
        /// </summary>
        /// <param name="parameter">変換パラメーター。</param>
        public ConvertResultType Convert(IConvertParameter parameter)
        {
            var cea = new CancelEventArgs();

            cea.Cancel = false;
            Prepare?.Invoke(parameter, cea);
            if (cea.Cancel)
            {
                var r = new ConvertResultType();
                r = ConvertResultType.Cancelled;
                var ccea = new ConvertCompleteEventArgs();
                ccea.Result = ConvertResultType.Cancelled;
                ConvertCompleted?.Invoke(parameter, ccea);
                return(r);
            }

            try
            {
                return(onConvert(parameter));
            }
            catch (Exception e)
            {
                var result = new ConvertCompleteEventArgs();
                result.Result = ConvertResultType.Failed;
                result.Error  = e;
                ConvertCompleted?.Invoke(parameter, result);
                return(result.Result);
            }
        }
예제 #2
0
        /// <summary>
        /// 変換の呼び出しを実施する。
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        private ConvertResultType onConvert(IConvertParameter parameter)
        {
            var result = new ConvertCompleteEventArgs();

            // 変換処理
            DoConvert(parameter);

            result.Result = ConvertResultType.Succeed;
            result.Error  = null;
            ConvertCompleted?.Invoke(parameter, result);
            return(result.Result);
        }
예제 #3
0
        /// <summary>
        /// 変換を行います。
        /// </summary>
        /// <param name="parameter">変換パラメーター。</param>
        /// <returns>ConvertResult のタスク。</returns>
        public async Task <ConvertResultType> ConvertAsync(IConvertParameter parameter)
        {
            var e = new CancelEventArgs();

            e.Cancel = false;
            Prepare?.Invoke(parameter, e);
            if (e.Cancel)
            {
                var r = new ConvertResultType();
                r = ConvertResultType.Cancelled;
                var cea = new ConvertCompleteEventArgs();
                cea.Result = ConvertResultType.Cancelled;
                ConvertCompleted?.Invoke(parameter, cea);
                return(r);
            }

            var result = await onConvertTask(parameter);

            return(result);
        }
예제 #4
0
        /// <summary>
        /// 変換を行う。
        /// </summary>
        /// <param name="parameter">変換パラメーター。</param>
        /// <param name="token">キャンセルトークン。</param>
        /// <returns>変換結果。</returns>
        private ConvertResultType Convert(T parameter, CancellationToken token)
        {
            Parameter = parameter;

            var cea = new CancelEventArgs();

            cea.Cancel = false;
            Prepare?.Invoke(parameter, cea);
            if (cea.Cancel)
            {
                var ccea = new ConvertCompleteEventArgs();
                ccea.Result = ConvertResultType.Cancelled;
                Completed?.Invoke(parameter, ccea);
                return(ConvertResultType.Cancelled);
            }

            try
            {
                token.ThrowIfCancellationRequested();

                var result = DoConvert(parameter, token);
                Completed?.Invoke(parameter, result);
                return(result.Result);
            }
            catch (OperationCanceledException ex)
            {
                var result = new ConvertCompleteEventArgs();
                result.Result = ConvertResultType.Cancelled;
                result.Error  = ex;
                Completed?.Invoke(parameter, result);
                return(result.Result);
            }
            catch (Exception ex)
            {
                var result = new ConvertCompleteEventArgs();
                result.Result = ConvertResultType.Failed;
                result.Error  = ex;
                Completed?.Invoke(parameter, result);
                return(result.Result);
            }
        }
예제 #5
0
        /// <summary>
        /// 非同期で変換を行うためのタスクメソッド。
        /// </summary>
        /// <param name="parameter">変換パラメーター。</param>
        /// <returns>ConvertResult のタスク。</returns>
        private Task <ConvertResultType> onConvertTask(IConvertParameter parameter)
        {
            var task = new Task <ConvertResultType>(() =>
            {
                try
                {
                    return(onConvert(parameter));
                }
                catch (Exception e)
                {
                    var result    = new ConvertCompleteEventArgs();
                    result.Result = ConvertResultType.Failed;
                    result.Error  = e;
                    ConvertCompleted?.Invoke(parameter, result);
                    return(result.Result);
                }
            });

            task.Start();
            return(task);
        }
예제 #6
0
        /// <summary>
        /// 変換を行う。
        /// </summary>
        /// <param name="converter">変換コンバーター。</param>
        /// <param name="isReactive">反応したものかどうか。</param>
        /// <returns>ConvertCompleteEventArgs オブジェクト。</returns>
        private ConvertCompleteEventArgs ExecuteConvert(FileConverterBase converter, bool isReactive = false)
        {
            if (isReactive)
            {
                var cea = new CancelEventArgs();
                cea.Cancel = false;
                converter.Prepare?.Invoke(converter.Parameter, cea);
                if (cea.Cancel)
                {
                    var ccea = new ConvertCompleteEventArgs();
                    ccea.Result = ConvertResultType.Cancelled;
                    return(ccea);
                }
            }

            if (!CanConvert(converter.Parameter))
            {
                var exception = new ArgumentException(ErrorMessage);
                converter.ConvertFailed?.Invoke(converter.Parameter, exception);
                throw exception;
            }

            // 変換元の一時パスを決定
            converter.Parameter.SourceConvertFileName = converter.Parameter.SourceFileName;
            if (converter.Parameter.UseSourceTemporary)
            {
                converter.Parameter.SourceConvertFileName = CreateTempFileName(converter.Parameter.SourceTempDirectory, converter.Parameter.SourceFileName);
            }
            if (converter.Parameter.OriginalFileName == null)
            {
                converter.Parameter.OriginalFileName = converter.Parameter.SourceConvertFileName;
            }

            // 変換先の一時パスを決定
            converter.Parameter.DestConvertFileName = converter.Parameter.DestFileName;
            if (converter.Parameter.UseDestTemporary)
            {
                converter.Parameter.DestConvertFileName = CreateTempFileName(converter.Parameter.DestTempDirectory, converter.Parameter.DestFileName);
            }

            // 変換元ファイルを一時パスへコピー
            if (converter.Parameter.UseSourceTemporary)
            {
                File.Copy(converter.Parameter.SourceFileName, converter.Parameter.SourceConvertFileName, true);
            }

            try
            {
                var result = new ConvertCompleteEventArgs();
                result.Result = ConvertResultType.Succeed;

                // 変換実行
                converter.ConvertFile(converter.Parameter);
                converter.CopyDestFile(converter.Parameter);
                converter.ConvertSucceed?.Invoke(converter.Parameter);

                // 反応変換の実行
                if (converter.ReactiveConvert != null)
                {
                    foreach (var reactive in converter.ReactiveConvert)
                    {
                        reactive.Parent = converter;
                        reactive.Parameter.OriginalFileName = converter.Parameter.OriginalFileName;
                        switch (reactive.Parameter.ReactiveTarget)
                        {
                        case ReactiveFileTarget.Original:
                            reactive.Parameter.SourceFileName = converter.Parameter.OriginalFileName;
                            break;

                        case ReactiveFileTarget.RecentOriginal:
                            reactive.Parameter.SourceFileName = converter.Parameter.SourceConvertFileName;
                            break;

                        case ReactiveFileTarget.RecentConvert:
                            reactive.Parameter.SourceFileName = converter.Parameter.DestConvertFileName;
                            break;

                        case ReactiveFileTarget.RecentDest:
                            reactive.Parameter.SourceFileName = converter.Parameter.DestFileName;
                            break;
                        }
                        try
                        {
                            var reactiveResult = ExecuteConvert(reactive, true);

                            // 異常が発生した場合は変換を強制終了する
                            if (reactiveResult.Result == ConvertResultType.Failed)
                            {
                                result = reactiveResult;
                                break;
                            }
                        }
                        catch (Exception ex)
                        {
                            // 異常が発生した場合は変換を強制終了する
                            result.Result = ConvertResultType.Failed;
                            result.Error  = ex;
                            break;
                        }
                    }
                }

                converter.DeleteTemporary(converter.Parameter);
                return(result);
            }
            catch (Exception ex)
            {
                converter.DeleteTemporary(converter.Parameter);
                converter.ConvertFailed?.Invoke(converter.Parameter, ex);
                throw;
            }
        }