Esempio n. 1
0
        public override Statement Expand(MacroStatement macro)
        {
            if (macro.Arguments.Count == 0)
            {
                throw new MonoRailException("Section must be called with a name");
            }

            var component = GetParentComponent(macro);

            componentContextName  = ComponentNaming.GetComponentContextName(component);
            componentVariableName = ComponentNaming.GetComponentNameFor(component);


            var sectionName = macro.Arguments[0].ToString();
            var block       = new Block();
            //if (!Component.SupportsSection(section.Name))
            //   throw new ViewComponentException( String.Format("The section '{0}' is not supported by the ViewComponent '{1}'", section.Name, ComponentName));
            var supportsSection = new MethodInvocationExpression(
                AstUtil.CreateReferenceExpression(componentVariableName + ".SupportsSection"),
                new StringLiteralExpression(sectionName));
            //create the new exception
            var raiseSectionNotSupportted = new RaiseStatement(
                new MethodInvocationExpression(
                    AstUtil.CreateReferenceExpression(typeof(ViewComponentException).FullName),
                    new StringLiteralExpression(
                        String.Format("The section '{0}' is not supported by the ViewComponent '{1}'", sectionName,
                                      component.Arguments[0])
                        )
                    ));

            var trueBlock = new Block();

            trueBlock.Add(raiseSectionNotSupportted);
            var ifSectionNotSupported =
                new IfStatement(new UnaryExpression(UnaryOperatorType.LogicalNot, supportsSection),
                                trueBlock, null);

            block.Add(ifSectionNotSupported);
            //componentContext.RegisterSection(sectionName);
            var mie = new MethodInvocationExpression(
                new MemberReferenceExpression(new ReferenceExpression(componentContextName), "RegisterSection"),
                new StringLiteralExpression(sectionName),
                CodeBuilderHelper.CreateCallableFromMacroBody(CodeBuilder, macro));

            block.Add(mie);

            var sections = (IDictionary)component["sections"];

            if (sections == null)
            {
                component["sections"] = sections = new Hashtable();
            }
            sections.Add(sectionName, block);
            return(null);
        }
        public override Statement Expand(MacroStatement macro)
        {
            componentContextName  = ComponentNaming.GetComponentContextName(macro);
            componentFactoryName  = ComponentNaming.GetComponentFactoryName(macro);
            componentVariableName = ComponentNaming.GetComponentNameFor(macro);

            if (macro.Arguments.Count == 0)
            {
                throw new MonoRailException("Component must be called with a name");
            }

            var block = new Block();

            var method = (Method)macro.GetAncestor(NodeType.Method);

            var componentName = new StringLiteralExpression(macro.Arguments[0].ToString());

            var dictionary = CreateParametersDictionary(macro);

            var macroBody = CodeBuilderHelper.CreateCallableFromMacroBody(CodeBuilder, macro);

            var initContext = new MethodInvocationExpression
            {
                Target = AstUtil.CreateReferenceExpression("Castle.MonoRail.Views.Brail.BrailViewComponentContext")
            };

            initContext.Arguments.Extend(
                new[]
            {
                new SelfLiteralExpression(),
                macroBody, componentName,
                AstUtil.CreateReferenceExpression("OutputStream"),
                dictionary
            });

            // compilerContext = BrailViewComponentContext(macroBodyClosure, "componentName", OutputStream, dictionary)
            block.Add(new BinaryExpression(BinaryOperatorType.Assign,
                                           new ReferenceExpression(componentContextName), initContext));

            // AddViewComponentProperties( compilerContext.ComponentParams )
            var addProperties =
                new MethodInvocationExpression(AstUtil.CreateReferenceExpression("AddViewComponentProperties"));

            addProperties.Arguments.Add(AstUtil.CreateReferenceExpression(componentContextName + ".ComponentParameters"));
            block.Add(addProperties);

            var viewComponentFactoryLocal = CodeBuilder.DeclareLocal(method, componentFactoryName,
                                                                     TypeSystemServices.Map(
                                                                         typeof(IViewComponentFactory)));

            // viewComponentFactory = context.GetService(IViewComponentFactory)
            var callService = new MethodInvocationExpression(
                AstUtil.CreateReferenceExpression("context.GetService"));

            callService.Arguments.Add(CodeBuilder.CreateTypeofExpression(typeof(IViewComponentFactory)));

            block.Add(new BinaryExpression(BinaryOperatorType.Assign,
                                           CodeBuilder.CreateLocalReference(componentFactoryName, viewComponentFactoryLocal),
                                           callService));

            // component = viewComponentFactory.Create( componentName)
            var createComponent = new MethodInvocationExpression(
                new MemberReferenceExpression(CodeBuilder.CreateLocalReference(componentFactoryName, viewComponentFactoryLocal),
                                              "Create"));

            createComponent.Arguments.Add(componentName);
            block.Add(new BinaryExpression(BinaryOperatorType.Assign,
                                           new ReferenceExpression(componentVariableName),
                                           createComponent));
            AddSections(block, macro);

            // component.Init(context, componentContext)
            var initComponent = new MethodInvocationExpression(
                AstUtil.CreateReferenceExpression(componentVariableName + ".Init"));

            initComponent.Arguments.Extend(
                new Expression[]
            {
                new ReferenceExpression("context"),
                new ReferenceExpression(componentContextName)
            });

            block.Add(initComponent);

            // component.Render()
            block.Add(new MethodInvocationExpression(
                          AstUtil.CreateReferenceExpression(componentVariableName + ".Render")));

            // if component.ViewToRender is not null:
            //	OutputSubView("/"+component.ViewToRender, context.CompnentParameters)
            var renderView    = new Block();
            var outputSubView = new MethodInvocationExpression(
                AstUtil.CreateReferenceExpression("OutputSubView"));

            outputSubView.Arguments.Add(new BinaryExpression(BinaryOperatorType.Addition,
                                                             new StringLiteralExpression("/"),
                                                             AstUtil.CreateReferenceExpression(componentContextName +
                                                                                               ".ViewToRender")));

            outputSubView.Arguments.Add(AstUtil.CreateReferenceExpression(componentContextName + ".ComponentParameters"));
            renderView.Add(outputSubView);

            block.Add(new IfStatement(AstUtil.CreateReferenceExpression(componentContextName + ".ViewToRender"),
                                      renderView, new Block()));

            // RemoveViewComponentProperties( compilerContext.ComponentParams )
            var removeProperties =
                new MethodInvocationExpression(AstUtil.CreateReferenceExpression("RemoveViewComponentProperties"));

            removeProperties.Arguments.Add(AstUtil.CreateReferenceExpression(componentContextName + ".ComponentParameters"));
            block.Add(removeProperties);

            return(block);
        }