Exemplo n.º 1
0
 /// <summary>
 /// Callback code registred for the EvalStatement event when an unkown statement is found.
 /// </summary>
 /// <param name="e">Eval Statement Arguments</param>
 protected virtual void OnEvalStatement(EvalStatementEventArgs e)
 {
     EvalStatement?.Invoke(this, e);
 }
Exemplo n.º 2
0
        /// <summary>
        /// Evaluate the statement found and return the content if the condition is verified
        /// or return and empty string.
        /// </summary>
        /// <param name="m">Matched Statement</param>
        /// <returns>Evaluated Result</returns>
        private string EvaluateStatement(Match m)
        {
            string keyword   = m.Groups["Keyword"].Value;
            string condition = m.Groups["Condition"].Value;
            string content   = m.Groups["Content"].Value.Trim(new[] { '\r', '\n' });
            string eval      = string.Empty;
            bool   hasError  = false;

            switch (keyword.ToLower())
            {
            case "if":
                if (EvalIf(condition, out hasError))
                {
                    eval = ParseStatements(content);
                }
                break;

            case "foreach":
                string forObjectName;
                IEnumerable <IPropertyAccess> forCollection;

                if (EvalForEach(condition, out hasError, out forObjectName, out forCollection))
                {
                    foreach (IPropertyAccess forObject in forCollection)
                    {
                        PropertySource[forObjectName] = forObject;
                        eval += ParseStatements(content);
                    }
                    PropertySource[forObjectName] = null;
                }
                break;

            case "with":
                string withObjectName;
                object withObjectFound;

                if (EvalWith(condition, out hasError, out withObjectName, out withObjectFound))
                {
                    if (withObjectFound is IEnumerable <IPropertyAccess> )
                    {
                        CollectionSource[withObjectName] = withObjectFound as IEnumerable <IPropertyAccess>;
                    }
                    else if (withObjectFound is IPropertyAccess)
                    {
                        PropertySource[withObjectName] = withObjectFound as IPropertyAccess;
                    }

                    eval = ParseStatements(content);

                    if (withObjectFound is IEnumerable <IPropertyAccess> )
                    {
                        CollectionSource[withObjectName] = null;
                    }
                    else if (withObjectFound is IPropertyAccess)
                    {
                        PropertySource[withObjectName] = null;
                    }
                }
                break;

            case "isinroles":
                if (EvalIsInRoles(condition, out hasError))
                {
                    eval = ParseStatements(content);
                }
                break;

            default:
                EvalStatementEventArgs evalArgs = new EvalStatementEventArgs(keyword, condition, content);

                OnEvalStatement(evalArgs);

                if (evalArgs.IsValid)
                {
                    eval = evalArgs.ParseContent ? ParseStatements(content) : evalArgs.Value;
                }
                break;
            }

            if (DebugMessages && hasError)
            {
                eval = string.Format(ConditionError, keyword, condition);
            }

            return(eval);
        }