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();
        }
        /// <summary>
        /// Recursively check all depths.
        /// </summary>
        /// <param name="lObjectInterface">ObjectInterface.</param>
        /// <param name="CallCallback">Do we have to call callback?</param>
        /// <param name="depth">Current depth.</param>
        /// <returns>FunctionIO with return parameters like ret value and parse count.</returns>
        public FunctionIO CheckDeepRelations(ref ObjectInterfacePrototipe lObjectInterface, bool CallCallback, int depth)
        {
            ObjectAttributeEvaluation Evaluation = new ObjectAttributeEvaluation();
            Evaluation.AttributeName = "ParseCount";

            if (depth > MaxDepth)
                return new FunctionIO(RetVal.False);

            bool More = true;
            int TotalMove = 0;
            int OccurenciesFound = 0;
            int StartOffset = lObjectInterface.GetOffset();
            bool eof = false;

            List<ParserPrimitives> SubPrimitives = cParserObjectRelation.GetSubParserPrimitives();
            while (More)
            {
                int CurrentMove = 0;
                int Positives = 0;

                ObjectInterfacePrototipe tInterface = lObjectInterface.GetSubObjects();
                FunctionIO ret2 = null;
                if (tInterface != null)
                {
                    tInterface.GetFirstObject();
                    if (tInterface.GetCurrentObject() != null)
                    {
                        ret2 = CheckDeepRelations(ref tInterface, CallCallback, depth + 1);
                        if (ret2["RetVal"].Value)
                            OccurenciesFound += ret2["Occurencies"].Value;
                    }
                }

                for (int x = 0; x < SubPrimitives.Count; x++)
                {
                    CurrentMove = 0;

                    FunctionIO ret = SubPrimitives[x].CheckRelations(ref lObjectInterface, x + 1 == SubPrimitives.Count ? null : SubPrimitives[x + 1], false);
                    if (ret["RetVal"].Value)
                    {
                        CurrentMove = ((ObjectAttributeEvaluation)ret["ParseCount"]).Value;
                        Positives++;
                    }
                    else
                        break;//Stop execution if we fail.
                    eof = lObjectInterface.MoveByOffset(CurrentMove);

                    if (eof)
                        break;
                }

                //if (eof)
                //    break;

                lObjectInterface.SetOffset(StartOffset);//We go back to start offset
                eof = lObjectInterface.MoveByOffset(TotalMove);//and move for one step and continue checking from there
                if (Positives == SubPrimitives.Count)
                {
                    OccurenciesFound++;//We find a new occurency and incrise counter
                    //We recall our callbacks
                    if (CallCallback)
                    {
                        for (int x = 0; x < SubPrimitives.Count; x++)
                        {
                            CurrentMove = 0;

                            FunctionIO ret = SubPrimitives[x].CheckRelations(ref lObjectInterface, x + 1 == SubPrimitives.Count ? null : SubPrimitives[x + 1], CallCallback);
                            CurrentMove = ((ObjectAttributeEvaluation)ret["ParseCount"]).Value;
                            eof = lObjectInterface.MoveByOffset(CurrentMove);
                        }
                    }
                }

                TotalMove++;//We incrise move
                lObjectInterface.SetOffset(StartOffset);//We go back to start offset
                eof = lObjectInterface.MoveByOffset(TotalMove);//and move for one step and continue checking from there

                if (eof)
                    break;
            }

            if (OccurenciesFound == 0)
                return new FunctionIO(RetVal.False);

            Evaluation.Evaluate(TotalMove);

            lObjectInterface.SetOffset(StartOffset);

            ObjectAttributeInt ParamOccurrenciesFound = new ObjectAttributeInt("Occurencies", OccurenciesFound);

            return new FunctionIO(ParamOccurrenciesFound, Evaluation, RetVal.False);
        }
