public static void Test1()
        {
            string HtmlCode = "<html><head></head><body><div><a id='abcd'>Test</a><p id='defgh'><a>test2<a/>test3</p><a>1</a><a>2</a><a>3</a><a>4</a></div></body></html>";

            HTMLInterface TestInterface = new HTMLInterface(HtmlCode);
            TestInterface.GetFirstObject();

            List<ObjectAttribute> RepresentAAttributes1 = new List<ObjectAttribute>();
            RepresentAAttributes1.Add(new ObjectAttributeString("name", "A"));
            RepresentAAttributes1.Add(new ObjectAttributeRegex("id", "[a-z]+"));
            RepresentAAttributes1.Add(new ObjectAttributeRegex("innerText", "[A-Z]+"));

            List<ObjectAttribute> RepresentAAttributes2 = new List<ObjectAttribute>();
            RepresentAAttributes2.Add(new ObjectAttributeString("name", "A"));
            RepresentAAttributes2.Add(new ObjectAttributeRegex("id", "[a-z]+"));
            RepresentAAttributes2.Add(new ObjectAttributeRegex("innerText", "Test"));

            List<ObjectAttribute> RepresentAAttributes3 = new List<ObjectAttribute>();
            RepresentAAttributes3.Add(new ObjectAttributeString("name", "A"));
            RepresentAAttributes3.Add(new ObjectAttributeRegex("innerText", "test2"));

            List<ObjectAttribute> RepresentAAttributes4 = new List<ObjectAttribute>();
            RepresentAAttributes4.Add(new ObjectAttributeString("name", "A"));
            RepresentAAttributes4.Add(new ObjectAttributeRegex("innerText", "\\d+"));

            List<ObjectAttribute> RepresentPAttributes = new List<ObjectAttribute>();
            RepresentPAttributes.Add(new ObjectAttributeString("name", "P"));
            RepresentPAttributes.Add(new ObjectAttributeRegex("id", "def[a-z]+"));

            ParserObject RepresentA1 = new ParserObject(RepresentAAttributes1);
            ParserObject RepresentA2 = new ParserObject(RepresentAAttributes2);
            ParserObject RepresentA3 = new ParserObject(RepresentAAttributes3);
            ParserObject RepresentA4 = new ParserObject(RepresentAAttributes4);
            ParserObject RepresentP = new ParserObject(RepresentPAttributes);
            RepresentP.AddSubParserPrimitive(RepresentA3);

            List<ObjectAttribute> RepresentDIVAttributes = new List<ObjectAttribute>();
            RepresentDIVAttributes.Add(new ObjectAttributeString("name", "DIV"));

            ParserObject RepresentDIV = new ParserObject(RepresentDIVAttributes);

            ParserObjectRelation RelationAND = new ParserObjectRelation();
            RelationAND.SetRelationFunction(new RelationFunctionAND(ref RelationAND, null));
            RelationAND.AddSubParserPrimitive(RepresentA1);
            RelationAND.AddSubParserPrimitive(RepresentA2);

            ParserObjectRelation RelationOR = new ParserObjectRelation();
            RelationOR.SetRelationFunction(new RelationFunctionOR(ref RelationOR, null));
            RelationOR.AddSubParserPrimitive(RelationAND);
            RelationOR.AddSubParserPrimitive(RepresentP);

            ParserObjectRelation RelationMORE = new ParserObjectRelation();
            RelationMORE.SetRelationFunction(new RelationFunctionMORE(ref RelationMORE, null));
            RelationMORE.AddSubParserPrimitive(RepresentA4);

            RepresentDIV.AddSubParserPrimitive(RelationOR);
            RepresentDIV.AddSubParserPrimitive(RepresentP);
            RepresentDIV.AddSubParserPrimitive(RelationMORE);

            ObjectInterfacePrototipe tInterface = (ObjectInterfacePrototipe)TestInterface;
            FunctionIO Result = RepresentDIV.CheckRelations(ref tInterface, null, true);
            Result.GetType();
        }
        public static void Test2()
        {
            string HtmlCode = "<html><head></head><body><div><div>a</div><div>b</div><div>c</div><p>a</p><p>b</p><p>c</p><a>test</a></div></body></html>";

            HTMLInterface TestInterface = new HTMLInterface(HtmlCode);
            TestInterface.GetFirstObject();

            List<ObjectAttribute> RepresentAAttributes1 = new List<ObjectAttribute>();
            RepresentAAttributes1.Add(new ObjectAttributeRegex("name", ".+"));
            //RepresentAAttributes1.Add(new ObjectAttributeRegex("innerText", "[a-z]"));
            ParserObject RepresentA1 = new ParserObject(RepresentAAttributes1);

            List<ObjectAttribute> RepresentPAttributes = new List<ObjectAttribute>();
            RepresentPAttributes.Add(new ObjectAttributeRegex("name", "P"));
            RepresentPAttributes.Add(new ObjectAttributeRegex("innerText", "[a-z]"));
            ParserObject RepresentP = new ParserObject(RepresentPAttributes);

            List<ObjectAttribute> RepresentAAttributes2 = new List<ObjectAttribute>();
            RepresentAAttributes2.Add(new ObjectAttributeString("name", "A"));
            RepresentAAttributes2.Add(new ObjectAttributeRegex("innerText", "[a-z]+"));
            ParserObject RepresentA2 = new ParserObject(RepresentAAttributes2);

            ObjectAttributeEvaluation evaluation = new ObjectAttributeEvaluation(EvaluationMode.Max, 1, 4);
            evaluation.AttributeName="ParseCount";
            FunctionIO RelationMOREInput = new FunctionIO();
            RelationMOREInput.AddFunctionParameter(evaluation);

            TestCallback CallbackClass = new TestCallback();

            ParserObjectRelation RelationMORE = new ParserObjectRelation();
            RelationMORE.SetRelationFunction(new RelationFunctionMORE(ref RelationMORE, RelationMOREInput));
            RelationMORE.AddSubParserPrimitive(RepresentA1);
            RelationMORE.SetCallback(CallbackClass.Call);

            ParserObjectRelation RelationMORE2 = new ParserObjectRelation();
            RelationMORE2.SetRelationFunction(new RelationFunctionMORE(ref RelationMORE2, RelationMOREInput));
            RelationMORE2.AddSubParserPrimitive(RepresentP);
            RelationMORE2.SetCallback(CallbackClass.Call);

            List<ObjectAttribute> RepresentDIVAttributes = new List<ObjectAttribute>();
            RepresentDIVAttributes.Add(new ObjectAttributeString("name", "DIV"));

            ParserObject RepresentDIV = new ParserObject(RepresentDIVAttributes);
            RepresentDIV.AddSubParserPrimitive(RelationMORE);
            RepresentDIV.AddSubParserPrimitive(RelationMORE2);
            RepresentDIV.AddSubParserPrimitive(RepresentA2);

            ObjectInterfacePrototipe tInterface = (ObjectInterfacePrototipe)TestInterface;
            FunctionIO Result = RepresentDIV.CheckRelations(ref tInterface, null, true);
            Result.GetType();
        }
