Xml格式的模板解析完之后存放在一个树结构中
Ejemplo n.º 1
0
        public static PhaseWaitFor Create(TreeNode parameters, Boolean useBrowser = false)
        {
            if (parameters.Descends == null)
            {
                return null;
            }

            PhaseWaitFor waitFor = new PhaseWaitFor();
            if (parameters.Descends.ContainsKey(Constant.WaitMillisecondsNode))
            {
                Int32 waitMS;
                Int32.TryParse(parameters.Descends[Constant.WaitMillisecondsNode].Value, out waitMS);
                waitFor.WaitMilliseconds = waitMS;
            }

            if (parameters.Descends.ContainsKey(Constant.UrlRegexNode))
            {
                waitFor.UrlRegex = parameters.Descends[Constant.UrlRegexNode].Value;
            }

            if (parameters.Descends.ContainsKey(Constant.ContentRegexNode))
            {
                waitFor.ContentRegex = parameters.Descends[Constant.ContentRegexNode].Value;
            }

            return waitFor;
        }
Ejemplo n.º 2
0
        public static TreeNode Parse(XmlNode xml)
        {
            TreeNode node = new TreeNode();
            node.Name = xml.Name;

            if (xml.Attributes != null)
            {
                foreach (XmlAttribute attr in xml.Attributes)
                {
                    node.Attributes.Add(attr.Name, HttpUtility.HtmlDecode(attr.Value));
                }
            }

            Int32 elementChild = 0;
            if (xml.ChildNodes != null && xml.ChildNodes.Count > 0)
            {
                foreach (XmlNode child in xml.ChildNodes)
                {
                    if (child.NodeType == XmlNodeType.Element)
                    {
                        elementChild++;
                        node.Add(Parse(child));
                    }
                }
            }

            if (elementChild == 0)
            {
                //node.Value = HttpUtility.HtmlDecode(xml.InnerText).Trim();
                node.Value = xml.InnerText.Trim();
            }

            return node;
        }
Ejemplo n.º 3
0
        public static PhaseClick Create(TreeNode parameters, Boolean useBrowser = false)
        {
            if (!parameters.Descends.ContainsKey(Constant.TargetNode))
            {
                return null;
            }

            PhaseClick click = new PhaseClick();

            String target = parameters.Descends[Constant.TargetNode].Value;
            click.Locator = TemplateGenHelper.GenerateLocator(target);

            return click;
        }
Ejemplo n.º 4
0
        public static PhaseAccount Create(TreeNode parameters, Boolean userBrowser = false)
        {
            if (!parameters.Descends.ContainsKey(Constant.UsernameNode) ||
                !parameters.Descends.ContainsKey(Constant.PasswordNode))
            {
                return null;
            }

            PhaseAccount account = new PhaseAccount();

            account.Username = parameters.Descends[Constant.UsernameNode].Value;
            account.Password = parameters.Descends[Constant.PasswordNode].Value;

            return account;
        }
Ejemplo n.º 5
0
        public static PhaseDownload Create(TreeNode parameters, Boolean useBrowser = false)
        {
            if (!parameters.Attributes.ContainsKey(Constant.FileUrlTagAttr))
            {
                return null;
            }

            PhaseDownload pdf = new PhaseDownload();

            pdf.Binding = parameters.Attributes.ContainsKey(Constant.ListBindingAttr) ? parameters.Attributes[Constant.ListBindingAttr] : null;
            pdf.Extension = parameters.Attributes.ContainsKey(Constant.FileExtensionAttr) ? parameters.Attributes[Constant.FileExtensionAttr] : null;
            pdf.UrlTagName = parameters.Attributes[Constant.UrlTagAttr];
            pdf.Directory = parameters.Descends.ContainsKey(Constant.DirectoryNode) ? parameters.Descends[Constant.DirectoryNode].Value : null;

            return pdf;
        }