예제 #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;
        }
        public override FunctionIO CheckRelations(ref ObjectInterfacePrototipe lObjectInterface, ParserPrimitives NextElement, bool CallCalback)
        {
            FunctionIO ReturnContainer = new FunctionIO();
            ObjectAttributeInt ReturnValue1 = new ObjectAttributeInt("ret", 1);
            ObjectAttributeInt ReturnValue0 = new ObjectAttributeInt("ret", 0);
            ObjectAttributeEvaluation Evaluation = null;
            if (cFunctionParameters == null || cFunctionParameters.GetFunctionParameterByName("ParseCount") == null)
            {
                Evaluation = new ObjectAttributeEvaluation();
                Evaluation.SetAttributeName("ParseCount");
            }
            else
                Evaluation = (ObjectAttributeEvaluation)cFunctionParameters.GetFunctionParameterByName("ParseCount");

            bool More = true;
            int TotalMove= 0;
            int StartOffset = lObjectInterface.GetOffset();
            bool eof = false;

            while (More)
            {
                int Positives = 0;
                int CurrentMove = 0;

                if (NextElement != null)
                {
                    FunctionIO ret = NextElement.CheckRelations(ref lObjectInterface, NextElement.GetNextPrimitive(), false);
                    if (((ObjectAttributeInt)ret.GetFunctionParameterByName("ret")).GetAttributeValue() == 1)
                    {
                        break;
                    }
                }

                List<ParserPrimitives> SubPrimitives = cParserObjectRelation.GetSubParserPrimitives();
                for (int x = 0; x < SubPrimitives.Count; x++)
                {
                    ParserPrimitives tPrimitives;
                    if (SubPrimitives.Count == x + 1)
                        tPrimitives = null;
                    else
                        tPrimitives = SubPrimitives[x + 1];
                    FunctionIO ret = SubPrimitives[x].CheckRelations(ref lObjectInterface, tPrimitives, CallCalback);
                    if (((ObjectAttributeInt)ret.GetFunctionParameterByName("ret")).GetAttributeValue() == 1)
                    {
                        CurrentMove += ((ObjectAttributeEvaluation)ret.GetFunctionParameterByName("ParseCount")).GetEvaluationValue();
                        Positives++;
                    }
                    eof=lObjectInterface.MoveByOffset(CurrentMove);
                }

                TotalMove += CurrentMove;

                if (Positives != SubPrimitives.Count || eof)
                {
                    More = false;
                }
            }

            if (!Evaluation.EvaluateNext(TotalMove))
            {
                ReturnContainer.AddFunctionParameter((ObjectAttribute)ReturnValue0);
                return ReturnContainer;
            }

            lObjectInterface.SetOffset(StartOffset);

            ReturnContainer.AddFunctionParameter(Evaluation);
            ReturnContainer.AddFunctionParameter((ObjectAttribute)ReturnValue1);
            return ReturnContainer;
        }
        public override FunctionIO CheckRelations(ref ObjectInterfacePrototipe lObjectInterface, ParserPrimitives NextElement, bool CallCalback)
        {
            FunctionIO ReturnContainer = new FunctionIO();
            ObjectAttributeInt ReturnValue1 = new ObjectAttributeInt("ret", 1);
            ObjectAttributeInt ReturnValue0 = new ObjectAttributeInt("ret", 0);
            ObjectAttributeEvaluation Evaluation= null;
            if (cFunctionParameters == null || cFunctionParameters.GetFunctionParameterByName("ParseCount") == null)
            {
                Evaluation = new ObjectAttributeEvaluation();
                Evaluation.SetAttributeName("ParseCount");
            }
            else
                Evaluation = (ObjectAttributeEvaluation)cFunctionParameters.GetFunctionParameterByName("ParseCount");

            bool result = false;
            List<ParserPrimitives> SubPrimitives = cParserObjectRelation.GetSubParserPrimitives();
            for (int x = 0; x < SubPrimitives.Count; x++)
            {
                ParserPrimitives tPrimitives;
                if (SubPrimitives.Count == x + 1)
                    tPrimitives = null;
                else
                    tPrimitives = SubPrimitives[x + 1];
                FunctionIO ret = SubPrimitives[x].CheckRelations(ref lObjectInterface, tPrimitives,CallCalback);
                if (((ObjectAttributeInt)ret.GetFunctionParameterByName("ret")).GetAttributeValue() == 1)
                {
                    result = true;
                    Evaluation.SubEvaluate((ObjectAttributeEvaluation)ret.GetFunctionParameterByName("ParseCount"));
                }
            }

            if (result)
            {
                ReturnContainer.AddFunctionParameter((ObjectAttribute)Evaluation);
                ReturnContainer.AddFunctionParameter((ObjectAttribute)ReturnValue1);
                return ReturnContainer;
            }

            ReturnContainer.AddFunctionParameter((ObjectAttribute)ReturnValue0);
            return ReturnContainer;
        }
        public override FunctionIO CheckRelations(ref ObjectInterfacePrototipe lObjectInterface, ParserPrimitives NextElement, bool CallCalback)
        {
            FunctionIO ReturnContainer= new FunctionIO();
            ObjectAttributeInt ReturnValue1 = new ObjectAttributeInt("ret",1);
            ObjectAttributeInt ReturnValue0 = new ObjectAttributeInt("ret",0);
            ObjectAttributeEvaluation Evaluation =  new ObjectAttributeEvaluation();
            Evaluation.SetAttributeName("ParseCount");

            if (!CheckCurrentObject(ref lObjectInterface))
            {
                ReturnContainer.AddFunctionParameter((ObjectAttribute)ReturnValue0);
                return ReturnContainer;
            }

            if (cParserPrimitives.Count == 0)
            {
                if (cCallback != null && CallCalback)
                    cCallback(lObjectInterface);

                ReturnContainer.AddFunctionParameter((ObjectAttribute)Evaluation);
                ReturnContainer.AddFunctionParameter((ObjectAttribute)ReturnValue1);
                return ReturnContainer;
            }

            //Here we preset next primitives
            for (int x = 0; x < cParserPrimitives.Count-1; x++)
            {
                cParserPrimitives[x].SetNextPrimitive(cParserPrimitives[x + 1]);
            }

            ObjectInterfacePrototipe SubObjects= lObjectInterface.GetSubObjects();
            SubObjects.GetFirstObject();
            ObjectAttributeEvaluation ParseCountEvaluation = new ObjectAttributeEvaluation( EvaluationMode.Max, 1, 100000000);
            for( int x=0; x< cParserPrimitives.Count; x++ )
            {
                ParserPrimitives tPrimitives;
                if (cParserPrimitives.Count == x + 1)
                    tPrimitives = null;
                else
                    tPrimitives = cParserPrimitives[x + 1];
                FunctionIO ret = cParserPrimitives[x].CheckRelations(ref SubObjects, tPrimitives, CallCalback);
                if (((ObjectAttributeInt)ret.GetFunctionParameterByName("ret")).GetAttributeValue() != 1)
                {
                    ReturnContainer.AddFunctionParameter((ObjectAttribute)ReturnValue0);
                    return ReturnContainer;
                }

                SubObjects.GetOffsetObject(((ObjectAttributeEvaluation)ret.GetFunctionParameterByName("ParseCount")).GetEvaluationValue());
            }

            if (cCallback != null && CallCalback)
                cCallback(lObjectInterface);

            ReturnContainer.AddFunctionParameter((ObjectAttribute)Evaluation);
            ReturnContainer.AddFunctionParameter((ObjectAttribute)ReturnValue1);
            return ReturnContainer;
        }
 public bool SubEvaluate(ObjectAttributeEvaluation Evaluation)
 {
     return EvaluateNext(Evaluation.GetEvaluationValue());
 }
        /// <summary>
        /// Gets evaluation based on if ParseCount function parameter is set or creates new ObjectAttrubteEvaluation.
        /// </summary>
        /// <returns></returns>
        protected ObjectAttributeEvaluation GetEvaluation()
        {
            ObjectAttributeEvaluation Evaluation = null;
            ObjectAttribute lParseCount;

            if (cFunctionParameters == null || (lParseCount=cFunctionParameters["ParseCount"]) == null)
            {
                Evaluation = new ObjectAttributeEvaluation();
                Evaluation.AttributeName= "ParseCount";
            }
            else
            {
                Evaluation = (ObjectAttributeEvaluation)lParseCount;
            }

            return Evaluation;
        }
        public override FunctionIO CheckRelations(ref ObjectInterfacePrototipe lObjectInterface, ParserPrimitives NextElement, bool CallCalback)
        {
            ObjectAttributeEvaluation Evaluation =  new ObjectAttributeEvaluation();
            Evaluation.AttributeName="ParseCount";
            bool negation = GetAttributeByName("negation")==null? false: true;

            bool Result = CheckCurrentObject(ref lObjectInterface);
            if (negation)
            {
                if (Result)
                    return new FunctionIO(RetVal.False);
            }
            else
            {
                if (!Result)
                    return new FunctionIO(RetVal.False);
            }

            if (cParserPrimitives.Count == 0)
            {
                if (cCallback != null && CallCalback)
                    if (!cCallback(lObjectInterface))
                        return new FunctionIO(Evaluation, RetVal.False);

                return new FunctionIO(Evaluation, RetVal.True);
            }

            //Here we preset next primitives
            for (int x = 0; x < cParserPrimitives.Count-1; x++)
                cParserPrimitives[x].SetNextPrimitive(cParserPrimitives[x + 1]);

            ObjectInterfacePrototipe SubObjects= lObjectInterface.GetSubObjects();
            if (SubObjects.GetFirstObject() == null)
                return new FunctionIO(Evaluation, RetVal.True);

            ObjectAttributeEvaluation ParseCountEvaluation = new ObjectAttributeEvaluation( EvaluationMode.Max, 1, int.MaxValue);
            for( int x=0; x< cParserPrimitives.Count; x++ )
            {
                ParserPrimitives tPrimitives;
                if (cParserPrimitives.Count == x + 1)
                    tPrimitives = null;
                else
                    tPrimitives = cParserPrimitives[x + 1];
                FunctionIO ret = cParserPrimitives[x].CheckRelations(ref SubObjects, tPrimitives, CallCalback);
                bool tResult= ret["RetVal"].Value;

                if (!negation)
                    if (!tResult)
                        return new FunctionIO(RetVal.False);
                else
                    if (tResult)
                        return new FunctionIO(RetVal.False);

                SubObjects.GetOffsetObject(ret["ParseCount"].Value);
            }

            if (cCallback != null && CallCalback)
                cCallback(lObjectInterface);

            return new FunctionIO(Evaluation, RetVal.True);
        }
 /// <summary>
 /// Evaluate value from other evaluation.
 /// </summary>
 /// <param name="Evaluation">Another evaluation.</param>
 /// <returns>RetTrue if evaluation was a success, false if not.</returns>
 public bool Evaluate(ObjectAttributeEvaluation Evaluation)
 {
     return Evaluate(Evaluation.Value);
 }