KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: RenderTag.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 javax.servlet.http.HttpServletRequest JavaDoc;
29 import javax.servlet.jsp.JspException JavaDoc;
30
31 import org.ofbiz.base.util.Debug;
32
33 /**
34  * Tag to render a region
35  *
36  * @author David M. Geary in the book "Advanced Java Server Pages"
37  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
38  * @version $Rev: 5462 $
39  * @since 2.0
40  */

41 public class RenderTag extends RegionTag {
42     
43     public static final String JavaDoc module = RenderTag.class.getName();
44     
45     private String JavaDoc sectionName = null;
46     private String JavaDoc role = null;
47     private String JavaDoc permission = null;
48     private String JavaDoc action = null;
49
50     public void setSection(String JavaDoc s) {
51         this.sectionName = s;
52     }
53
54     public void setRole(String JavaDoc s) {
55         this.role = s;
56     }
57
58     public void setPermission(String JavaDoc permission) {
59         this.permission = permission;
60     }
61
62     public void setAction(String JavaDoc action) {
63         this.action = action;
64     }
65
66     protected boolean renderingRegion() {
67         return sectionName == null;
68     }
69
70     protected boolean renderingSection() {
71         return sectionName != null;
72     }
73
74     public int doStartTag() throws JspException JavaDoc {
75         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc)
76             pageContext.getRequest();
77
78         if (role != null && !request.isUserInRole(role))
79             return SKIP_BODY;
80
81         if (renderingRegion()) {
82             if (!findRegionByKey()) {
83                 createRegionFromTemplate(null);
84             }
85             RegionStack.push(pageContext.getRequest(), regionObj);
86         }
87         return EVAL_BODY_INCLUDE;
88     }
89
90     public int doEndTag() throws JspException JavaDoc {
91         Region regionEnd = null;
92
93         try {
94             regionEnd = RegionStack.peek(pageContext.getRequest());
95         } catch (Exception JavaDoc e) {
96             throw new JspException JavaDoc("Error finding region on stack: " + e.getMessage());
97         }
98
99         if (regionEnd == null)
100             throw new JspException JavaDoc("Can't find region on stack");
101
102         if (renderingSection()) {
103             Section section = regionEnd.get(sectionName);
104
105             if (section == null)
106                 return EVAL_PAGE; // ignore missing sections
107

108             section.render(pageContext);
109         } else if (renderingRegion()) {
110             try {
111                 regionEnd.render(pageContext);
112                 RegionStack.pop(pageContext.getRequest());
113             } catch (Exception JavaDoc ex) {
114                 Debug.logError(ex, "Error rendering region [" + regionEnd.getId() + "]: ", module);
115                 // IOException or ServletException
116
throw new JspException JavaDoc("Error rendering region [" + regionEnd.getId() + "]: " + ex.getMessage());
117             }
118         }
119         return EVAL_PAGE;
120     }
121
122     public void release() {
123         super.release();
124         sectionName = role = null;
125     }
126 }
127
Popular Tags