Ejemplo n.º 6
0
        public void Add(TreeNode node)
        {
            if (Descends == null)
            {
                Descends = new Dictionary<string, TreeNode>();
                ListDescends = new List<TreeNode>();
            }

            // Descends存放第一次出现,一般用来构建Phase的时候直接取用
            // ListDescends存放所有有序和重复出现的对象
            if (!Descends.ContainsKey(node.Name))
            {
                Descends.Add(node.Name, node);
            }

            ListDescends.Add(node);
        }
Ejemplo n.º 7
0
        public static PhaseInput Create(TreeNode parameters, Boolean useBrowser = false)
        {
            if (!parameters.Descends.ContainsKey(Constant.TargetNode) ||
                !parameters.Descends.ContainsKey(Constant.DataNode))
            {
                return null;
            }

            PhaseInput input = new PhaseInput();

            String target = parameters.Descends[Constant.TargetNode].Value;
            String data = parameters.Descends[Constant.DataNode].Value;
            input.Locator = TemplateGenHelper.GenerateLocator(target);
            input.InputValue = data;

            return input;
        }
Ejemplo n.º 8
0
        public static PhasePlainList Create(TreeNode parameters, Boolean useBrowser = false)
        {
            if (parameters.ListDescends == null || parameters.ListDescends.Count == 0)
            {
                return null;
            }

            PhasePlainList list = new PhasePlainList();

            list.List = new List<string>();

            list.List.AddRange(
                parameters.ListDescends
                .Where(o => !String.IsNullOrWhiteSpace(o.Value))
                .Select(o => o.Value));

            return list;
        }
Ejemplo n.º 9
0
        public static PhaseParseUrl Create(TreeNode parameters, Boolean useBrowser = false)
        {
            if (parameters.Descends == null)
            {
                return null;
            }

            try
            {
                PhaseParseUrl parseUrl = new PhaseParseUrl();
                parseUrl.RegularExpression = parameters.Descends[Constant.RegexNode].Value;
                return parseUrl;
            }
            catch (KeyNotFoundException e)
            {
                return null;
            }
        }
Ejemplo n.º 10
0
        public static PhaseLogout Create(TreeNode parameters, Boolean useBrowser = false)
        {
            if (parameters.Descends.Count == 0)
            {
                return null;
            }

            PhaseLogout login = new PhaseLogout();

            foreach (TreeNode node in parameters.Descends.Values)
            {
                IPhase phase = TemplateGenHelper.GeneratePhaseFromTreeNode(node, useBrowser);
                if (phase == null)
                {
                    return null;
                }
                login._logoutPhases.Add(phase);
            }

            return login;
        }
Ejemplo n.º 11
0
        public static IPhase GeneratePhaseFromTreeNode(TreeNode node, Boolean useBrowser = false)
        {
            if (node == null)
            {
                return null;
            }

            // 使用Reflection调用对应的PhaseXXX类的Build方法来创建
            String phaseTypeName = String.Format(
                    useBrowser ? _browserTypeFormat : _plainTypeFormat, node.Name);
            Type phaseType = Type.GetType(phaseTypeName);
            if (phaseType == null)
            {
                phaseType = Type.GetType(String.Format(_browserTypeFormat, node.Name));
            }

            if (phaseType == null)
            {
                return null;
            }

            MethodInfo creator = phaseType.GetMethod(Constant.PhaseCreatorMethod);
            return (IPhase)creator.Invoke(null, new object[] { node, useBrowser });
        }
Ejemplo n.º 12
0
        public static PhaseParseFile Create(TreeNode parameters, Boolean useBrowser = false)
        {
            if (!parameters.Attributes.ContainsKey(Constant.FileUrlTagAttr) ||
                !parameters.Attributes.ContainsKey(Constant.DocumentTypeAttr))
            {
                return null;
            }

            PhaseParseFile file = new PhaseParseFile();

            file.Save = parameters.Attributes.ContainsKey(Constant.SaveAttr) ? Constant.True(parameters.Attributes[Constant.SaveAttr]) : false;
            file.Binding = parameters.Attributes.ContainsKey(Constant.ListBindingAttr) ? parameters.Attributes[Constant.ListBindingAttr] : null;
            file.ListID = parameters.Attributes.ContainsKey(Constant.ListIDAttr) ? parameters.Attributes[Constant.ListIDAttr] : null;
            file.FileUrlTagName = parameters.Attributes[Constant.FileUrlTagAttr];
            file.Type = ParseDocumentType(parameters.Attributes[Constant.DocumentTypeAttr]);
            file.Directory = parameters.Descends.ContainsKey(Constant.DirectoryNode) ? parameters.Descends[Constant.DirectoryNode].Value : null;

            return file;
        }
