示例#1
0
        private async Task <ParserResult> RunAsync(ParserObject parserObject)
        {
            var result = new List <ParserResult>();

            var loader    = new HtmlLoader();
            var domParser = new HtmlParser();

            string dom = await loader.GetSourceByUri(parserObject.Uri);

            if (string.IsNullOrEmpty(dom))
            {
                throw new NullReferenceException($"Сould not get a response from {parserObject.Uri}");
            }

            IHtmlDocument document = await domParser.ParseDocumentAsync(dom);

            IElement domElement = document
                                  .QuerySelectorAll("td")
                                  .First(i => i.TextContent.Contains(parserObject.Pattern));

            string value = domElement
                           .SkipLine(document, parserObject.SkipAfterLines)
                           .QuerySelectorAll("span")
                           .Last(s => s.ClassName == parserObject.ClassName).TextContent;

            return(new ParserResult()
            {
                Hedar = parserObject.Pattern,
                Value = value
            });
        }
示例#2
0
        public override ParserObject Parse(string str)
        {
            var obj = new ParserObject();

            if (!str.Contains("<"))
            {
                obj.Type = ParserObject.ParserObjectType.Simple;
                var i = str.IndexOf('=');
                obj.Name  = str.Substring(1, i - 1).Trim();
                obj.Value = str.Substring(i + 1, str.Length - i);
            }
            else
            {
                var    tag        = str.Substring(1, str.IndexOf('>') - 1);
                var    closingTag = "</" + tag + ">";
                string prop       = "<>";
                if (str.Contains(closingTag))
                {
                    prop = str.Replace("</" + tag + ">", "").Replace("<" + tag + ">", "");
                    if (!prop.Contains("<"))
                    {
                        obj.Type  = ParserObject.ParserObjectType.Simple;
                        obj.Name  = tag;
                        obj.Value = prop;
                        return(obj);
                    }
                }

                obj.Type = ParserObject.ParserObjectType.Complex;
                if (tag.Contains("="))
                {
                    var i = str.IndexOf(' ');
                    obj.Name  = str.Substring(0, i).Trim();
                    obj.Value = Split(str.Substring(i + 1, str.Length - i - 1).Trim());
                    if (tag.EndsWith("/"))
                    {
                        return(obj);
                    }
                }
                else
                {
                    obj.Name  = tag.Trim();
                    obj.Value = new List <ParserObject>();
                }
                var list = obj.Value as List <ParserObject>;
                prop = str.Replace("</" + obj.Name + ">", "").Replace(tag, "");
                list.AddRange(Split(prop));
            }
            return(obj);
        }
示例#3
0
        public override ParserObject Parse(string str)
        {
            var obj = new ParserObject();
            int i   = 1;

            while (str[i] != '"')
            {
                i++;
            }
            obj.Name = str.Substring(1, i - 1);
            i++;
            while (str[i] == ' ' || str[i] == ':')
            {
                i++;
            }
            var  oc = str[i];
            char cc = (char)0;

            if (oc == '{')
            {
                obj.Type = ParserObject.ParserObjectType.Complex;
                cc       = '}';
            }
            else
            if (oc == '[')
            {
                obj.Type = ParserObject.ParserObjectType.Array;
                cc       = ']';
            }
            else
            {
                obj.Type = ParserObject.ParserObjectType.Simple;
                if (oc == '"')
                {
                    cc = '"';
                }
            }
            var j     = i + 1;
            var count = 1;

            while (count != 0 && j < str.Length)
            {
                if (str[j] == cc)
                {
                    count--;
                }
                else
                if (str[j] == oc)
                {
                    count++;
                }
                j++;
            }
            var value = str.Substring(i + 1, j - 2 - i).Trim();

            if (obj.Type != ParserObject.ParserObjectType.Simple)
            {
                obj.Value = Split(value);
            }
            else
            {
                obj.Value = value;
            }

            return(obj);
        }
示例#4
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);
        }