Exemplo n.º 1
0
 public bool SetParseResult(string res)
 {
     UiDispatcher?.Invoke(() => { ParseResult.Add(res); }); return(true);
 }
        public override ParseResult <TResult> Parse()
        {
            var result  = new ParseResult <TResult>();
            var regexes = regexList.Cast <KeyValuePair <string, DeviceModel> >();

            if (!regexes.Any())
            {
                return(result);
            }

            KeyValuePair <string, DeviceModel> localDevice = new KeyValuePair <string, DeviceModel>(null, null);

            string[] localMatches = null;
            foreach (var regex in regexes)
            {
                var matches = MatchUserAgent(regex.Value.Regex);
                if (matches.Length > 0)
                {
                    localDevice  = regex;
                    localMatches = matches;
                    break;
                }
            }

            if (localMatches == null)
            {
                return(result);
            }

            if (!localDevice.Key.Equals(UnknownBrand))
            {
                var localBrand = DeviceBrands.SingleOrDefault(x => x.Value == localDevice.Key).Key;
                if (string.IsNullOrEmpty(localBrand))
                {
                    // This Exception should never be thrown. If so a defined brand name is missing in DeviceBrands
                    throw new Exception("The brand with name '" + localDevice.Key + "' should be listed in the deviceBrands array.");
                }
                brand = localBrand;
            }

            if (localDevice.Value.Device != null && DeviceTypes.ContainsKey(localDevice.Value.Device))
            {
                DeviceTypes.TryGetValue(localDevice.Value.Device, out var localDeviceType);
                deviceType = localDeviceType;
            }
            model = "";
            if (!string.IsNullOrEmpty(localDevice.Value.Name))
            {
                model = BuildModel(localDevice.Value.Name, localMatches);
            }

            if (localDevice.Value.Models != null)
            {
                Model    localModel        = null;
                string[] localModelMatches = null;
                foreach (var localmodel in localDevice.Value.Models)
                {
                    var modelMatches = MatchUserAgent(localmodel.Regex);
                    if (modelMatches.Length > 0)
                    {
                        localModel        = localmodel;
                        localModelMatches = modelMatches;
                        break;
                    }
                }

                if (localModelMatches == null)
                {
                    result.Add(new TResult {
                        Name = model, Brand = brand, Type = deviceType
                    });
                    return(result);
                }

                model = BuildModel(localModel.Name, localModelMatches)?.Trim();

                if (localModel.Brand != null)
                {
                    var localBrand = DeviceBrands.SingleOrDefault(x => x.Value == localModel.Brand).Key;
                    if (!string.IsNullOrEmpty(brand))
                    {
                        brand = localBrand;
                    }
                }
                if (localModel.Device != null && DeviceTypes.ContainsKey(localModel.Device))
                {
                    DeviceTypes.TryGetValue(localModel.Device, out var localDeviceType);
                    deviceType = localDeviceType;
                }
            }
            result.Add(new TResult {
                Name = model, Brand = brand, Type = deviceType
            });

            return(result);
        }
Exemplo n.º 3
0
        protected override Task <DataFlowResult> Parse(DataFlowContext context)
        {
            var selectable = context.Selectable;

            var results = new ParseResult <T>();

            if (selectable.Properties == null)
            {
                selectable.Properties = new Dictionary <string, object>();
            }

            var environments = new Dictionary <string, string>();

            foreach (var property in context.Response.Request.Properties)
            {
                environments.Add(property.Key, property.Value);
            }

            if (Model.GlobalValueSelectors != null)
            {
                foreach (var selector in Model.GlobalValueSelectors)
                {
                    string name = selector.Name;
                    if (string.IsNullOrWhiteSpace(name))
                    {
                        continue;
                    }

                    var value = selectable.Select(selector.ToSelector()).GetValue();
                    if (!environments.ContainsKey(name))
                    {
                        environments.Add(name, value);
                    }
                    else
                    {
                        environments[name] = value;
                    }
                }
            }

            bool singleExtractor = Model.Selector == null;

            if (!singleExtractor)
            {
                var selector = Model.Selector.ToSelector();

                var list = selectable.SelectList(selector).Nodes()?.ToList();
                if (list != null)
                {
                    if (Model.Take > 0 && list.Count > Model.Take)
                    {
                        list = Model.TakeFromHead
                                                        ? list.Take(Model.Take).ToList()
                                                        : list.Skip(list.Count - Model.Take).ToList();
                    }

                    for (var i = 0; i < list.Count; ++i)
                    {
                        var item = list.ElementAt(i);
                        var obj  = ParseObject(context, environments, item, i);
                        if (obj != null)
                        {
                            results.Add(obj);
                        }
                        else
                        {
                            Logger?.LogWarning($"解析到空数据,类型: {Model.TypeName}");
                        }
                    }
                }
            }
            else
            {
                var obj = ParseObject(context, environments, selectable, 0);
                if (obj != null)
                {
                    results.Add(obj);
                }
                else
                {
                    Logger?.LogWarning($"解析到空数据,类型: {Model.TypeName}");
                }
            }

            AddParseResult(context, results);

            return(base.Parse(context));
        }