Ejemplo n.º 13
0
        public static PhaseMakeList Create(TreeNode parameters, Boolean useBrowser = false)
        {
            if (!parameters.Descends.ContainsKey(Constant.PatternNode))
            {
                throw new ArgumentException("Pattern node is expected for MakeList phase");
            }

            PhaseMakeList list = new PhaseMakeList();

            list.Binding = parameters.Attributes.ContainsKey(Constant.ListBindingAttr) ? parameters.Attributes[Constant.ListBindingAttr] : null;
            list.ListID = parameters.Attributes.ContainsKey(Constant.ListIDAttr) ? parameters.Attributes[Constant.ListIDAttr] : null;
            list.From = parameters.Attributes.ContainsKey(Constant.FromAttr) ? parameters.Attributes[Constant.FromAttr] : "1";
            list.To = parameters.Attributes.ContainsKey(Constant.ToAttr) ? parameters.Attributes[Constant.ToAttr] : "1";
            list.Step = parameters.Attributes.ContainsKey(Constant.StepAttr) ? parameters.Attributes[Constant.StepAttr] : "1";
            list.Hook = parameters.Attributes.ContainsKey(Constant.HookAttr) ? Constant.True(parameters.Attributes[Constant.HookAttr]) : false;
            list.Save = parameters.Attributes.ContainsKey(Constant.SaveAttr) ? Constant.True(parameters.Attributes[Constant.SaveAttr]) : false;

            //list.Pattern = parameters.Descends[Constant.PatternNode].Value;
            // 初始化Pattern
            list.Patterns = new List<Pattern>();
            foreach (var patternNode in parameters.ListDescends.Where(o => o.Name == Constant.PatternNode))
            {
                if (patternNode.Attributes.ContainsKey(Constant.NameAttr) && !String.IsNullOrWhiteSpace(patternNode.Value))
                {
                    list.Patterns.Add(new Pattern
                    {
                        Name = patternNode.Attributes[Constant.NameAttr],
                        RawPattern = patternNode.Value,
                    });
                }
            }
            
            if (parameters.Descends.ContainsKey(Constant.FirstPageNode))
            {
                list.FirstPage = TemplateGenHelper.GeneratePhaseFromTreeNode(parameters.Descends[Constant.FirstPageNode].Descends.FirstOrDefault().Value, useBrowser);
            }

            return list;
        }
Ejemplo n.º 14
0
        public static PhaseParseContent Create(TreeNode parameters, Boolean useBrowser = false)
        {
            if (parameters.Descends == null)
            {
                return null;
            }

            try
            {
                PhaseParseContent parseContent = new PhaseParseContent();

                parseContent.Save = false;
                if (parameters.Attributes.ContainsKey(Constant.SaveAttr))
                {
                    String save = parameters.Attributes[Constant.SaveAttr];
                    parseContent.Save = Constant.True(save);
                }

                parseContent.ListID = parameters.Attributes.ContainsKey(Constant.ListIDAttr) ? parameters.Attributes[Constant.ListIDAttr] : null;

                if (parameters.Descends.ContainsKey(Constant.NestedRegexNode))
                {
                    parseContent.NestedRegularExpression = parameters.Descends[Constant.NestedRegexNode].Value;
                }

                if (parameters.Descends.ContainsKey(Constant.RegexNode))
                {
                    parseContent.RegularExpression = parameters.Descends[Constant.RegexNode].Value;
                }
                else
                {
                    parseContent.BaseXPath = parameters.Descends[Constant.BaseXPathNode].Value;
                    TreeNode xpaths = parameters.Descends[Constant.XPathNode];
                    if (xpaths == null)
                    {
                        return null;
                    }
                    else
                    {
                        parseContent.XPaths = new Dictionary<string, string>();
                        foreach (var pair in xpaths.Descends)
                        {
                            parseContent.XPaths.Add(pair.Key, pair.Value.Value);
                        }
                    }
                }

                return parseContent;
            }
            catch (KeyNotFoundException e)
            {
                return null;
            }
        }
