KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > widget > screen > ScreenWidgetViewHandler


1 /*
2  * $Id: ScreenWidgetViewHandler.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.widget.screen;
26
27 import java.io.IOException JavaDoc;
28 import java.io.OutputStreamWriter JavaDoc;
29 import java.io.Writer JavaDoc;
30
31 import javax.servlet.ServletContext JavaDoc;
32 import javax.servlet.ServletOutputStream JavaDoc;
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35 import javax.xml.parsers.ParserConfigurationException JavaDoc;
36
37 import org.ofbiz.base.util.GeneralException;
38 import org.ofbiz.base.util.UtilJ2eeCompat;
39 import org.ofbiz.webapp.view.ViewHandler;
40 import org.ofbiz.webapp.view.ViewHandlerException;
41 import org.ofbiz.widget.html.HtmlScreenRenderer;
42 import org.xml.sax.SAXException JavaDoc;
43
44 /**
45  * Handles view rendering for the Screen Widget
46  *
47  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
48  * @version $Rev: 5462 $
49  * @since 3.1
50  */

51 public class ScreenWidgetViewHandler implements ViewHandler {
52
53     public static final String JavaDoc module = ScreenWidgetViewHandler.class.getName();
54     
55     protected ServletContext JavaDoc servletContext = null;
56     protected HtmlScreenRenderer htmlScreenRenderer = new HtmlScreenRenderer();
57
58     /**
59      * @see org.ofbiz.webapp.view.ViewHandler#init(javax.servlet.ServletContext)
60      */

61     public void init(ServletContext JavaDoc context) throws ViewHandlerException {
62         this.servletContext = context;
63     }
64     
65     /**
66      * @see org.ofbiz.webapp.view.ViewHandler#render(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
67      */

68     public void render(String JavaDoc name, String JavaDoc page, String JavaDoc info, String JavaDoc contentType, String JavaDoc encoding, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ViewHandlerException {
69         Writer JavaDoc writer = null;
70         try {
71             // use UtilJ2eeCompat to get this setup properly
72
boolean useOutputStreamNotWriter = false;
73             if (this.servletContext != null) {
74                 useOutputStreamNotWriter = UtilJ2eeCompat.useOutputStreamNotWriter(this.servletContext);
75             }
76             if (useOutputStreamNotWriter) {
77                 ServletOutputStream JavaDoc ros = response.getOutputStream();
78                 writer = new OutputStreamWriter JavaDoc(ros, "UTF-8");
79             } else {
80                 writer = response.getWriter();
81             }
82
83             ScreenRenderer screens = new ScreenRenderer(writer, null, htmlScreenRenderer);
84             screens.populateContextForRequest(request, response, servletContext);
85             screens.render(page);
86         } catch (IOException JavaDoc e) {
87             throw new ViewHandlerException("Error in the response writer/output stream: " + e.toString(), e);
88         } catch (SAXException JavaDoc e) {
89             throw new ViewHandlerException("XML Error rendering page: " + e.toString(), e);
90         } catch (ParserConfigurationException JavaDoc e) {
91             throw new ViewHandlerException("XML Error rendering page: " + e.toString(), e);
92         } catch (GeneralException e) {
93             throw new ViewHandlerException("Lower level error rendering page: " + e.toString(), e);
94         }
95     }
96 }
97
Popular Tags