Source Code Cross Referenced for FreeMarkerViewHandler.java in  » ERP-CRM-Financial » SourceTap-CRM » org » ofbiz » content » webapp » ftl » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » ERP CRM Financial » SourceTap CRM » org.ofbiz.content.webapp.ftl 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Id: FreeMarkerViewHandler.java,v 1.7 2003/12/23 13:50:40 jonesde Exp $
003:         *
004:         * Copyright (c) 2001-2003 The Open For Business Project - www.ofbiz.org
005:         *
006:         * Permission is hereby granted, free of charge, to any person obtaining a
007:         * copy of this software and associated documentation files (the "Software"),
008:         * to deal in the Software without restriction, including without limitation
009:         * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010:         * and/or sell copies of the Software, and to permit persons to whom the
011:         * Software is furnished to do so, subject to the following conditions:
012:         *
013:         * The above copyright notice and this permission notice shall be included
014:         * in all copies or substantial portions of the Software.
015:         *
016:         * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017:         * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018:         * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019:         * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020:         * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021:         * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022:         * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023:         *
024:         */
025:        package org.ofbiz.content.webapp.ftl;
026:
027:        import java.io.File;
028:        import java.io.IOException;
029:        import java.util.HashMap;
030:        import java.util.Map;
031:
032:        import javax.servlet.ServletContext;
033:        import javax.servlet.http.HttpServletRequest;
034:        import javax.servlet.http.HttpServletResponse;
035:        import javax.servlet.http.HttpSession;
036:
037:        import org.ofbiz.base.util.UtilHttp;
038:        import org.ofbiz.content.webapp.view.ViewHandler;
039:        import org.ofbiz.content.webapp.view.ViewHandlerException;
040:
041:        import freemarker.ext.beans.BeansWrapper;
042:        import freemarker.ext.jsp.TaglibFactory;
043:        import freemarker.ext.servlet.HttpRequestHashModel;
044:        import freemarker.ext.servlet.HttpSessionHashModel;
045:        import freemarker.template.Configuration;
046:        import freemarker.template.SimpleHash;
047:        import freemarker.template.Template;
048:        import freemarker.template.TemplateException;
049:        import freemarker.template.WrappingTemplateModel;
050:
051:        /**
052:         * FreemarkerViewHandler - Freemarker Template Engine View Handler
053:         *
054:         * @author     <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
055:         * @version    $Revision: 1.7 $
056:         * @since      2.1
057:         */
058:        public class FreeMarkerViewHandler implements  ViewHandler {
059:
060:            public static final String module = FreeMarkerViewHandler.class
061:                    .getName();
062:
063:            protected ServletContext servletContext = null;
064:            protected Configuration config = null;
065:
066:            public void init(ServletContext context)
067:                    throws ViewHandlerException {
068:                this .servletContext = context;
069:                config = Configuration.getDefaultConfiguration();
070:                //nice thought, but doesn't do auto reloading with this: config.setServletContextForTemplateLoading(context, "/");
071:                try {
072:                    config.setDirectoryForTemplateLoading(new File(context
073:                            .getRealPath("/")));
074:                } catch (java.io.IOException e) {
075:                    throw new ViewHandlerException(
076:                            "Could not create file for webapp root path", e);
077:                }
078:                WrappingTemplateModel.setDefaultObjectWrapper(BeansWrapper
079:                        .getDefaultInstance());
080:                try {
081:                    config.setObjectWrapper(BeansWrapper.getDefaultInstance());
082:                    config.setCacheStorage(new OfbizCacheStorage("unknown"));
083:                    config.setSetting("datetime_format",
084:                            "yyyy-MM-dd HH:mm:ss.SSS");
085:                } catch (TemplateException e) {
086:                    throw new ViewHandlerException(
087:                            "Freemarker TemplateException", e.getCause());
088:                }
089:            }
090:
091:            public void render(String name, String page, String info,
092:                    String contentType, String encoding,
093:                    HttpServletRequest request, HttpServletResponse response)
094:                    throws ViewHandlerException {
095:                if (page == null || page.length() == 0)
096:                    throw new ViewHandlerException("Invalid template source");
097:
098:                // make the root context (data model) for freemarker
099:                SimpleHash root = new SimpleHash(BeansWrapper
100:                        .getDefaultInstance());
101:                prepOfbizRoot(root, request, response);
102:
103:                // get the template
104:                Template template = null;
105:                try {
106:                    template = config.getTemplate(page, request.getLocale());
107:                } catch (IOException e) {
108:                    throw new ViewHandlerException(
109:                            "Cannot open template file: " + page, e);
110:                }
111:                template.setObjectWrapper(BeansWrapper.getDefaultInstance());
112:
113:                // process the template & flush the output
114:                try {
115:                    template.process(root, response.getWriter(), BeansWrapper
116:                            .getDefaultInstance());
117:                    response.flushBuffer();
118:                } catch (TemplateException te) {
119:                    throw new ViewHandlerException(
120:                            "Problems processing Freemarker template", te);
121:                } catch (IOException ie) {
122:                    throw new ViewHandlerException(
123:                            "Problems writing to output stream", ie);
124:                }
125:            }
126:
127:            public static void prepOfbizRoot(SimpleHash root,
128:                    HttpServletRequest request, HttpServletResponse response) {
129:                Map rootPrep = new HashMap();
130:                prepOfbizRoot(rootPrep, request, response);
131:                root.putAll(rootPrep);
132:            }
133:
134:            public static void prepOfbizRoot(Map root,
135:                    HttpServletRequest request, HttpServletResponse response) {
136:                ServletContext servletContext = (ServletContext) request
137:                        .getAttribute("servletContext");
138:                HttpSession session = request.getSession();
139:
140:                BeansWrapper wrapper = BeansWrapper.getDefaultInstance();
141:
142:                // add in the OFBiz objects
143:                root.put("delegator", request.getAttribute("delegator"));
144:                root.put("dispatcher", request.getAttribute("dispatcher"));
145:                root.put("security", request.getAttribute("security"));
146:                root.put("userLogin", session.getAttribute("userLogin"));
147:
148:                // add the response object (for transforms) to the context as a BeanModel
149:                root.put("response", response);
150:
151:                // add the application object (for transforms) to the context as a BeanModel
152:                root.put("application", servletContext);
153:
154:                // add the servlet context -- this has been deprecated, and now requires servlet, do we really need it?
155:                //root.put("applicationAttributes", new ServletContextHashModel(servletContext, BeansWrapper.getDefaultInstance()));                       
156:
157:                // add the session object (for transforms) to the context as a BeanModel
158:                root.put("session", session);
159:
160:                // add the session
161:                root.put("sessionAttributes", new HttpSessionHashModel(session,
162:                        wrapper));
163:
164:                // add the request object (for transforms) to the context as a BeanModel
165:                root.put("request", request);
166:
167:                // add the request
168:                root.put("requestAttributes", new HttpRequestHashModel(request,
169:                        wrapper));
170:
171:                // add the request parameters -- this now uses a Map from UtilHttp
172:                Map requestParameters = UtilHttp.getParameterMap(request);
173:                root.put("requestParameters", requestParameters);
174:
175:                // add the TabLibFactory
176:                TaglibFactory JspTaglibs = new TaglibFactory(servletContext);
177:                root.put("JspTaglibs", JspTaglibs);
178:
179:                FreeMarkerWorker.addAllOfbizTransforms(root);
180:            }
181:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.