Esempio n. 1
0
        private void AddNewItem(bool isAlert = true)
        {
            var path     = SelectXPath;
            var rootPath = RootXPath;

            if (!string.IsNullOrEmpty(rootPath))
            {
                //TODO: 当XPath路径错误时,需要捕获异常
                HtmlNode root = null;
                try
                {
                    root = HtmlDoc.DocumentNode.SelectSingleNodePlus(rootPath, RootFormat);
                }
                catch (Exception)
                {
                    XLogSys.Print.Error(string.Format(GlobalHelper.Get("key_662"), RootXPath, RootFormat));
                }
                if (!(root != null).SafeCheck(string.Format(GlobalHelper.Get("key_663"), RootFormat, RootXPath)))
                {
                    return;
                }
                root = HtmlDoc.DocumentNode.SelectSingleNodePlus(rootPath, RootFormat)?.ParentNode;

                HtmlNode node = null;
                if (
                    !ControlExtended.SafeInvoke(() => HtmlDoc.DocumentNode.SelectSingleNodePlus(path, SearchFormat),
                                                ref node,
                                                LogType.Info, GlobalHelper.Get("key_664"), true))

                {
                    return;
                }
                if (!(node != null).SafeCheck(GlobalHelper.Get("key_665")))
                {
                    return;
                }

                if (!node.IsAncestor(root) && isAlert)
                {
                    if (
                        MessageBox.Show(GlobalHelper.Get("key_666"), GlobalHelper.Get("key_99"), MessageBoxButton.YesNo) ==
                        MessageBoxResult.No)
                    {
                        return;
                    }
                }
                string attr      = "";
                string attrValue = "";
                XPathAnalyzer.GetAttribute(path, out attr, out attrValue);
                if (SearchFormat == SelectorFormat.XPath)
                {
                    path = XPath.TakeOffPlus(node.XPath, root.XPath);
                    if (attr != "")
                    {
                        path += "/@" + attr + "[1]";
                    }
                }
            }
            if (CrawlItems.FirstOrDefault(d => d.Name == SelectName) == null ||
                MessageBox.Show(GlobalHelper.Get("add_column_sure"), GlobalHelper.Get("key_99"), MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                var item = new CrawlItem {
                    XPath = path, Name = SelectName, SampleData1 = SelectText
                };
                item.Format = SearchFormat;
                CrawlItems.Add(item);

                SelectXPath = "";
                SelectName  = "";

                XLogSys.Print.Info(GlobalHelper.Get("key_668"));
            }
        }
Esempio n. 2
0
        private void AddNewItem(bool isAlert = true)
        {
            var path     = SelectXPath;
            var rootPath = RootXPath;

            if (!string.IsNullOrEmpty(rootPath))
            {
                //TODO: 当XPath路径错误时,需要捕获异常
                HtmlNode root = null;
                try
                {
                    root = HtmlDoc.DocumentNode.SelectSingleNodePlus(rootPath, RootFormat);
                }
                catch (Exception ex)
                {
                    XLogSys.Print.Error($"{RootXPath}  不能被识别为正确的{RootFormat}表达式,请检查");
                }
                if (!(root != null).SafeCheck($"使用当前父节点{RootFormat} {RootXPath},在文档中找不到任何父节点"))
                {
                    return;
                }
                root = HtmlDoc.DocumentNode.SelectSingleNodePlus(rootPath, RootFormat)?.ParentNode;

                HtmlNode node = null;
                if (
                    !ControlExtended.SafeInvoke(() => HtmlDoc.DocumentNode.SelectSingleNodePlus(path, SearchFormat),
                                                ref node,
                                                LogType.Info, "检查子节点XPath正确性", true))

                {
                    return;
                }
                if (!(node != null).SafeCheck("使用当前子节点XPath,在文档中找不到任何子节点"))
                {
                    return;
                }

                if (!node.IsAncestor(root) && isAlert)
                {
                    if (
                        MessageBox.Show("当前XPath所在节点不是父节点的后代,请检查对应的XPath,是否依然要添加?", "提示信息", MessageBoxButton.YesNo) ==
                        MessageBoxResult.No)
                    {
                        return;
                    }
                }
                string attr      = "";
                string attrValue = "";
                XPathAnalyzer.GetAttribute(path, out attr, out attrValue);
                if (SearchFormat == SelectorFormat.XPath)
                {
                    path = XPath.TakeOffPlus(node.XPath, root.XPath);
                    if (attr != "")
                    {
                        path += "/@" + attr + "[1]";
                    }
                }
            }
            if (CrawlItems.FirstOrDefault(d => d.Name == SelectName) == null ||
                MessageBox.Show("已经存在同名的属性,是否依然添加?", "提示信息", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                var item = new CrawlItem {
                    XPath = path, Name = SelectName, SampleData1 = SelectText
                };
                item.Format = SearchFormat;
                CrawlItems.Add(item);

                SelectXPath = "";
                SelectName  = "";

                XLogSys.Print.Info("成功添加属性");
            }
        }