Esempio n. 1
0
        private static string EvalComponentExpression(ExpressionParser parser, TemplateToken token, TemplateParserContext context)
        {
            TemplateToken.ParameterizedExpression pExpr = token.ExtractParameters();

            string componentName = pExpr.MainExpression;

            if (pExpr.MainExpression.StartsWith("(") && pExpr.MainExpression.EndsWith(")"))
            {
                componentName = parser.Evaluate <string>(pExpr.MainExpression, context, token.TokenPosition);
            }

            TemplateParserContext newContext = (TemplateParserContext)context.CreateLocal();

            foreach (string varName in pExpr.Parameters.Keys)
            {
                newContext.SetLocal(varName, parser.Evaluate(pExpr.Parameters[varName], context, token.TokenPosition));
            }

            ControllerAction viewComponent = WebAppHelper.GetViewComponent(componentName);

            if (viewComponent == null)
            {
                throw new TemplateRenderingException("View component " + componentName + " not found", token.TokenPosition);
            }

            try
            {
                return(WebAppHelper.RunViewComponent(viewComponent, newContext));
            }
            catch (Exception e)
            {
                throw new TemplateRenderingException("Error rendering view component " + componentName, e, token.TokenPosition);
            }
        }
Esempio n. 2
0
        internal static object[] CreateParameters(MethodInfo method, TemplateParserContext context)
        {
            ParameterInfo[] parameters = method.GetParameters();

            object[] parameterValues = new object[parameters.Length];

            int i = 0;
            foreach (ParameterInfo parameter in parameters)
            {
                ClientDataAttribute[] attributes = (ClientDataAttribute[])parameter.GetCustomAttributes(typeof(ClientDataAttribute), true);

                ClientDataAttribute mapAttribute;

                if (attributes.Length > 0)
                    mapAttribute = attributes[0];
                else
                    mapAttribute = new GetOrPostAttribute(parameter.Name);

                object value;
                Type type;

                if (context != null && context.Get(parameter.Name, out value, out type))
                    parameterValues[i++] = value.Convert(parameter.ParameterType);
                else
                    parameterValues[i++] = GetClientValue(mapAttribute, parameter.ParameterType); //TODO: add IObjectBinder support for complex objects
            }

            return parameterValues;
        }
Esempio n. 3
0
        public object Run(Controller controller, string methodName, TemplateParserContext context)
        {
            ActionMethod actionMethod = _actionMethods[methodName];

            if (actionMethod.DefaultLayoutName != null)
            {
                controller.View.LayoutName = actionMethod.DefaultLayoutName;
            }

            if (actionMethod.DefaultViewName != null)
            {
                controller.View.ViewName = actionMethod.DefaultViewName;
            }

            if (!AllowCaching && !actionMethod.AllowCaching)
            {
                WebAppContext.Response.DisableCaching();
            }

            object returnValue = actionMethod.Invoke(controller, context);

            if (returnValue is StringBuilder)
            {
                return(returnValue.ToString());
            }

            return(returnValue);
        }
Esempio n. 4
0
        internal static string RunViewComponent(ControllerAction controllerAction, TemplateParserContext context)
        {
            ControllerClass controllerClass = controllerAction.ControllerClass;

            if (!controllerClass.IsActionMethod(controllerAction.ActionMethod))
                return null;

            using (ViewComponent component = controllerClass.CreateViewComponent(context))
            {
                try
                {
                    controllerClass.SetupController(component, context);

                    string returnValue = controllerClass.Run(component, "Run", context) as string;

                    if (returnValue != null)
                        return returnValue;

                    return TemplateUtil.ExtractBody(component.View.Render());
                }
                catch
                {
                    component.SkipTearDown = true;

                    throw;
                }
            }
        }
Esempio n. 5
0
        internal static string RunViewComponent(ControllerAction controllerAction, TemplateParserContext context)
        {
            ControllerClass controllerClass = controllerAction.ControllerClass;

            if (!controllerClass.IsActionMethod(controllerAction.ActionMethod))
            {
                return(null);
            }

            using (ViewComponent component = controllerClass.CreateViewComponent(context))
            {
                try
                {
                    controllerClass.SetupController(component, context);

                    string returnValue = controllerClass.Run(component, "Run", context) as string;

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

                    return(TemplateUtil.ExtractBody(component.View.Render()));
                }
                catch
                {
                    component.SkipTearDown = true;

                    throw;
                }
            }
        }
Esempio n. 6
0
        internal ViewComponent CreateViewComponent(TemplateParserContext context)
        {
            ViewComponent controller = (ViewComponent)Activator.CreateInstance(_classType);

            controller.Initialize(this, context);

            return(controller);
        }
Esempio n. 7
0
        public static string RenderComponent(View view, string name, params object[] parameters)
        {
            TemplateParserContext newContext = new TemplateParserContext(view, view.ViewTemplate);

            for (int i = 0; i < parameters.Length ; i+=2)
                newContext.SetLocal((string) parameters[i], parameters[i+1], parameters[i+1] == null ? typeof(object) : parameters[i+1].GetType());

            return WebAppHelper.RunViewComponent(WebAppHelper.GetViewComponent(name), newContext);
        }
