Ejemplo n.º 1
0
 /// <inheritdoc />
 public override Func <ITag, TemplateContext, object> BuildExcuteMethod()
 {
     return((tag, context) =>
     {
         var t = tag as VariableTag;
         object baseValue = null;
         Type type = null;
         if (t.Parent == null)
         {
             return context.TempData[t.Name];
         }
         baseValue = TagExecutor.Execute(t.Parent, context);
         if (baseValue == null && t.Parent is VariableTag variable)
         {
             type = context.TempData.GetType(variable.Name);
         }
         else
         {
             type = baseValue.GetType();
         }
         if (type == null)
         {
             return null;
         }
         return DynamicHelpers.CallPropertyOrField(baseValue, t.Name, type);
     });
 }
Ejemplo n.º 2
0
        /// <inheritdoc />
        public override Func <ITag, TemplateContext, object> BuildExcuteMethod()
        {
            return((tag, context) =>
            {
                var t = tag as IfTag;
                for (int i = 0; i < t.Children.Count - 1; i++)
                {
                    var c = (ElseifTag)t.Children[i];
                    if (c == null)
                    {
                        continue;
                    }
                    if (t.Children[i] is ElseTag)
                    {
                        return TagExecutor.Execute(t.Children[i], context);
                    }

                    var condition = TagExecutor.Execute(c.Condition, context);
                    if (Utility.ToBoolean(condition))
                    {
                        return TagExecutor.Execute(t.Children[i], context);
                    }
                }
                return null;
            });
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Performs the render for a tags.
        /// </summary>
        /// <param name="writer">See the <see cref="System.IO.TextWriter"/>.</param>
        /// <param name="collection">The tags collection.</param>
        public virtual void Render(System.IO.TextWriter writer, ITag[] collection)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("\"writer\" cannot be null.");
            }

            if (collection != null && collection.Length > 0)
            {
                for (int i = 0; i < collection.Length; i++)
                {
                    try
                    {
                        var tagResult = TagExecutor.Execute(collection[i], this.Context);
                        if (tagResult != null)
                        {
                            writer.Write(tagResult.ToString());
                        }
                    }
                    catch (Exception.TemplateException e)
                    {
                        ThrowException(e, collection[i], writer);
                    }
                    catch (System.Exception e)
                    {
                        System.Exception         baseException = e.GetBaseException();
                        Exception.ParseException ex            = new Exception.ParseException(baseException.Message, baseException);
                        ThrowException(ex, collection[i], writer);
                    }
                }
            }
        }
Ejemplo n.º 4
0
 /// <inheritdoc />
 public override Func <ITag, TemplateContext, object> BuildExcuteMethod()
 {
     return((tag, context) =>
     {
         var t = tag as ElseifTag;
         var condition = TagExecutor.Execute(t.Condition, context);
         if (Utility.ToBoolean(condition))
         {
             if (t.Children.Count == 0)
             {
                 return null;
             }
             if (t.Children.Count == 1)
             {
                 return TagExecutor.Execute(t.Children[0], context);
             }
             var sb = new System.Text.StringBuilder();
             for (int i = 0; i < t.Children.Count; i++)
             {
                 sb.Append(TagExecutor.Execute(t.Children[i], context));
             }
             return sb.ToString();
         }
         return null;
     });
 }
Ejemplo n.º 5
0
 /// <inheritdoc />
 public override Func <ITag, TemplateContext, object> BuildExcuteMethod()
 {
     return((tag, context) =>
     {
         var t = tag as IndexValueTag;
         object obj = TagExecutor.Execute(t.Parent, context);
         object index = TagExecutor.Execute(t.Index, context);
         return DynamicHelpers.CallIndexValue(obj, index);
     });
 }