Ejemplo n.º 15
0
        public static PhaseSimpleScan Create(TreeNode parameters, Boolean useBrowser = false)
        {
            if (!parameters.Descends.ContainsKey(Constant.RequestNode) ||
                !parameters.Descends.ContainsKey(Constant.ParseNode))
            {
                throw new ArgumentException("Request and Parse nodes are both expected for Scan phase.");
            }

            PhaseSimpleScan scan = new PhaseSimpleScan();

            scan.Binding = parameters.Attributes.ContainsKey(Constant.ListBindingAttr) ? parameters.Attributes[Constant.ListBindingAttr] : null;
            scan.ListID = parameters.Attributes.ContainsKey(Constant.ListIDAttr) ? parameters.Attributes[Constant.ListIDAttr] : null;
            scan.Request = TemplateGenHelper.GeneratePhaseFromTreeNode(parameters.Descends[Constant.RequestNode].Descends.FirstOrDefault().Value);
            scan.Parse = TemplateGenHelper.GeneratePhaseFromTreeNode(parameters.Descends[Constant.ParseNode].Descends.FirstOrDefault().Value);

            return scan;
        }
Ejemplo n.º 16
0
        //TODO:把所有参数的解析都延迟到运行时,比如这里的From和To。
        public static PhaseScan Create(TreeNode parameters, Boolean useBrowser = false)
        {
            // RequestNode和ListNode至少要有一个
            if (!(parameters.Descends.ContainsKey(Constant.RequestNode) ||
                parameters.Descends.ContainsKey(Constant.ListNode)) ||
                !parameters.Descends.ContainsKey(Constant.ParseNode))
            {
                return null;
            }

            PhaseScan scan = new PhaseScan();

            // 只有To是可以留到Phase创建之后才解析的
            scan.From = parameters.Attributes.ContainsKey(Constant.FromAttr) ? parameters.Attributes[Constant.FromAttr] : "1";
            scan.To = parameters.Attributes.ContainsKey(Constant.ToAttr) ? parameters.Attributes[Constant.ToAttr] : "1";
            scan.Step = parameters.Attributes.ContainsKey(Constant.StepAttr) ? parameters.Attributes[Constant.StepAttr] : "1";

            Int32 integer;
            
            if (!Int32.TryParse(scan.From, out integer))
            {
                return null;
            }
            scan._from = integer;

            if (!Int32.TryParse(scan.Step, out integer))
            {
                return null;
            }
            scan._step = integer;

            IPhase request = null;
            if (parameters.Descends.ContainsKey(Constant.RequestNode))
            {
                request = TemplateGenHelper.GeneratePhaseFromTreeNode(parameters.Descends[Constant.RequestNode].Descends.FirstOrDefault().Value, useBrowser);
            }

            IPhase list = null;
            if (parameters.Descends.ContainsKey(Constant.ListNode))
            {
                TreeNode listNode = parameters.Descends[Constant.ListNode];
                list = TemplateGenHelper.GeneratePhaseFromTreeNode(listNode.Descends.FirstOrDefault().Value, useBrowser);
                scan.MetaListUrlKey = "Url";
                if (listNode.Attributes.ContainsKey(Constant.UrlAttr))
                {
                    scan.MetaListUrlKey = listNode.Attributes[Constant.UrlAttr];
                }
            }

            IPhase parse = TemplateGenHelper.GeneratePhaseFromTreeNode(parameters.Descends[Constant.ParseNode].Descends.FirstOrDefault().Value, useBrowser);

            if (request as IParameterUpdatable<Int32> == null && request as PhaseClick == null && list == null)
            {
                return null;
            }

            scan._updatableRequest = request as IParameterUpdatable<Int32>;

            scan.Request = request;
            scan.Parse = parse;
            scan.MetaList = list;

            return scan;
        }