Resolve() public method

运行时解析模板中的数据绑定
public Resolve ( String raw ) : String
raw String
return String
Esempio n. 1
0
        public PhaseResult Run(Context context)
        {
            Account account = new Account();
            account.UserName = context.Resolve(Username);
            account.Password = context.Resolve(Password);
            context.Account = account;

            PhaseResult pr = new PhaseResult(this);
            pr.Succeed = true;
            return pr;
        }
Esempio n. 2
0
        public override PhaseResult Run(Context context)
        {
            GeckoWebBrowser browser = (GeckoWebBrowser)context.GetService(typeof(GeckoWebBrowser));
            Debug.Assert(browser != null, "browser is null");

            Locator.Locator = context.Resolve(Locator.Locator);

            Boolean succ = RequestHelper.OperateBrowserInput(browser, Locator, context.Resolve(InputValue));

            PhaseResult pr = new PhaseResult(this);
            pr.Succeed = succ;
            context.LastRequestContent = RequestHelper.GetGeckoContent(browser) ?? String.Empty;

            return pr;
        }
Esempio n. 3
0
        public override PhaseResult Run(Context context)
        {
            GeckoWebBrowser browser = (GeckoWebBrowser)context.GetService(typeof(GeckoWebBrowser));
            Debug.Assert(browser != null, "browser is null");

            String regex = context.Resolve(RegularExpression);
            Debug.Assert(String.IsNullOrWhiteSpace(regex), "regular expression is null!");

            String url = browser.Document.Uri;
            Match match = Regex.Match(url, regex);

            PhaseResult pr = new PhaseResult(this);

            if (match.Success)
            {
                // 表达式中所有的Group的id名
                List<String> ids = RegexHelper.ParseGroupIndexNames(regex);

                // 按照Group的名字写入到ParameterProvider
                foreach (String id in ids)
                {
                    context.ParameterProvider.SetString(id, match.Groups[id].Value);
                }

                pr.Succeed = true;
                pr.SetInt(Constant.RVCount, ids.Count);
            }
            else
            {
                pr.Succeed = false;
            }

            return pr;
        }
Esempio n. 4
0
        public override PhaseResult Run(Context context)
        {
            PhaseResult pr = new PhaseResult(this);

            GeckoWebBrowser browser = (GeckoWebBrowser)context.GetService(typeof(GeckoWebBrowser));
            Debug.Assert(browser != null, "browser is null");

            if (WaitMilliseconds > 0)
            {
                // 等待一定的毫秒数
                Thread.Sleep(WaitMilliseconds);
            }
            else
            {
                String urlRegex = context.Resolve(UrlRegex);
                String contentRegex = context.Resolve(ContentRegex);
                
                // TODO::加上Timeout!!!返回结果只有为Timeout的时候才算失败
                // 先等待Url的Pattern
                if (!String.IsNullOrWhiteSpace(urlRegex))
                {
                    while (browser.Document.Uri == null || !Regex.IsMatch(browser.Document.Uri, urlRegex))
                    {
                        Thread.Sleep(200);
                    }
                }

                // 再等待Content的Pattern
                if (!String.IsNullOrWhiteSpace(contentRegex))
                {
                    while (!Regex.IsMatch(RequestHelper.GetGeckoContent(browser), contentRegex))
                    {
                        Thread.Sleep(200);
                    }
                }
            }

            string content = RequestHelper.GetGeckoContent(browser);
            context.LastRequestContent = content ?? String.Empty;
            pr.SetString(Constant.RVHttpRequestResult, content);
            pr.Succeed = true;
            return pr;
        }
Esempio n. 5
0
        /// <summary>
        /// context里的RuntimeProvider应该已经设置好
        /// </summary>
        /// <returns></returns>
        private List<String> GenerateOneBatch(Context context)
        {
            Int32 from;
            Int32 to;
            Int32 step;

            List<String> result = new List<string>();

            if (!Int32.TryParse(From, out from))
            {
                throw new ArgumentException("From is corrupted.");
            }

            if (!Int32.TryParse(Step, out step))
            {
                throw new ArgumentException("Step is corrupted.");
            }

            // 如果To不是一个数字,则是一个正则表达式,先使用FirstPage进行请求html
            if (!Int32.TryParse(To, out to))
            {
                if (FirstPage == null)
                {
                    String msg = "When To is a regular expression, FirstPage is expected.";
                    throw new ArgumentException(msg);
                }
                PhaseResult html = FirstPage.Run(context);
                context.PushResult(html);

                Match match = Regex.Match(context.LastRequestContent, To);
                if (!match.Success || !Int32.TryParse(match.Groups[1].Value, out to))
                {
                    to = from;
                }
            }

            //String pattern = context.Resolve(Pattern);
            for (int i = from; i <= to; i += step)
            {
                Dictionary<String, String> dict = new Dictionary<string, string>();
                foreach (Pattern pattern in Patterns)
                {
                    String resolvedPattern = context.Resolve(pattern.RawPattern);
                    if (ParameterResolver.HasDataBinding(resolvedPattern, Constant.RuntimePrefix))
                    {
                        return null;
                    }
                    String gen = resolvedPattern.Replace(Constant.UpdatablePlaceHolder, i.ToString());
                    dict.Add(pattern.Name, gen);
                }
                result.Add(JsonConvert.SerializeObject(dict));
            }

            if (Save)
            {
                context.JsonResult.AddRange(result);
            }

            return result;
        }
Esempio n. 6
0
        private void Initialize(Context context)
        {
            _regex = context.Resolve(RegularExpression);
            _nested = context.Resolve(NestedRegularExpression);
            _baseXPath = context.Resolve(BaseXPath);

            if (XPaths != null && XPaths.Count > 0)
            {
                _xpaths = new Dictionary<string, string>();
                foreach (var pair in XPaths)
                {
                    _xpaths.Add(pair.Key, context.Resolve(pair.Value));
                }
            }

            if (Images != null && Images.Count > 0)
            {
                _images = new Dictionary<String, DomElementLocator>();
                foreach (var pair in Images)
                {
                    pair.Value.Locator = context.Resolve(pair.Value.Locator);
                    _images.Add(pair.Key, pair.Value);
                }
            }
        }