Esempio n. 8
0
        public static string RenderComponent(View view, string name, params object[] parameters)
        {
            TemplateParserContext newContext = new TemplateParserContext(view, view.ViewTemplate);

            for (int i = 0; i < parameters.Length; i += 2)
            {
                newContext.SetLocal((string)parameters[i], parameters[i + 1], parameters[i + 1] == null ? typeof(object) : parameters[i + 1].GetType());
            }

            return(WebAppHelper.RunViewComponent(WebAppHelper.GetViewComponent(name), newContext));
        }
Esempio n. 9
0
 internal void TearDownController(Controller controller, TemplateParserContext context)
 {
     try
     {
         foreach (MethodInfo method in _teardownMethods)
         {
             method.Invoke(controller, WebAppHelper.CreateParameters(method, context));
         }
     }
     catch (TargetInvocationException ex)
     {
         throw ExceptionHelper.ResolveTargetInvocationException(ex);
     }
 }
Esempio n. 10
0
        protected override string OnEvalExpression(ExpressionParser parser, TemplateToken token, IParserContext context)
        {
            TemplateParserContext parserContext = (TemplateParserContext)context;

            switch (token.TokenId)
            {
            case TokenID.Control: return(EvalControlExpression(parser, token, parserContext));

            case TokenID.Component: return(EvalComponentExpression(parser, token, parserContext));

            case TokenID.Translation: return(EvalTranslationExpression(parser, token, parserContext));

            default: return(EvalExpression(parser, token, parserContext));
            }
        }
Esempio n. 11
0
        internal void SetupController(Controller controller, TemplateParserContext context)
        {
            try
            {
                foreach (MethodInfo method in _setupMethods)
                {
                    method.Invoke(controller, WebAppHelper.CreateParameters(method, context));
                }
            }
            catch (TargetInvocationException ex)
            {
                throw ExceptionHelper.ResolveTargetInvocationException(ex);
            }

            RegisterAjaxMethods(controller);
        }
Esempio n. 12
0
        protected override CompiledTemplate OnEvalParseFile(ExpressionParser parser, TemplateParser templateParser, string fileName, TemplateToken token, IParserContext context, out Dictionary <string, IValueWithType> parameters)
        {
            TemplateParserContext parserContext = (TemplateParserContext)context;

            TemplateToken.ParameterizedExpression pExpr = token.ExtractParameters();

            parameters = new Dictionary <string, IValueWithType>(StringComparer.InvariantCultureIgnoreCase);

            foreach (KeyValuePair <string, string> var in pExpr.Parameters)
            {
                parameters[var.Key] = parser.Evaluate(var.Value, context, token.TokenPosition);
            }

            ICompiledTemplate subTemplate = parserContext.Template.CompileSubTemplate(parser.Evaluate <string>(pExpr.MainExpression, context, token.TokenPosition));

            //TODO : fix this. It should be possible to include templates with different syntax
            return(((ViciCompiledTemplate)subTemplate).CompiledTemplate);
        }
Esempio n. 13
0
        internal static object[] CreateParameters(MethodInfo method, TemplateParserContext context)
        {
            ParameterInfo[] parameters = method.GetParameters();

            object[] parameterValues = new object[parameters.Length];

            int i = 0;

            foreach (ParameterInfo parameter in parameters)
            {
                ClientDataAttribute[] attributes = (ClientDataAttribute[])parameter.GetCustomAttributes(typeof(ClientDataAttribute), true);

                ClientDataAttribute mapAttribute;

                if (attributes.Length > 0)
                {
                    mapAttribute = attributes[0];
                }
                else
                {
                    mapAttribute = new GetOrPostAttribute(parameter.Name);
                }

                object value;
                Type   type;

                if (context != null && context.Get(parameter.Name, out value, out type))
                {
                    parameterValues[i++] = value.Convert(parameter.ParameterType);
                }
                else
                {
                    parameterValues[i++] = GetClientValue(mapAttribute, parameter.ParameterType);
                }
            }

            return(parameterValues);
        }
Esempio n. 14
0
        private static string EvalExpression(ExpressionParser parser, TemplateToken token, TemplateParserContext context)
        {
            char q = token.TokenId == TokenID.ExpressionNew ? '`' : ':';

            int quoteIdx = token.Text.LastIndexOf(q);

            string expr = quoteIdx >= 0 ? token.Text.Substring(0, quoteIdx).Trim() : token.Text;

            IValueWithType result = parser.Evaluate(expr, context, token.TokenPosition);

            if (token.TokenType == TemplateTokenType.Statement)
            {
                return("");
            }

            if (result.Value is Control)
            {
                return(context.View.ParseTranslations(((Control)result.Value).Render(context.View)));
            }

            string fmt = quoteIdx >= 0 ? token.Text.Substring(quoteIdx + 1).Trim() : null;

            if (fmt != null)
            {
                return(String.Format(WebAppConfig.FormatProvider, "{0:" + fmt + "}", result.Value));
            }
            else if (result.Value is string)
            {
                return((string)result.Value);
            }
            else
            {
                return(result.Value == null ? "" : Convert.ToString(result.Value, WebAppConfig.FormatProvider));
            }
        }
