1 // Copyright 2006, 2007, 2008 The Apache Software Foundation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package org.apache.tapestry5.internal.services;
16
17 import org.apache.tapestry5.MarkupWriter;
18 import org.apache.tapestry5.internal.InternalConstants;
19 import org.apache.tapestry5.internal.structure.Page;
20 import org.apache.tapestry5.services.Environment;
21 import org.apache.tapestry5.services.MarkupRenderer;
22 import org.apache.tapestry5.services.Request;
23
24 public class PageMarkupRendererImpl implements PageMarkupRenderer
25 {
26 private final Environment environment;
27
28 private final PageRenderQueue pageRenderQueue;
29
30 private final MarkupRenderer markupRendererPipeline;
31
32 private final Request request;
33
34 public PageMarkupRendererImpl(MarkupRenderer markupRendererPipeline, PageRenderQueue pageRenderQueue,
35 Environment environment, Request request)
36 {
37 // We have to go through some awkward tricks here:
38 // - MarkupRenderer and MarkupRendererFilter are PUBLIC
39 // - Page, PageMarkupRenderer, PageRenderQueue are PRIVATE
40 // - This service is the bridge between public and private
41
42
43 this.pageRenderQueue = pageRenderQueue;
44 this.environment = environment;
45
46 this.markupRendererPipeline = markupRendererPipeline;
47 this.request = request;
48 }
49
50 public void renderPageMarkup(Page page, MarkupWriter writer)
51 {
52 // Don't clear the environment when rendering a page to a document as we may be doing so when in the middle
53 // of another render.
54
55 if (request.getAttribute(InternalConstants.GENERATING_RENDERED_PAGE) == null)
56 environment.clear();
57
58 // This is why the PRQ is scope perthread; we tell it what to render here ...
59
60 pageRenderQueue.initializeForCompletePage(page);
61
62 // ... then our statically fixed pipeline is able to (eventually) call into it.
63
64 markupRendererPipeline.renderMarkup(writer);
65
66 if (writer.getDocument().getRootElement() == null)
67 throw new RuntimeException(ServicesMessages.noMarkupFromPageRender(page));
68 }
69 }