Ejemplo n.º 6
0
        private void RegiserExcutor(IEngine engine)
        {
            engine.RegisterExecuteFunc <ArithmeticTag>((tag, context) =>
            {
                var t          = tag as ArithmeticTag;
                var parameters = new List <object>();

                for (int i = 0; i < t.Children.Count; i++)
                {
                    var opt = t.Children[i] as OperatorTag;
                    if (opt != null)
                    {
                        parameters.Add(opt.Value);
                    }
                    else
                    {
                        parameters.Add(TagExecutor.Execute(t.Children[i], context));
                    }
                }
                var stack = ExpressionEvaluator.ProcessExpression(parameters.ToArray());
                return(ExpressionEvaluator.Calculate(stack));
            });

            engine.RegisterExecuteFunc <LogicTag>((tag, context) =>
            {
                var t = tag as LogicTag;
                List <object> parameters = new List <object>();

                for (int i = 0; i < t.Children.Count; i++)
                {
                    bool isOperator = t.Children[i] is OperatorTag;
                    object result   = TagExecutor.Execute(t.Children[i], context);
                    if (Eval(parameters, isOperator, result))
                    {
                        return(parameters[parameters.Count - 1]);
                    }
                }

                var stack = ExpressionEvaluator.ProcessExpression(parameters.ToArray());
                return(ExpressionEvaluator.Calculate(stack));
            });

            engine.RegisterExecuteFunc <ReferenceTag>((tag, context) =>
            {
                var t = tag as ReferenceTag;
                if (t.Child != null)
                {
                    return(TagExecutor.Execute(t.Child, context));
                }
                return(null);
            });
        }
Ejemplo n.º 7
0
 /// <inheritdoc />
 public override Func <ITag, TemplateContext, object> BuildExcuteMethod()
 {
     return((tag, context) =>
     {
         var t = tag as JsonTag;
         var result = new Dictionary <object, object>();
         foreach (var kv in t.Dict)
         {
             var key = kv.Key == null ? null : TagExecutor.Execute(kv.Key, context);
             var value = kv.Value == null ? null : TagExecutor.Execute(kv.Value, context);
             result.Add(key, value);
         }
         return result;
     });
 }
Ejemplo n.º 8
0
 /// <inheritdoc />
 public override Func <ITag, TemplateContext, object> BuildExcuteMethod()
 {
     return((tag, context) =>
     {
         var t = tag as SetTag;
         object value = TagExecutor.Execute(t.Value, context);
         if (value != null)
         {
             if (!context.TempData.Update(t.Name, value))
             {
                 context.TempData.Set(t.Name, value, null);
             }
         }
         return null;
     });
 }
Ejemplo n.º 9
0
 /// <inheritdoc />
 public override Func <ITag, TemplateContext, object> BuildExcuteMethod()
 {
     return((tag, context) =>
     {
         var t = tag as IncludeTag;
         object path = TagExecutor.Execute(t.Path, context);
         if (path == null)
         {
             return null;
         }
         var res = context.Load(path.ToString());
         if (res != null)
         {
             return res.Content;
         }
         return null;
     });
 }
Ejemplo n.º 10
0
 /// <inheritdoc />
 public override Func <ITag, TemplateContext, object> BuildExcuteMethod()
 {
     return((tag, context) =>
     {
         var t = tag as ForeachTag;
         if (t.Source != null)
         {
             using (var writer = new StringWriter())
             {
                 object value = TagExecutor.Execute(t.Source, context);
                 var enumerable = ForeachTag.ToIEnumerable(value);
                 TemplateContext ctx;
                 if (enumerable != null)
                 {
                     var ienum = enumerable.GetEnumerator();
                     ctx = TemplateContext.CreateContext(context);
                     int i = 0;
                     while (ienum.MoveNext())
                     {
                         i++;
                         ctx.TempData.Set(t.Name, ienum.Current, ienum.Current == null ? typeof(object) : ienum.Current.GetType());
                         //为了兼容以前的用户 foreachIndex 保留
                         ctx.TempData.Set("foreachIndex", i);
                         for (int n = 0; n < t.Children.Count; n++)
                         {
                             object result = TagExecutor.Execute(t.Children[n], ctx);
                             if (i == 0 && t.Children.Count == 1)
                             {
                                 return result;
                             }
                             if (result != null)
                             {
                                 writer.Write(result.ToString());
                             }
                         }
                     }
                 }
                 return writer.ToString();
             }
         }
         return null;
     });
 }