Esempio n. 15
0
 internal void TearDownController(Controller controller, TemplateParserContext context)
 {
     try
     {
         foreach (MethodInfo method in _teardownMethods)
             method.Invoke(controller, WebAppHelper.CreateParameters(method, context));
     }
     catch (TargetInvocationException ex)
     {
         throw ExceptionHelper.ResolveTargetInvocationException(ex);
     }
 }
Esempio n. 16
0
        internal void SetupController(Controller controller, TemplateParserContext context)
        {
            try
            {
                foreach (MethodInfo method in _setupMethods)
                    method.Invoke(controller, WebAppHelper.CreateParameters(method, context));
            }
            catch (TargetInvocationException ex)
            {
                throw ExceptionHelper.ResolveTargetInvocationException(ex);
            }

            RegisterAjaxMethods(controller);
        }
Esempio n. 17
0
        public object Run(Controller controller, string methodName, TemplateParserContext context)
        {
            ActionMethod actionMethod = _actionMethods[methodName];

            if (actionMethod.DefaultLayoutName != null)
                controller.View.LayoutName = actionMethod.DefaultLayoutName;

            if (actionMethod.DefaultViewName != null)
                controller.View.ViewName = actionMethod.DefaultViewName;

            if (!AllowCaching && !actionMethod.AllowCaching)
                WebAppContext.Response.DisableCaching();

            object returnValue = actionMethod.Invoke(controller, context);

            if (returnValue is StringBuilder)
                return returnValue.ToString();

            return returnValue;
        }
Esempio n. 18
0
        internal ViewComponent CreateViewComponent(TemplateParserContext context)
        {
            ViewComponent controller = (ViewComponent) Activator.CreateInstance(_classType);

            controller.Initialize(this, context);

            return controller;
        }
Esempio n. 19
0
 public object Invoke(object target, TemplateParserContext parserContext)
 {
     return Invoke(target, WebAppHelper.CreateParameters(_methodInfo,parserContext));
 }
Esempio n. 20
0
        private static string EvalControlExpression(ExpressionParser parser, TemplateToken token, TemplateParserContext context)
        {
            int colon1 = token.Text.LastIndexOf(':');
            int colon2 = -1;

            if (colon1 > 0)
            {
                colon2 = token.Text.LastIndexOf(':', colon1 - 1);
            }

            string expr       = token.Text;
            string className  = null;
            string classError = null;

            if (colon2 >= 0)
            {
                classError = expr.Substring(colon1 + 1).Trim();
                className  = expr.Substring(colon2 + 1, colon1 - colon2 - 1);
                expr       = expr.Substring(0, colon2).Trim();
            }
            else if (colon1 >= 0)
            {
                className = expr.Substring(colon1 + 1).Trim();
                expr      = expr.Substring(0, colon1).Trim();
            }

            Control control = parser.Evaluate <Control>(expr, context, token.TokenPosition);

            if (control != null)
            {
                return(control.Render(context.View, className, classError));
                //return context.View.ParseTranslations(html);
            }
            else
            {
                return("[?[" + expr + "]?]");
            }
        }
Esempio n. 21
0
 public TemplateParserContext(TemplateParserContext parentContext, Template template) : base(parentContext)
 {
     _template = template;
     _view = parentContext.View;
 }
Esempio n. 22
0
 private TemplateParserContext(TemplateParserContext parentContext) : base(parentContext)
 {
     _template = parentContext.Template;
     _view     = parentContext.View;
 }
Esempio n. 23
0
 public TemplateParserContext(TemplateParserContext parentContext, Template template) : base(parentContext)
 {
     _template = template;
     _view     = parentContext.View;
 }
Esempio n. 24
0
 private TemplateParserContext(TemplateParserContext parentContext) : base(parentContext)
 {
     _template = parentContext.Template;
     _view = parentContext.View;
 }
Esempio n. 25
0
 public object Invoke(object target, TemplateParserContext parserContext)
 {
     return(Invoke(target, WebAppHelper.CreateParameters(_methodInfo, parserContext)));
 }
Esempio n. 26
0
        private static string EvalTranslationExpression(ExpressionParser parser, TemplateToken token, TemplateParserContext context)
        {
            int parenIdx = token.Text.IndexOf('(');

            string tag        = token.Text;
            string parameters = null;

            if (parenIdx > 0)
            {
                parameters = token.Text.Substring(parenIdx + 1);
                tag        = token.Text.Substring(0, parenIdx).Trim();
            }

            string translation = TranslationHelper.GetTranslation(context.View.ViewName, tag);

            if (translation == null)
            {
                return("{?{" + tag + "}?}");
            }

            if (parameters != null)
            {
                IParserContext localContext = context.CreateLocal();

                localContext.Set("__Translation__", translation);

                translation = parser.Evaluate <string>("string.Format(__Translation__," + parameters, localContext, token.TokenPosition);
            }

            return(translation);
        }