KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > module > sitemesh > freemarker > FreemarkerDecoratorServlet


1 /*
2  * Title: FreemarkerDecoratorServlet Description:
3  *
4  * This software is published under the terms of the OpenSymphony Software License version 1.1, of which a copy has been included
5  * with this distribution in the LICENSE.txt file.
6  */

7
8 package com.opensymphony.module.sitemesh.freemarker;
9
10 import java.io.IOException JavaDoc;
11 import java.io.StringWriter JavaDoc;
12
13 import javax.servlet.ServletException JavaDoc;
14 import javax.servlet.http.HttpServletRequest JavaDoc;
15 import javax.servlet.http.HttpServletResponse JavaDoc;
16
17 import com.opensymphony.module.sitemesh.HTMLPage;
18 import com.opensymphony.module.sitemesh.RequestConstants;
19
20 import freemarker.ext.servlet.FreemarkerServlet;
21 import freemarker.template.SimpleHash;
22 import freemarker.template.Template;
23 import freemarker.template.TemplateModel;
24
25 /**
26  * Servlet that allows Freemarker templates to be used as decorators.
27  *
28  * @author <a HREF="mailto:richard.hallier@freesbee.fr">Richard HALLIER</a>
29  * @version $Revision: 1.2 $
30  */

31 public class FreemarkerDecoratorServlet extends FreemarkerServlet
32 {
33     /* (non-Javadoc)
34      * @see freemarker.ext.servlet.FreemarkerServlet#preTemplateProcess(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, freemarker.template.Template, freemarker.template.TemplateModel)
35      */

36     protected boolean preTemplateProcess(
37         HttpServletRequest JavaDoc request,
38         HttpServletResponse JavaDoc response,
39         Template template,
40         TemplateModel templateModel)
41         throws ServletException JavaDoc, IOException JavaDoc
42     {
43         boolean result=super.preTemplateProcess(request, response, template, templateModel);
44         
45         SimpleHash hash = (SimpleHash) templateModel;
46         
47         HTMLPage htmlPage = (HTMLPage) request.getAttribute(RequestConstants.PAGE);
48
49         String JavaDoc title, body, head;
50
51         if(htmlPage==null)
52         {
53             title="No Title";
54             body="No Body";
55             head="<!-- No head -->";
56         }
57         else
58         {
59             title=htmlPage.getTitle();
60             
61             StringWriter JavaDoc buffer = new StringWriter JavaDoc();
62             htmlPage.writeBody(buffer);
63             body=buffer.toString();
64             
65             buffer = new StringWriter JavaDoc();
66             htmlPage.writeHead(buffer);
67             head=buffer.toString();
68
69             hash.put("page",htmlPage);
70         }
71         
72         hash.put("title",title);
73         hash.put("body",body);
74         hash.put("head",head);
75         hash.put("base",request.getContextPath());
76         
77         /*
78         Factory factory = Factory.getInstance(new Config(getServletConfig()));
79         Decorator decorator = factory.getDecoratorMapper().getDecorator(request, htmlPage);
80         -> decorator.getPage()
81         */

82         
83         return result;
84     }
85 }
86
Popular Tags