예제 #1
0
        //R4.2 - Implement the convert method
        public static object Convert(object Input, string InputFormat, string OutputFormat)
        {
            //We add the known converters here
            RegisterFileConverter(new ConverterJpgPng());

            List <ConverterStack> ConverterStacks = new List <ConverterStack>();

            TheValidConverterStack = new ConverterStack();

            //find an appropriate converter stack and execute
            AddConvertersWhichCanConvertTheInputFormat(ConverterStacks, InputFormat, OutputFormat); //which are guaranteed to convert the inputformat


            int MaximumStackDepth = 10;

            if (!ValidStackFound)
            {
                for (int i = 2; i <= MaximumStackDepth; i++)
                {
                    List <ConverterStack> TheLongestStacks = new List <ConverterStack>();
                    var query = from ConverterStack cs in ConverterStacks where cs.StackDepth == i - 1 select cs;
                    TheLongestStacks = query.ToList();
                    foreach (ConverterStack cs in TheLongestStacks)
                    {
                        foreach (IFileConverter fc in FileConvertersAvailable)
                        {
                            if (fc.InputFileFormat == cs.Output)
                            {
                                cs.Stack.Add(fc);
                            }
                            ConverterStacks.Add(cs);
                            if (cs.Output == OutputFormat)
                            {
                                TheValidConverterStack = cs;
                                ValidStackFound        = true;
                                break;
                            }
                        }
                        if (ValidStackFound)
                        {
                            break;
                        }
                    }
                    if (ValidStackFound)
                    {
                        break;
                    }
                }
                if (!ValidStackFound)
                {
                    throw new Exception("Conversion is not possible");
                }
                return(null);
            }
            else
            {
                return(ApplyTheValidConverterStack(Input, TheValidConverterStack));
            }
        }
예제 #2
0
        private static object ApplyTheValidConverterStack(object Input, ConverterStack TheValidConverterStack)
        {
            object input  = Input.ToString();
            object output = null;

            foreach (IFileConverter FileConverterInstance in TheValidConverterStack.Stack)
            {
                output = FileConverterInstance.Convert(input);
            }
            return(output);
        }
예제 #3
0
        private static void AddConvertersWhichCanConvertTheInputFormat(List <ConverterStack> ConverterStacks, string pInputFormat, string pOutputFormat)
        {
            foreach (IFileConverter fc in FileConvertersAvailable)
            {
                bool CanConvertFromInput = fc.InputFileFormat == pInputFormat;
                if (CanConvertFromInput)
                {
                    //add Qualifying Converters to Stack
                    List <IFileConverter> ConvertersWhichCanConvertInput = new List <IFileConverter>();
                    ConvertersWhichCanConvertInput.Add(fc);

                    //We will add converters to the lists in these objects to generate chains of converters
                    ConverterStack NewConverterStack = new ConverterStack(fc.InputFileFormat, fc.OutputFileFormat, ConvertersWhichCanConvertInput, 1);
                    ConverterStacks.Add(NewConverterStack);

                    bool CanConvertToOutput = fc.OutputFileFormat == pOutputFormat;
                    if (CanConvertToOutput)
                    {
                        ValidStackFound        = true;
                        TheValidConverterStack = NewConverterStack;
                    }
                }
            }
        }