Ejemplo n.º 11
0
        /// <inheritdoc />
        public override Func <ITag, TemplateContext, object> BuildExcuteMethod()
        {
            return((tag, context) =>
            {
                var t = tag as ForTag;
                TagExecutor.Execute(t.Initial, context);
                //如果标签为空,则直接为false,避免死循环以内存溢出
                bool run;

                if (t.Condition == null)
                {
                    run = false;
                }
                else
                {
                    run = Utility.ToBoolean(TagExecutor.Execute(t.Condition, context));
                }
                using (var writer = new StringWriter())
                {
                    while (run)
                    {
                        for (int i = 0; i < t.Children.Count; i++)
                        {
                            var obj = TagExecutor.Execute(t.Children[i], context);
                            if (obj != null)
                            {
                                writer.Write(obj.ToString());
                            }
                        }

                        if (t.Do != null)
                        {
                            //执行计算,不需要输出,比如i++
                            TagExecutor.Execute(t.Do, context);
                        }
                        run = Utility.ToBoolean(TagExecutor.Execute(t.Condition, context));
                    }
                    return writer.ToString();
                }
            });
        }
Ejemplo n.º 12
0
 /// <inheritdoc />
 public override Func <ITag, TemplateContext, object> BuildExcuteMethod()
 {
     return((tag, context) =>
     {
         var t = tag as LayoutTag;
         object path = TagExecutor.Execute(t.Path, context);
         if (path == null)
         {
             return null;
         }
         var res = context.Load(path.ToString());
         if (res != null)
         {
             var render = new TemplateRender();
             render.Context = context;
             //render.TemplateContent = res.Content;
             var tags = render.ReadAll(res.Content);
             for (int i = 0; i < tags.Length; i++)
             {
                 if (tags[i] is BodyTag)
                 {
                     BodyTag body = (BodyTag)tags[i];
                     for (int j = 0; j < t.Children.Count; j++)
                     {
                         body.AddChild(t.Children[j]);
                     }
                     tags[i] = body;
                 }
             }
             using (System.IO.StringWriter writer = new StringWriter())
             {
                 render.Render(writer, tags);
                 return writer.ToString();
             }
         }
         return null;
     });
 }
Ejemplo n.º 13
0
 /// <inheritdoc />
 public override Func <ITag, TemplateContext, object> BuildExcuteMethod()
 {
     return((tag, context) =>
     {
         var t = tag as LoadTag;
         object path = TagExecutor.Execute(t.Path, context);
         if (path == null)
         {
             return null;
         }
         var res = context.Load(path.ToString());
         if (res != null)
         {
             var render = new TemplateRender();
             render.Context = context;
             using (System.IO.StringWriter writer = new StringWriter())
             {
                 render.Render(writer, render.ReadAll(res.Content));
                 return writer.ToString();
             }
         }
         return null;
     });
 }
Ejemplo n.º 14
0
        /// <inheritdoc />
        public override Func <ITag, TemplateContext, object> BuildExcuteMethod()
        {
            return((tag, context) =>
            {
                var t = tag as FunctaionTag;
                object[] args = new object[t.Children.Count];
                for (int i = 0; i < t.Children.Count; i++)
                {
                    args[i] = TagExecutor.Execute(t.Children[i], context);
                }
                Type type = null;
                object parentValue;
                if (t.Parent == null)
                {
                    parentValue = context.TempData[t.Name];
                }
                else
                {
                    parentValue = TagExecutor.Execute(t.Parent, context);
                    if (parentValue != null)
                    {
                        type = parentValue.GetType();
                    }
                    else
                    {
                        if (t.Parent is VariableTag variable)
                        {
                            type = context.TempData.GetType(variable.Name);
                        }
                    }
                }

                if (parentValue == null && type == null)
                {
                    return null;
                }
                if (t.Parent == null || (t.Parent != null && string.IsNullOrEmpty(t.Name)))
                {
                    //if (parentValue is FuncHandler funcHandler)
                    //{
                    //    return funcHandler(args);
                    //}
                    if (parentValue is Delegate func)
                    {
                        //var ps = func.Method.GetParameters();
                        //if (ps.Length==0)
                        return func.DynamicInvoke(args);
                    }
                    return null;
                }

                var result = DynamicHelpers.CallMethod(type, parentValue, t.Name, args);

                if (result != null)
                {
                    return result;
                }

                result = DynamicHelpers.CallPropertyOrField(parentValue, t.Name);

                if (result != null && result is Delegate)
                {
                    return (result as Delegate).DynamicInvoke(args);
                }

                return null;
            });
        }
Ejemplo n.º 15
0
 private void Awake()
 {
     executor = GetComponent <TagExecutor>();
 }