001: /*
002: * $Id: Region.java,v 1.2 2003/09/14 05:36:48 jonesde Exp $
003: *
004: * Copyright (c) 2001-2003 Sun Microsystems Inc., published in "Advanced Java Server Pages" by Prentice Hall PTR
005: * Copyright (c) 2001-2002 The Open For Business Project - www.ofbiz.org
006: *
007: * Permission is hereby granted, free of charge, to any person obtaining a
008: * copy of this software and associated documentation files (the "Software"),
009: * to deal in the Software without restriction, including without limitation
010: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
011: * and/or sell copies of the Software, and to permit persons to whom the
012: * Software is furnished to do so, subject to the following conditions:
013: *
014: * The above copyright notice and this permission notice shall be included
015: * in all copies or substantial portions of the Software.
016: *
017: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
018: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
019: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
020: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
021: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
022: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
023: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
024: *
025: */
026: package org.ofbiz.content.webapp.region;
027:
028: import java.util.HashMap;
029: import java.util.Map;
030:
031: import javax.servlet.RequestDispatcher;
032: import javax.servlet.ServletException;
033: import javax.servlet.http.HttpServletRequest;
034: import javax.servlet.http.HttpServletResponse;
035: import javax.servlet.jsp.JspException;
036: import javax.servlet.jsp.PageContext;
037:
038: import org.ofbiz.base.util.Debug;
039: import org.ofbiz.base.util.UtilJ2eeCompat;
040:
041: /**
042: * A region is content that contains a set of sections that can render in a PageContext
043: * <br>Implements abstract render(PageContext) from Content
044: *
045: * @author David M. Geary in the book "Advanced Java Server Pages"
046: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
047: * @version $Revision: 1.2 $
048: * @since 2.0
049: */
050: public class Region extends Content {
051:
052: public static final String module = Region.class.getName();
053:
054: private Map sections = new HashMap();
055: protected String id;
056:
057: public Region(String id, String content) {
058: this (id, content, null); // content is the name of a template
059: }
060:
061: public Region(String id, String content, Map sections) {
062: super (content, "region");
063: this .id = id;
064: if (sections != null) {
065: this .sections.putAll(sections);
066: }
067: }
068:
069: public String getId() {
070: return this .id;
071: }
072:
073: public void put(Section section) {
074: sections.put(section.getName(), section);
075: }
076:
077: public void putAll(Map newSections) {
078: sections.putAll(newSections);
079: }
080:
081: public Section get(String name) {
082: return (Section) sections.get(name);
083: }
084:
085: public Map getSections() {
086: return sections;
087: }
088:
089: public void render(PageContext pageContext) throws JspException {
090: if (Debug.verboseOn())
091: Debug.logVerbose("Rendering " + this .toString(), module);
092:
093: try {
094: this .render((HttpServletRequest) pageContext.getRequest(),
095: (HttpServletResponse) pageContext.getResponse());
096: } catch (java.io.IOException e) {
097: Debug.logError(e, "Error rendering region: ", module);
098: if (UtilJ2eeCompat.useNestedJspException(pageContext
099: .getServletContext()))
100: throw new JspException(e);
101: else
102: throw new JspException(e.toString());
103: } catch (ServletException e) {
104: Throwable throwable = e.getRootCause() != null ? e
105: .getRootCause() : e;
106:
107: Debug.logError(throwable, "Error rendering region: ",
108: module);
109: if (UtilJ2eeCompat.useNestedJspException(pageContext
110: .getServletContext()))
111: throw new JspException(throwable);
112: else
113: throw new JspException(throwable.toString());
114: }
115: }
116:
117: public void render(HttpServletRequest request,
118: HttpServletResponse response) throws java.io.IOException,
119: ServletException {
120: if (Debug.verboseOn())
121: Debug.logVerbose("Rendering " + this .toString(), module);
122:
123: RequestDispatcher rd = request.getRequestDispatcher(content);
124:
125: if (rd == null) {
126: throw new IllegalArgumentException(
127: "Source returned a null dispatcher (" + content
128: + ")");
129: }
130: rd.include(request, response);
131: }
132:
133: public String toString() {
134: String s = "Region: " + content + ", type=" + type;
135:
136: /*
137: int indent = 4;
138: Iterator iter = sections.values().iterator();
139:
140: while (iter.hasNext()) {
141: Section section = (Section) iter.next();
142: for (int i = 0; i < indent; ++i) {
143: s += " ";
144: }
145: s += section.toString() + "<br/>";
146: }
147: */
148: return s;
149: }
150: }
|