Пример #3
0
        private ParserPrimitives ParseNode( XmlNode ElementToParse )
        {
            XmlNodeType NodeType= ElementToParse.NodeType;
            if (NodeType != XmlNodeType.Element)
                return null;

            ParserPrimitives ParserPrimitive = null;

            if (ElementToParse.Name == "object")
            {
                ParserPrimitive = new ParserObject();
                List<ObjectAttribute> Attributes= new List<ObjectAttribute>();

                foreach( XmlAttribute Attribute in ElementToParse.Attributes )
                {
                    if (Attribute.Name == "callback")
                    {
                        ParserPrimitive.SetCallback(cCallbackStore.GetCallbackEntryByName(Attribute.Value).GetCallbackFunction());
                        continue;
                    }
                    Attributes.Add(new ObjectAttributeRegex(Attribute.Name, Attribute.Value));
                }

                ((ParserObject)ParserPrimitive).SetObjectAttributes(Attributes);
            }
            else if (ElementToParse.Name == "relation")
            {
                ParserPrimitive = new ParserObjectRelation();
                string RelationFunctionName="";
                int min_evaluation = 0, max_evaluation = 0;
                EvaluationMode evaluation_mode= EvaluationMode.Max;
                FunctionIO FunctionInput = new FunctionIO();
                foreach (XmlAttribute Attribute in ElementToParse.Attributes)
                {
                    if (Attribute.Name == "callback")
                    {
                        ParserPrimitive.SetCallback(cCallbackStore.GetCallbackEntryByName(Attribute.Value).GetCallbackFunction());
                        continue;
                    }
                    else if (Attribute.Name == "relation_function")
                    {
                        RelationFunctionName = Attribute.Value;
                        continue;
                    }
                    else if (Attribute.Name == "evaluation_min")
                    {
                        min_evaluation = System.Int32.Parse(Attribute.Value);
                        continue;
                    }
                    else if (Attribute.Name == "evaluation_max")
                    {
                        min_evaluation = System.Int32.Parse(Attribute.Value);
                        continue;
                    }
                    else if (Attribute.Name == "evaluation_mode")
                    {
                        switch (Attribute.Value)
                        {
                            case "min":
                                evaluation_mode = EvaluationMode.Min;
                                break;
                            case "max":
                                evaluation_mode = EvaluationMode.Max;
                                break;
                        }
                        continue;
                    }

                    FunctionInput.AddFunctionParameter( new ObjectAttributeString(Attribute.Name, Attribute.Value) );
                }

                ObjectAttributeEvaluation evaluation= new ObjectAttributeEvaluation( evaluation_mode, min_evaluation, max_evaluation );
                FunctionInput.AddFunctionParameter( evaluation );
                RelationFunction tRelationFunction= null;
                ParserObjectRelation Relation = (ParserObjectRelation)ParserPrimitive;
                switch( RelationFunctionName )
                {
                    case "more":
                        tRelationFunction = new RelationFunctionMORE(ref Relation, FunctionInput);
                        break;
                    case "and":
                        tRelationFunction = new RelationFunctionAND(ref Relation, FunctionInput);
                        break;
                    case "or":
                        tRelationFunction = new RelationFunctionOR(ref Relation, FunctionInput);
                        break;
                }

                Relation.SetRelationFunction(tRelationFunction);
            }

            return ParserPrimitive;
        }
 /// <summary>
 /// Relation function constructor.
 /// </summary>
 /// <param name="lParserObjectRelation">Parent ObjectRelation.</param>
 /// <param name="lFunctionParameters">Input parameters for relation function.</param>
 public RelationFunctionAND(ref ParserObjectRelation lParserObjectRelation, FunctionIO lFunctionParameters)
     : base(ref lParserObjectRelation, lFunctionParameters)
 {
 }
        public RelationFunction CreateRelationFunctionByName( string RelationFunctionName, ParserObjectRelation lParserObjectRelation, FunctionIO lFunctionInput )
        {
            if (cFunctionEntries[RelationFunctionName] != null)
            {
                //Creates a new instance of specifficed class.
                return (RelationFunction)Activator.CreateInstance(cFunctionEntries[RelationFunctionName].AssociatedType, new object[] { lParserObjectRelation, lFunctionInput });
            }

            return null;
        }
 public RelationFunction(ref ParserObjectRelation lParserObjectRelation, FunctionIO lFunctionParameters)
 {
     cParserObjectRelation = lParserObjectRelation;
     cFunctionParameters = lFunctionParameters;
 }