Exemplo n.º 4
0
        public ParseResult Parse(string[] args, IEnumerable <ArgumentRaw> argumentsRaw, IEnumerable <CommandMap> commandsMap, bool enableMultiAction)
        {
            var parseResult = new ParseResult();

            parseResult.Args = args;
            parseResult.Maps = commandsMap;
            parseResult.EnableMultiAction = enableMultiAction;

            IEnumerable <ArgumentRaw> initialExtraArguments;
            var allPropertiesMap = commandsMap.GetProperties();
            var allMethodsMaps   = commandsMap.GetMethods();
            var hasPropertyMap   = allPropertiesMap.Any();

            var methodsParsed    = this.actionParser.Parse(argumentsRaw, enableMultiAction, allMethodsMaps, out initialExtraArguments).ToList();
            var hasMethodsParsed = methodsParsed.Count > 0;

            // *******
            // Step1: Parse properties if has initial extras
            // *******
            if (initialExtraArguments.Any())
            {
                var levelInitial = this.GetLevelWithProperties(commandsMap, initialExtraArguments);
                if (!this.IsEmptyLevel(levelInitial))
                {
                    parseResult.Add(levelInitial);
                }
            }

            if (hasMethodsParsed)
            {
                // Separate per level
                var levelsGroups = methodsParsed.GroupBy(f => f.Level).OrderBy(f => f.Key);
                foreach (var levelGroup in levelsGroups)
                {
                    // Separate per Command
                    var commandsGroups = levelGroup.GroupBy(f => f.ActionMap.Target);

                    // Step2 & Step 3: Create level only with methods and separate valids and invalids methods
                    var levelOfMethods = this.GetLevelWithMethods(hasPropertyMap, commandsGroups);
                    if (levelOfMethods == null)
                    {
                        continue;
                    }

                    var bestValidMethodInLevel = this.GetBestValidMethodInLevel(levelOfMethods);

                    if (bestValidMethodInLevel != null)
                    {
                        // Step2: Add or not the method
                        var canAddMethodLevel = true;

                        if (this.IsMethodImplicitAndNoArguments(bestValidMethodInLevel))
                        {
                            // Step2.1: If has args, this default implicit method can't be added.
                            //            because it is invoked only if no has input.
                            //          eg:
                            //          method: void defaultMethodWithoutArgs()
                            //          input : $ --some-args value
                            //          result: can't executed because is invalid.
                            if (args.Length > 0)
                            {
                                canAddMethodLevel = false;
                            }
                        }

                        if (canAddMethodLevel)
                        {
                            // Step2.2: Remove imcompatible valid methods
                            this.RemoveIncompatibleValidMethods(levelOfMethods, bestValidMethodInLevel);
                            if (!this.IsEmptyLevel(levelOfMethods))
                            {
                                parseResult.Add(levelOfMethods);
                            }
                        }

                        // Step3: Get extras of the best method and use as properties input to create a new level.
                        var levelOfProperties = this.GetLevelWithPropertiesUsingMethodExtras(commandsMap, bestValidMethodInLevel);
                        if (levelOfProperties != null && !this.IsEmptyLevel(levelOfProperties))
                        {
                            parseResult.Add(levelOfProperties);
                        }
                    }
                    else
                    {
                        // Step4 & Step5: When not exists valid methods
                        var allRemoved = this.RemoveInvalidImplicitDefaultMethodsIfExists(args, levelOfMethods, hasPropertyMap);
                        if (allRemoved)
                        {
                            // Step4: All defaults and invalid methods have been ignored to maintain the fluency
                            // and the process of following with properties.

                            // select all raw before 'level 1'
                            IEnumerable <ArgumentRaw> raws;
                            var methodLevel1 = methodsParsed.FirstOrDefault(f => f.Level == 1);
                            if (methodLevel1 != null)
                            {
                                var rawsAux = new List <ArgumentRaw>();
                                foreach (var raw in argumentsRaw)
                                {
                                    if (raw != methodLevel1.ArgumentRawOfAction)
                                    {
                                        rawsAux.Add(raw);
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }

                                raws = rawsAux;
                            }
                            else
                            {
                                raws = argumentsRaw;
                            }

                            var levelOfProperties = this.GetLevelWithProperties(commandsMap, raws);
                            if (levelOfProperties != null && !this.IsEmptyLevel(levelOfProperties))
                            {
                                parseResult.Add(levelOfProperties);
                            }
                        }
                        else
                        {
                            // Step5: There are some default method that has a real problem.
                            parseResult.Add(levelOfMethods);
                        }
                    }
                }
            }

            // *******
            // Step5: Create properties with default values if not exists
            //        Create required properties if not exists (to show errors)
            // *******
            var levelDefaultOrRequired = this.GetLevelWithPropertiesDefaultOrRequired(parseResult.Levels, commandsMap);

            if (levelDefaultOrRequired != null && !this.IsEmptyLevel(levelDefaultOrRequired))
            {
                parseResult.Add(levelDefaultOrRequired);
            }

            // *******
            // Step6: Organize level number
            // *******
            this.OrganizeLevelsSequence(parseResult);

            return(parseResult);
        }