001// Copyright 2006, 2007, 2008, 2010, 2011 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007// http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry5.internal.services;
016
017import org.apache.tapestry5.MarkupWriter;
018import org.apache.tapestry5.internal.structure.Page;
019import org.apache.tapestry5.services.MarkupRenderer;
020
021public class PageMarkupRendererImpl implements PageMarkupRenderer
022{
023    private final PageRenderQueue pageRenderQueue;
024
025    private final MarkupRenderer markupRendererPipeline;
026
027    public PageMarkupRendererImpl(MarkupRenderer markupRendererPipeline, PageRenderQueue pageRenderQueue)
028    {
029        // We have to go through some awkward tricks here:
030        // - MarkupRenderer and MarkupRendererFilter are PUBLIC
031        // - Page, PageMarkupRenderer, PageRenderQueue are PRIVATE
032        // - This service is the bridge between public and private
033
034        this.pageRenderQueue = pageRenderQueue;
035
036        this.markupRendererPipeline = markupRendererPipeline;
037    }
038
039    public void renderPageMarkup(Page page, MarkupWriter writer)
040    {
041        // This is why the PRQ is scope perthread; we tell it what to render here ...
042
043        pageRenderQueue.initializeForCompletePage(page);
044
045        // ... then our statically fixed pipeline is able to (eventually) call into it.
046
047        markupRendererPipeline.renderMarkup(writer);
048
049        if (writer.getDocument().getRootElement() == null)
050        {
051            throw new RuntimeException(String.format("Page %s did not generate any markup when rendered. This could be because its template file could not be located, or because a render phase method in the page prevented rendering.", page.getName()));
052        }
053    }
054}