You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Lance Java <la...@googlemail.com> on 2012/02/27 14:57:06 UTC

How does MultiZoneUpdateEventResultProcessor get a html string from a RenderCommand

I thought I'd start another thread since my last thread got off topic
without answering my initial question.

I would like to write a component that takes a RenderCommand parameter (or
a Block parameter) and uses tapestry's template engine to get a html string
on the serverside. I do not want the html to be rendered to the response
output stream. I would instead like to use this html string to construct a
JSONObject.

I assume that tapestry must already be doing this somewhere in
MultiZoneUpdateEventResultProcessor which generates a JSONObject containing
all of the html strings to update on the client. I find the code hard to
follow and I'm hoping that someone can give me a snippit of code which will
take a RenderCommand and generate a html string.

This is my current attempt but I can't get it to work:

public class TmlToString {
        @Parameter
        @Property
        private RenderCommand renderMe;

        @BeginRender
        public void beginRender() {
                MarkupWriter writer = new MarkupWriterImpl();

                Logger logger = LoggerFactory.getLogger(TmlToString.class);
                RenderQueue renderQueue = new RenderQueueImpl(logger);

                Element element = writer.element("div");
                renderMe.render(writer, renderQueue);
                writer.end();

                System.out.println("childMarkup: " +
element.getChildMarkup()); // this is an empty string
                System.out.println("toString: " + element.toString()); //
prints <div></div>
        }
}


Cheers,
Lance.

Re: How does MultiZoneUpdateEventResultProcessor get a html string from a RenderCommand

Posted by Howard Lewis Ship <hl...@gmail.com>.
The code is simpler in 5.4, using the AjaxResponseRenderer, but it's
basically the same thing:
- Contribute a filter into the active thread's PartialMarkupRenderer pipeline
- The filter creates an empty element for the renderable object
- The renderable object renders markup inside the element
- The body of the element is captured and stuffed into a JSONObject key

On Mon, Feb 27, 2012 at 6:10 AM, Thiago H. de Paula Figueiredo
<th...@gmail.com> wrote:
> Hi!
>
> Have you checked its source? Here's the 5.1.0.5 one (which is the version
> used in  the project I'm working now):
>
> public class MultiZoneUpdateEventResultProcessor implements
> ComponentEventResultProcessor<MultiZoneUpdate>
> {
>    private final PageRenderQueue queue;
>
>    private final TypeCoercer typeCoercer;
>
>    public MultiZoneUpdateEventResultProcessor(PageRenderQueue queue,
> TypeCoercer typeCoercer)
>    {
>        this.queue = queue;
>        this.typeCoercer = typeCoercer;
>    }
>
>    public void processResultValue(final MultiZoneUpdate value) throws
> IOException
>    {
>        // There has to be at least a single command in the queue to force a
> render.
>        queue.initializeForPartialPageRender(new RenderCommand()
>        {
>            public void render(MarkupWriter writer, RenderQueue queue)
>            {
>            }
>        });
>
>        queue.addPartialMarkupRendererFilter(new SetupZonesFilter());
>
>        Map<String, Object> map = value.getZoneToRenderMap();
>
>        for (String zoneId : map.keySet())
>        {
>            Object provided = map.get(zoneId);
>
>            RenderCommand zoneRenderCommand = toRenderer(zoneId, provided);
>
>            queue.addPartialMarkupRendererFilter(new
> SingleZonePartialRendererFilter(zoneId, zoneRenderCommand, queue));
>        }
>    }
>
>    private RenderCommand toRenderer(String zoneId, Object provided)
>    {
>        try
>        {
>            return typeCoercer.coerce(provided, RenderCommand.class);
>        }
>        catch (Exception ex)
>        {
>            throw new IllegalArgumentException(String.format("Failure
> converting renderer for zone '%s': %s",
>                                                             zoneId,
>
> InternalUtils.toMessage(ex)), ex);
>        }
>    }
> }
>
> --
> Thiago H. de Paula Figueiredo
> Independent Java, Apache Tapestry 5 and Hibernate consultant, developer, and
> instructor
> Owner, Ars Machina Tecnologia da Informação Ltda.
> http://www.arsmachina.com.br
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>



-- 
Howard M. Lewis Ship

Creator of Apache Tapestry

The source for Tapestry training, mentoring and support. Contact me to
learn how I can get you up and productive in Tapestry fast!

(971) 678-5210
http://howardlewisship.com

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: How does MultiZoneUpdateEventResultProcessor get a html string from a RenderCommand

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
Hi!

Have you checked its source? Here's the 5.1.0.5 one (which is the version  
used in  the project I'm working now):

public class MultiZoneUpdateEventResultProcessor implements  
ComponentEventResultProcessor<MultiZoneUpdate>
{
     private final PageRenderQueue queue;

     private final TypeCoercer typeCoercer;

     public MultiZoneUpdateEventResultProcessor(PageRenderQueue queue,  
TypeCoercer typeCoercer)
     {
         this.queue = queue;
         this.typeCoercer = typeCoercer;
     }

     public void processResultValue(final MultiZoneUpdate value) throws  
IOException
     {
         // There has to be at least a single command in the queue to force  
a render.
         queue.initializeForPartialPageRender(new RenderCommand()
         {
             public void render(MarkupWriter writer, RenderQueue queue)
             {
             }
         });

         queue.addPartialMarkupRendererFilter(new SetupZonesFilter());

         Map<String, Object> map = value.getZoneToRenderMap();

         for (String zoneId : map.keySet())
         {
             Object provided = map.get(zoneId);

             RenderCommand zoneRenderCommand = toRenderer(zoneId, provided);

             queue.addPartialMarkupRendererFilter(new  
SingleZonePartialRendererFilter(zoneId, zoneRenderCommand, queue));
         }
     }

     private RenderCommand toRenderer(String zoneId, Object provided)
     {
         try
         {
             return typeCoercer.coerce(provided, RenderCommand.class);
         }
         catch (Exception ex)
         {
             throw new IllegalArgumentException(String.format("Failure  
converting renderer for zone '%s': %s",
                                                              zoneId,
                                                              InternalUtils.toMessage(ex)),  
ex);
         }
     }
}

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org