Example #1
0
        public static IDomain Parse(string inputFolderName, string homePageName, string colorThemeName, string unitTestName, IDictionary <ConditionalDefine, bool> conditionalDefineTable)
        {
            if (inputFolderName == null)
            {
                throw new ArgumentNullException(nameof(inputFolderName));
            }
            if (homePageName == null)
            {
                throw new ArgumentNullException(nameof(homePageName));
            }

            if (!Directory.Exists(inputFolderName))
            {
                throw new ParsingException(1, inputFolderName, "Input folder not found.");
            }

            if (homePageName.Length == 0)
            {
                throw new ParsingException(2, inputFolderName, "Empty home page name.");
            }

            InitAttachedProperties();

            IFormParser FormParserAreas       = new ParserArea("area", "txt");
            IFormParser FormParserDesigns     = new ParserDesign("design", "xaml");
            IFormParser FormParserLayouts     = new ParserLayout("layout", "xaml");
            IFormParser FormParserObjects     = new ParserObject("object", "txt");
            IFormParser FormParserPages       = new ParserPage("page", "txt");
            IFormParser FormParserResources   = new ParserResource("resource", "png");
            IFormParser FormParserBackgrounds = new ParserBackground("background", "xaml");
            IFormParser FormParserColorThemes = new ParserColorTheme("color", "txt");
            IFormParser FormParserFonts       = new ParserFont("font", "ttf");
            IFormParser FormParserDynamics    = new ParserDynamic("dynamic", "txt");
            IFormParser FormParserUnitTest    = new ParserUnitTest("unit_test", "txt");

            IFormParserCollection FormParsers = new FormParserCollection()
            {
                FormParserAreas,
                FormParserDesigns,
                FormParserLayouts,
                FormParserObjects,
                FormParserPages,
                FormParserResources,
                FormParserBackgrounds,
                FormParserColorThemes,
                FormParserFonts,
                FormParserDynamics,
                FormParserUnitTest,
            };

            string[] FolderNames;
            try
            {
                FolderNames = Directory.GetDirectories(inputFolderName, "*.*");
            }
            catch (Exception e)
            {
                throw new ParsingException(3, inputFolderName, e.Message);
            }

            foreach (string FullFolderName in FolderNames)
            {
                bool   Parsed     = false;
                string FolderName = Path.GetFileName(FullFolderName);

                foreach (IFormParser FormParser in FormParsers)
                {
                    if (FolderName == FormParser.FolderName)
                    {
                        ParseForm(FormParser, Path.Combine(inputFolderName, FolderName), conditionalDefineTable);
                        Parsed = true;
                        break;
                    }
                }

                if (!Parsed)
                {
                    throw new ParsingException(4, FullFolderName, $"Unexpected folder '{FolderName}'.");
                }
            }

            foreach (IFormParser FormParser in FormParsers)
            {
                if (FormParser.ParsedResult == null)
                {
                    throw new ParsingException(5, inputFolderName, $"Missing folder '{FormParser.FolderName}'.");
                }
            }

            IFormCollection <IArea>       Areas       = (IFormCollection <IArea>)FormParserAreas.ParsedResult;
            IFormCollection <IDesign>     Designs     = (IFormCollection <IDesign>)FormParserDesigns.ParsedResult;
            IFormCollection <ILayout>     Layouts     = (IFormCollection <ILayout>)FormParserLayouts.ParsedResult;
            IFormCollection <IObject>     Objects     = (IFormCollection <IObject>)FormParserObjects.ParsedResult;
            IFormCollection <IPage>       Pages       = (IFormCollection <IPage>)FormParserPages.ParsedResult;
            IFormCollection <IResource>   Resources   = (IFormCollection <IResource>)FormParserResources.ParsedResult;
            IFormCollection <IBackground> Backgrounds = (IFormCollection <IBackground>)FormParserBackgrounds.ParsedResult;
            IFormCollection <IColorTheme> ColorThemes = (IFormCollection <IColorTheme>)FormParserColorThemes.ParsedResult;
            IFormCollection <IFont>       Fonts       = (IFormCollection <IFont>)FormParserFonts.ParsedResult;
            IFormCollection <IDynamic>    Dynamics    = (IFormCollection <IDynamic>)FormParserDynamics.ParsedResult;
            IFormCollection <IUnitTest>   UnitTests   = (IFormCollection <IUnitTest>)FormParserUnitTest.ParsedResult;

            string       TranslationFile = Path.Combine(inputFolderName, "translations.cvs");
            ITranslation Translation;

            if (File.Exists(TranslationFile))
            {
                Translation = new Translation(TranslationFile, '\t');
                Translation.Process(conditionalDefineTable);
            }
            else
            {
                Translation = null;
            }

            string PreprocessorDefineFile = Path.Combine(inputFolderName, "defines.txt");
            IPreprocessorDefine PreprocessorDefine;

            if (File.Exists(PreprocessorDefineFile))
            {
                PreprocessorDefine = new PreprocessorDefine(PreprocessorDefineFile);
                PreprocessorDefine.Process(conditionalDefineTable);
            }
            else
            {
                PreprocessorDefine = null;
            }

            IPage HomePage = null;

            foreach (IPage Page in Pages)
            {
                if (Page.Name == homePageName)
                {
                    HomePage = Page;
                    break;
                }
            }
            if (HomePage == null)
            {
                throw new ParsingException(6, inputFolderName, $"Home page '{homePageName}' not found.");
            }

            IColorTheme SelectedColorTheme = null;

            foreach (IColorTheme ColorTheme in ColorThemes)
            {
                if (ColorTheme.Name == colorThemeName)
                {
                    SelectedColorTheme = ColorTheme;
                    break;
                }
            }
            if (SelectedColorTheme == null)
            {
                throw new ParsingException(7, inputFolderName, $"Color theme '{colorThemeName}' not found.");
            }

            IUnitTest SelectedUnitTest = null;

            if (unitTestName != null)
            {
                foreach (IUnitTest UnitTest in UnitTests)
                {
                    if (UnitTest.Name == unitTestName)
                    {
                        SelectedUnitTest = UnitTest;
                        break;
                    }
                }
                if (SelectedUnitTest == null)
                {
                    throw new ParsingException(239, inputFolderName, $"Unit test '{unitTestName}' not found.");
                }
            }

            IDomain NewDomain = new Domain(inputFolderName,
                                           Areas,
                                           Designs,
                                           Layouts,
                                           Objects,
                                           Pages,
                                           Resources,
                                           Backgrounds,
                                           ColorThemes,
                                           Fonts,
                                           Dynamics,
                                           UnitTests,
                                           Translation,
                                           PreprocessorDefine,
                                           HomePage,
                                           SelectedColorTheme,
                                           SelectedUnitTest);

            bool IsConnected = true;

            for (int i = 0; i < 100 && IsConnected; i++)
            {
                IsConnected = false;

                foreach (IFormParser FormParser in FormParsers)
                {
                    foreach (IConnectable Connectable in FormParser.ParsedResult)
                    {
                        IsConnected |= Connectable.Connect(NewDomain);
                    }
                }

                if (Translation != null)
                {
                    IsConnected |= Translation.Connect(NewDomain);
                }
            }
            if (IsConnected)
            {
                throw new ParsingException(8, inputFolderName, $"Unexpected error during processing of the input folder.");
            }

            // Make sure areas used by other areas are first
            BubbleSort(Areas);

            // Remove all layouts and use their clone.
            //((List<ILayout>)Layouts).Clear();
            foreach (IPage Page in Pages)
            {
                foreach (KeyValuePair <IArea, ILayout> Entry in Page.AreaLayouts)
                {
                    Layouts.Add(Entry.Value);
                }
            }

            return(NewDomain);
        }