001: /*
002: * $Id: RenderTag.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 javax.servlet.http.HttpServletRequest;
029: import javax.servlet.jsp.JspException;
030:
031: import org.ofbiz.base.util.Debug;
032:
033: /**
034: * Tag to render a region
035: *
036: * @author David M. Geary in the book "Advanced Java Server Pages"
037: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
038: * @version $Revision: 1.2 $
039: * @since 2.0
040: */
041: public class RenderTag extends RegionTag {
042:
043: public static final String module = RenderTag.class.getName();
044:
045: private String sectionName = null;
046: private String role = null;
047: private String permission = null;
048: private String action = null;
049:
050: public void setSection(String s) {
051: this .sectionName = s;
052: }
053:
054: public void setRole(String s) {
055: this .role = s;
056: }
057:
058: public void setPermission(String permission) {
059: this .permission = permission;
060: }
061:
062: public void setAction(String action) {
063: this .action = action;
064: }
065:
066: protected boolean renderingRegion() {
067: return sectionName == null;
068: }
069:
070: protected boolean renderingSection() {
071: return sectionName != null;
072: }
073:
074: public int doStartTag() throws JspException {
075: HttpServletRequest request = (HttpServletRequest) pageContext
076: .getRequest();
077:
078: if (role != null && !request.isUserInRole(role))
079: return SKIP_BODY;
080:
081: if (renderingRegion()) {
082: if (!findRegionByKey()) {
083: createRegionFromTemplate(null);
084: }
085: RegionStack.push(pageContext.getRequest(), regionObj);
086: }
087: return EVAL_BODY_INCLUDE;
088: }
089:
090: public int doEndTag() throws JspException {
091: Region regionEnd = null;
092:
093: try {
094: regionEnd = RegionStack.peek(pageContext.getRequest());
095: } catch (Exception e) {
096: throw new JspException("Error finding region on stack: "
097: + e.getMessage());
098: }
099:
100: if (regionEnd == null)
101: throw new JspException("Can't find region on stack");
102:
103: if (renderingSection()) {
104: Section section = regionEnd.get(sectionName);
105:
106: if (section == null)
107: return EVAL_PAGE; // ignore missing sections
108:
109: section.render(pageContext);
110: } else if (renderingRegion()) {
111: try {
112: regionEnd.render(pageContext);
113: RegionStack.pop(pageContext.getRequest());
114: } catch (Exception ex) {
115: Debug.logError(ex, "Error rendering region ["
116: + regionEnd.getId() + "]: ", module);
117: // IOException or ServletException
118: throw new JspException("Error rendering region ["
119: + regionEnd.getId() + "]: " + ex.getMessage());
120: }
121: }
122: return EVAL_PAGE;
123: }
124:
125: public void release() {
126: super.release();
127: sectionName = role = null;
128: }
129: }
|