KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > webapp > region > Region


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

26 package org.ofbiz.webapp.region;
27
28 import java.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
30 import javax.servlet.RequestDispatcher JavaDoc;
31 import javax.servlet.ServletContext JavaDoc;
32 import javax.servlet.ServletException JavaDoc;
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35 import javax.servlet.jsp.JspException JavaDoc;
36 import javax.servlet.jsp.PageContext JavaDoc;
37
38 import org.ofbiz.base.util.Debug;
39 import org.ofbiz.base.util.UtilJ2eeCompat;
40
41 /**
42  * A region is content that contains a set of sections that can render in a PageContext
43  * <br/>Implements abstract render(PageContext) from Content
44  *
45  * @author David M. Geary in the book "Advanced Java Server Pages"
46  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
47  * @version $Rev: 5462 $
48  * @since 2.0
49  */

50 public class Region extends Content {
51     
52     public static final String JavaDoc module = Region.class.getName();
53
54     private Map JavaDoc sections = new HashMap JavaDoc();
55     protected String JavaDoc id;
56
57     public Region(String JavaDoc id, String JavaDoc content) {
58         this(id, content, null); // content is the name of a template
59
}
60
61     public Region(String JavaDoc id, String JavaDoc content, Map JavaDoc sections) {
62         super(content, "region");
63         this.id = id;
64         if (sections != null) {
65             this.sections.putAll(sections);
66         }
67     }
68
69     public String JavaDoc getId() {
70         return this.id;
71     }
72
73     public void put(Section section) {
74         sections.put(section.getName(), section);
75     }
76
77     public void putAll(Map JavaDoc newSections) {
78         sections.putAll(newSections);
79     }
80
81     public Section get(String JavaDoc name) {
82         return (Section) sections.get(name);
83     }
84
85     public Map JavaDoc getSections() {
86         return sections;
87     }
88
89     public void render(PageContext JavaDoc pageContext) throws JspException JavaDoc {
90         if (Debug.verboseOn()) Debug.logVerbose("Rendering " + this.toString(), module);
91
92         try {
93             this.render((HttpServletRequest JavaDoc) pageContext.getRequest(), (HttpServletResponse JavaDoc) pageContext.getResponse());
94         } catch (java.io.IOException JavaDoc e) {
95             Debug.logError(e, "Error rendering region: ", module);
96             if (UtilJ2eeCompat.useNestedJspException(pageContext.getServletContext()))
97                 throw new JspException JavaDoc(e);
98             else
99                 throw new JspException JavaDoc(e.toString());
100         } catch (ServletException JavaDoc e) {
101             Throwable JavaDoc throwable = e.getRootCause() != null ? e.getRootCause() : e;
102
103             Debug.logError(throwable, "Error rendering region: ", module);
104             if (UtilJ2eeCompat.useNestedJspException(pageContext.getServletContext())) {
105                 throw new JspException JavaDoc(throwable);
106             } else {
107                 throw new JspException JavaDoc(throwable.toString());
108             }
109         }
110     }
111
112     public void render(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws java.io.IOException JavaDoc, ServletException JavaDoc {
113         if (Debug.verboseOn()) Debug.logVerbose("Rendering " + this.toString(), module);
114
115         ServletContext JavaDoc application = (ServletContext JavaDoc) request.getAttribute("servletContext");
116         RequestDispatcher JavaDoc rd = application.getRequestDispatcher(content);
117         Debug.logInfo("real path for [" + content + "] is: " + application.getRealPath(content), module);
118         //RequestDispatcher rd = request.getRequestDispatcher(content);
119
if (rd == null) {
120             throw new IllegalArgumentException JavaDoc("HttpServletRequest returned a null RequestDispatcher for: [" + content + "]");
121         } else {
122             // Exception newE = new Exception("Stack Trace");
123
// Debug.logInfo(newE, "Got RD for: [" + content + "]: " + rd.toString(), module);
124
}
125         rd.include(request, response);
126     }
127
128     public String JavaDoc toString() {
129         String JavaDoc s = "Region: " + content + ", type=" + type;
130
131         /*
132          int indent = 4;
133          Iterator iter = sections.values().iterator();
134
135          while (iter.hasNext()) {
136          Section section = (Section) iter.next();
137          for (int i = 0; i < indent; ++i) {
138          s += "&nbsp;";
139          }
140          s += section.toString() + "<br/>";
141          }
142          */

143         return s;
144     }
145 }
146
Popular Tags