コード例 #1
0
        public TweetFilter(string config_file)
        {
            if (!System.IO.File.Exists(config_file))
            {
                ft = new FilterApp();
                return;
            }

            XmlDocument cfg = new XmlDocument();

            cfg.Load(config_file);
            XmlNode ft_xml = cfg.GetElementsByTagName("Filter")[0];

            if (ft_xml == null)
            {
                throw new Exception("Failed in config: not valid config");
            }

            ft     = parse_xml(ft_xml);
            loaded = true;
        }
コード例 #2
0
        private FilterApp parse_xml(XmlNode ft)
        {
            if (ft.Attributes?["operation"] != null)
            {
                FilterOp op;
                switch (ft.Attributes["operation"].Value)
                {
                case "or":
                    op = FilterOp.OP_OR;
                    break;

                case "and":
                    op = FilterOp.OP_AND;
                    break;

                case "xor":
                    op = FilterOp.OP_XOR;
                    break;

                default:
                    throw new Exception("Failed in config: no such operation");
                }

                if (ft.ChildNodes.Count < 2)
                {
                    throw new Exception("Failed in config: wrong parity");
                }
                else if (op == FilterOp.OP_XOR && ft.ChildNodes.Count > 2)
                {
                    throw new Exception("Failed in config: xor operation is binary operation");
                }
                else
                {
                    FilterApp f1 = parse_xml(ft.ChildNodes[0]);
                    FilterApp f2 = parse_xml(ft.ChildNodes[1]);
                    FilterApp f  = new FilterApp(f1, f2, op);

                    for (int i = 2; i < ft.ChildNodes.Count; i++)
                    {
                        f = new FilterApp(f, parse_xml(ft.ChildNodes[i]), op);
                    }

                    return(f);
                }
            }
            else
            {
                if (ft["Type"] == null || ft["Value"] == null)
                {
                    throw new Exception("Failed in config: terminal filter is not valid");
                }

                FilterApp f = null;

                if (ft["Type"].InnerText == "body")
                {
                    f = new FilterApp(new Filter(FilterType.VAL_BODY, ft["Value"].InnerText));
                }
                else if (ft["Type"].InnerText == "retweet")
                {
                    if (!int.TryParse(ft["Value"].InnerText, out int x))
                    {
                        throw new Exception("Failed in config: retweet filter must have integer threshold");
                    }
                    f = new FilterApp(new Filter(FilterType.VAL_RETWEET, x));
                }
                else if (ft["Type"].InnerText == "like")
                {
                    if (!int.TryParse(ft["Value"].InnerText, out int x))
                    {
                        throw new Exception("Failed in config: like filter must have integer threshold");
                    }
                    f = new FilterApp(new Filter(FilterType.VAL_LIKE, x));
                }

                return(f);
            }
        }
コード例 #3
0
 public FilterApp(FilterApp lft, FilterApp rft, FilterOp fop)
 {
     m_filter = new Tuple <FilterApp, FilterApp, FilterOp>(lft, rft, fop);
     multi    = true;
 }